Complex nested Elastic NEST Query - elasticsearch

How would I convert this to the equivalent Nest query?
{
"query": {
"nested": {
"path": "boundedContexts",
"query": {
"nested": {
"path": "boundedContexts.aggregateRoots",
"query": {
"nested": {
"path": "boundedContexts.aggregateRoots.modelMetaData",
"query": {
"bool": {
"must": [
{ "match": { "boundedContexts.aggregateRoots.modelMetaData.modelReferenceId": "4e7c5c0e-93a7-4bf6-9705-cf1327760e21" } },
{ "match": { "boundedContexts.aggregateRoots.modelMetaData.modelType.name": "AggregateRoot" } }
]
}
}
}
}
}
}
}
},
"size": 1,
"sort": [
{
"generatedDate": {
"order": "desc"
}
}
]
}
I am trying to generate an equivalent Nest query similar to this ..
ISearchResponse<ViewModels.DomainModel> response = null;
response = await _elasticClient.SearchAsync<ViewModels.DomainModel>(s => s.Index(_modelMetadataProvider.CurrentIndexName)
.Query(q => q.Nested(n1 => n1.Path("boundedContexts")
.Query(q2 => q2.Nested(n2 => n2.Path("boundedContexts.aggregateRoots")
.Query(q3 => q3.Nested(n3 => n3.Path("boundedContexts.aggregateRoots.modelMetaData").
Query(q4 =>q4.Bool(b => b.Must(bs =>
bs.Match(p => p.Field("boundedContexts.aggregateRoots.modelMetaData.modelReferenceId")))))))))))
.Size(1).Sort(s=>s.Descending("generatedDate")));
I am stuck on how to compare to a variable against at the p.Field("boundedContexts.aggregateRoots.modelMetaData.modelReferenceId") level of query.

Related

Sql query on elastic 6.8 does not work as expected. Array of nested objects are flattened same as of type object

Thanks for the answer in advance.
I am running a query
SELECT key
FROM records_index
WHERE
(product_nested_field.code = '1234' and product_nested_field.codeset = 'category1' OR product_nested_field.code = '444' and product_nested_field.codeset = 'category1')
AND (role_name IN ('user', 'admin'))
GROUP BY records_uuid
In records_index I have record with two products
[
{codeset: category1, code:444},
{codeset: category2, code:1234}
]
The problem is that query does find a specified record.
such behavior is expected for "type": "object" but why I am getting that result for product_nested_field of type nested?
when I translate SQL to JSON I am getting
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"nested": {
"query": {
"term": {
"product_nested_field.codeset": {
"value": "category1"
}
}
}
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"nested": {
"query": {
"term": {
"product_nested_field.code": {
"value": "1234"
}
}
}
}
},
{
"nested": {
"query": {
"term": {
"product_nested_field.code": {
"value": "444"
}
}
}
}
}
]
}
}
]
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
why elastic moves product_nested_field.codeset = 'category1' into separate nested query.

Filter Creation

So after much experimentation I have found that the syntax I needed to filter products based on their specs is as follows
{
"post_filter": {
"bool": {
"filter": [{
"nested": {
"path": "productSpecification",
"query": {
"bool": {
"filter": [{
"term": {
"productSpecification.name": "Brand"
}
},
{
"terms": {
"productSpecification.value": [
"Brand1"
]
}
}
]
}
}
}
},
{
"nested": {
"path": "productSpecification",
"query": {
"bool": {
"filter": [{
"term": {
"productSpecification.name": "Guarantee"
}
},
{
"terms": {
"productSpecification.value": [
"3 years"
]
}
}
]
}
}
}
}
]
}
}
}
I'm now experimenting with creating a QueryContainer function to build this based on the selected name/values, If anyone can give me a point in the right direction on this that would be much appreciated.
Thanks
Ok I am now building a query from nest that works but is a bit messier than the DSL query above (not sure if someone can spot what Im doing below that makes it have some unnecessary bool filters
Here is my nest syntax
.PostFilter(pf => FilterTest(elasticParams,pf))
private static QueryContainer FilterTest(ElasticParams elasticParams, QueryContainerDescriptor<Product>q) => q
.Bool(b => b
.Filter(fi => FilterSelected(elasticParams, fi)));
private static QueryContainer FilterSelected2(ElasticParams elasticParams, QueryContainerDescriptor<Product> q) =>
elasticParams.Filters.Aggregate(new QueryContainer(), (c, s) => c && +q.Nested(n => n
.Path(p => p.ProductSpecification)
.Query(qq => qq
.Bool(bo => bo
.Filter(fi => fi
.Term(tt => tt
.Field(a => a.ProductSpecification.Suffix("name"))
.Value(s.name)) && fi
.Terms(ttt => ttt
.Field(fff => fff.ProductSpecification.Suffix("value"))
.Terms(s.Values))
)))));
As mentioned before this is filters as I would want however it DSL that it outputs is rather fugly as can be seen below
{
"post_filter": {
"bool": {
"filter": [{
"bool": {
"filter": [{
"nested": {
"query": {
"bool": {
"filter": [{
"bool": {
"must": [{
"term": {
"productSpecification.name": {
"value": "Brand"
}
}
}, {
"terms": {
"productSpecification.value": ["Brand1", "Brand2"]
}
}]
}
}]
}
},
"path": "productSpecification"
}
}, {
"nested": {
"query": {
"bool": {
"filter": [{
"bool": {
"must": [{
"term": {
"productSpecification.name": {
"value": "Guarantee"
}
}
}, {
"terms": {
"productSpecification.value": ["3 years"]
}
}]
}
}]
}
},
"path": "productSpecification"
}
}]
}
}]
}
}
}

how to write existsQuery in nest 1.7

I'm using nest 1.7 and i need to write this query:
GET _search
{
"from": 0,
"size": 3,
"query": {
"bool": {
"must": [
{
"constant_score": {
"filter": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"exists": {
"field": "Collaborateurs"
}
},
{
"exists": {
"field": "Collaborateurs.Nom"
}
},
{
"exists": {
"field": "Collaborateurs.Fonction"
}
},
{
"exists": {
"field": "Collaborateurs.TagVisuel"
}
},
{
"exists": {
"field": "siAnnuaire"
}
},
{
"term": {
"siAnnuaire": {
"value": true
}
}
},
{
"exists": {
"field": "TagPhoto"
}
},
{
"range": {
"NbAnnonce": {
"gt": 0
}
}
},
{
"geo_distance": {
"distance": "10.0km",
"AgenceLocation": {
"lat": 48.8523513700019,
"lon": 2.35127712591128
}
}
}
]
}
}
]
}
}
}
},
{
"function_score": {
"functions": [
{
"random_score": {
"seed": 69937385
}
}
]
}
}
]
}
}
}
Using the fluent API, the method calls follow pretty much the structure of the query DSL json
var response = client.Search<Document>(x => x
.From(0)
.Size(3)
.Query(q => q
.Bool(b => b
.Must(m => m
.ConstantScore(cs => cs
.Filter(csf => csf
.Bool(cb => cb
.Must(
cm => cm.Exists(p => p.Collaborateurs),
cm => cm.Exists(p => p.Collaborateurs.Nom),
cm => cm.Exists(p => p.Collaborateurs.Fonction)
// etc, etc for the other queries
)
)
)
), m => m
.FunctionScore(fs => fs
.Functions(fu => fu
.RandomScore(69937385)
)
)
)
)
)
);
which yields
{
"from": 0,
"size": 3,
"query": {
"bool": {
"must": [
{
"constant_score": {
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "collaborateurs"
}
},
{
"exists": {
"field": "collaborateurs.nom"
}
},
{
"exists": {
"field": "collaborateurs.fonction"
}
}
]
}
}
}
},
{
"function_score": {
"functions": [
{
"random_score": {
"seed": 69937385
}
}
]
}
}
]
}
}
}
field names have been camel-cased by default, but you can change this behaviour using .SetDefaultPropertyNameInferrer(Func<string, string>) on ConnectionSettings.

Convert DSL Query To NEST Query

My DSL query is below :
{
"from": 0,
"size": 200,
"query": {
"filtered": {
"filter": {
"bool": {
"must": {
"query": {
"match": {
"contactId": {
"query": "e84aca88-7b82-43d9-8788-4cc25af0c43a",
"type": "phrase"
}
}
}
}
}
}
}
}
}
I tried to convert dsl query to NEST query.
var descriptor =
new SearchDescriptor<dynamic>().From(0)
.Size(200)
.Query(
a =>
a.Filtered(
b =>
b.Filter(
c =>
c.Bool(
d =>
d.Must(
e =>
e.Query(
f =>
f.Match(
g =>
g.OnField("contactId")
.Query(
"e84aca88-7b82-43d9-8788-4cc25af0c43a"))))))));
string result = System.Text.Encoding.Default.GetString(client.Serializer.Serialize(descriptor));
I examined the json results with the help of SearchDescriptor. There were differences :
{
"from": 0,
"size": 200,
"query":
{
"filtered":
{
"filter": {
"bool":
{
"must": [
{
"fquery":
{
"query":
{
"match":
{
"contactId":
{
"query": "e84aca88-7b82-43d9-8788-4cc25af0c43a"
}
}
}
}
}]
}
}
}
}
}
As you can see fquery added but i didnt fquery my Nest query. Fquery why added? First DSL query is returning result but second DSL query is not returning any result
My updated code :
var searchResults =
client.Search<dynamic>(u=>u.From(0)
.Size(200)
.Query(
a =>
a.Filtered(
b =>
b.Filter(
c =>
c.Bool(
d =>
d.Must(
e =>
e.Query(
f =>
f.MatchPhrase(
g =>
g.OnField("contactId")
.Query(
"e84aca88-7b82-43d9-8788-4cc25af0c43a")))))))));

in NEST, how do I dynamically build a query from a list of terms?

Say my user provides a list of search terms which I've collected into an array/list, and now I want to combine those OR-wise into a NEST query using MatchPhrase. How would I do that? The code for a (single) search term would look something like this:
var search = client.Search<ElasticRequirement>(s => s
.Query(q =>
q.MatchPhrase(m => m.OnField(f => f.Title).Query(term.ToLower()).Slop(slop))
|| q.MatchPhrase(m => m.OnField(f => f.Description).Query(text).Slop(slop))
)
.LowercaseExpandedTerms()
.Explain()
.Query(q => q.Fuzzy(f => f.PrefixLength(1).OnField(c => c.Title).OnField(c => c.Description)))
);
This is fine, but I need to apply that same MatchPhrase filter once for each provided search term. Any help much appreciated.
You can use bool should expressions to build your query dynamically. I'll provide the complete solution below. Call BuildQuery() method with appropriate parameters.
ISearchResponse<ElasticRequirement> BuildQuery(IElasticClient client, IEnumerable<string> terms, int slop)
{
return client.Search<ElasticRequirement>(s => s
.Query(q => q
.Bool(b => b
.Should(terms.Select(t => BuildPhraseQueryContainer(q, t, slop)).ToArray())))
.LowercaseExpandedTerms()
.Explain()
.Query(q => q.Fuzzy(f => f.PrefixLength(1).OnField(c => c.Title).OnField(c => c.Description))));
}
QueryContainer BuildPhraseQueryContainer(QueryDescriptor<ElasticRequirement> qd, string term, int slop)
{
return qd.MatchPhrase(m => m.OnField(f => f.Title).Query(term.ToLower()).Slop(slop)) ||
qd.MatchPhrase(m => m.OnField(f => f.Description).Query(term.ToLower()).Slop(slop));
}
For terms = {"term1", "term2", "term3"} and slop = 0, the Elasticsearch search JSON command that will get built by my code is as under:
{
"explain": true,
"query": {
"bool": {
"should": [
{
"bool": {
"should": [
{
"match": {
"title": {
"type": "phrase",
"query": "term1",
"slop": 0
}
}
},
{
"match": {
"description": {
"type": "phrase",
"query": "term1",
"slop": 0
}
}
}
]
}
},
{
"bool": {
"should": [
{
"match": {
"title": {
"type": "phrase",
"query": "term2",
"slop": 0
}
}
},
{
"match": {
"description": {
"type": "phrase",
"query": "term2",
"slop": 0
}
}
}
]
}
},
{
"bool": {
"should": [
{
"match": {
"title": {
"type": "phrase",
"query": "term3",
"slop": 0
}
}
},
{
"match": {
"description": {
"type": "phrase",
"query": "term3",
"slop": 0
}
}
}
]
}
}
]
}
}
}
You can tweak this code such that all the match commands are under the same should node. I'll leave that up to you to figure out :)

Resources