How to combine a multiMatchQuery with a boolQuery using Spring Data Elastic Search - spring-data-elasticsearch

I am using a multi match query like so:
var builder = QueryBuilders.multiMatchQuery(search, "projectName", "jobNumber");
var query = new NativeSearchQueryBuilder()
.withQuery(builder)
.withPageable(pageable)
.build();
var result = searchOperations.search(query, Foo.class);
Now I need to make sure that the _id field also matches the FooId field. How do I create a query to do that and combine it with my multi match query?

If both clauses are mandatory, you can combine them using must method of BoolQueryBuilder:
var builder = new BoolQueryBuilder()
.must(QueryBuilders.multiMatchQuery(search, "projectName", "jobNumber"))
.must(QueryBuilders.termQuery("_id", fooId));
var query = new NativeSearchQueryBuilder()
.withQuery(builder)
.withPageable(pageable)
.build();
var result = searchOperations.search(query, Foo.class);
Or you can define the clause by id as a filter having the same result:
var builder = QueryBuilders.multiMatchQuery(search, "projectName", "jobNumber");
var filter = QueryBuilders.termQuery("_id", fooId)
var query = new NativeSearchQueryBuilder()
.withQuery(builder)
.withFilter(filter)
.withPageable(pageable)
.build();
var result = searchOperations.search(query, Foo.class);

Related

Shuffle the results of a LINQ query based on SessionID

I'm working on an ASP.NET MVC application with Entity Framework 6 and a SQL Server database.
I'm trying to shuffle the results of a query by adding a SortingCode which I'd like to assign a value based on the current SessionId, so that every time the returned rows are shuffled without affecting the pagination. SortingCode in this attempt is a string, but it can be any type, as long as it allows me to get shuffled results. I have something like this:
var sessionId = Session.SessionID.GetHashCode();
var rnd = new Random(sessionId);
var query = (from l in _context.Adverts
select new AdvertSummary
{
Id = l.Id,
Title = l.Title,
Description = l.Description,
SortingCode = l.Title.OrderBy(x => rnd.Next()).ToString(),
});
The IQueryable result is then converted into a list later on in my code with:
var pagedResults = query.Skip(skip).Take(pageSize).ToList();
The above attempt with the Random class doesn't work, and results in an error
DbExpressionBinding requires an input expression with a collection ResultType
Is there anything that I can do to get shuffled results?
I would suggest to use SqlFunctions.Checksum for such task. SortingCode will be nearly close to the seeded Random.
var sessionId = Session.SessionID;
var query =
from l in _context.Adverts
select new AdvertSummary
{
Id = l.Id,
Title = l.Title,
Description = l.Description,
SortingCode = SqlFunctions.Checksum(sessionId, l.Title)
};
var pagedResults = query
.OrderBy(x => x.SortingCode)
.ThenBy(x => x.Id)
.Skip(skip)
.Take(pageSize)
.ToList();

Query related work items

In TFS, some of work items are assigned. For those tasks test cases work items created and linked to the appropriate tasks.
I want to get the list of test cases linked for a particular task. How to write query to get it?
You have to use link query to get that:
SELECT [System.Id], [System.Title], [System.AssignedTo], [System.State]
FROM WorkItemLinks
WHERE ([Source].[System.TeamProject] = #project AND
[Source].[System.WorkItemType] = 'Test Case') And
([System.Links.LinkType] <> '') And ([Target].[System.Id] = #taskId)
ORDER BY [System.Id] mode(MayContain)
Here's the code to run it:
var tpc = new TfsTeamProjectCollection("http://localhost:8080/tfs/DefaultCollection");
var store = tpc.GetService<WorkItemStore>();
var queryContext = new Dictionary<string, string>();
queryContext.Add("project", "<team project name>");
queryContext.Add("taskId", "<task ID you are interested in>");
var query = new Query(store, "<WIQL query text>", queryContext);
var result = query.RunLinkQuery();

Breeze - How to create contains Query

How can I query in breeze a contains query of list if integers?
kind of this?
elemnts.where(p => listofids.contains(p.id));
For now you need to use 'or' clauses
var whereClause = breeze.Predicate.create("id", "==", 3)
.or("id", "==", 5)
.or("id", "==", 6);
var query = breeze.EntityQuery.from("elemnts").where(whereClause);
or ( with your list of ids):
var preds = listOfIds.map(function(id) {
return breeze.Predicate.create("id", "==", id);
})
var whereClause = Predicate.or(preds);
var query = breeze.EntityQuery.from("elemnts").where(whereClause);

breeze.js mixing AND and OR predicates

I'm trying to configure the proper syntax for breeze.js when mixing AND and OR predicates. I did not see an example on the breeze site and could not find one anywhere else.
Basically I want to do something like this in my WHERE clause:
(
age > 30 AND
sex == 'M' AND
jobStartDate >= '1/1/2000'
)
OR
(
exemptStatus == 1
)
this will bring back entities that match the 3 criteria OR are exempt. I'm using EF6 with ODATA syntax.
Thanks
Try this:
var Predicate = breeze.Predicate;
var baseQuery = EntityQuery.from("Something");
var pred1 = new Predicate("age", ">", 30);
var pred2 = new Predicate("sex", "==", 'M');
var pred3 = new Predicate("jobStartDate", ">=", new Date(2000,1,1));
var pred4 = new Predicate("exemptStatus", "==", 1);
var compositePred = pred1.and(pred2).and(pred3).or(pred4);
var query = baseQuery.where(compositePredicate);
myEntityManager.executeQuery(query);
or
var compositePred = Predicate.and(pred1, pred2, pred3).or(pred4);

Merging two Query Results

I have following two Queries on Same Table and need to merge it.
var Prod = this.UnitOfWork.myRepository.GetData();
var PreProd = this.UnitOfWork.myRepository.GetData();
var Merge = Prod.Union(PreProd);
But when i check results of Merge it doesn't show Union but shows following message
Message: This method supports the LINQ to Query Result Union not
working Entities infrastructure and is not intended to be used
directly from your code
How this can be accomplished.
You can use .AsEnumerable() to convert IQueryable to IEnumerable firstly.
var Prod = this.UnitOfWork.myRepository.GetData().AsEnumerable();
var PreProd = this.UnitOfWork.myRepository.GetData().AsEnumerable();
var Merge = Prod.Union(PreProd);
LINQ to EF supported Union:
IQueryable<TSource> Union<TSource>(
this IQueryable<TSource> source1,
IEnumerable<TSource> source2
)
so try:
var Prod = this.UnitOfWork.myRepository.GetData();
var PreProd = this.UnitOfWork.myRepository.GetData();
var Merge = Prod.Union(PreProd.AsEnumerable());

Resources