-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot build valid 'nested' queries in filter context #183
Comments
Thanks for letting us know! I think it's quite logical that you can't use As for the bad request with the nested One workaround in the mean time (and I realise this is not optimal) would be: bodybuilder().orFilter('nested', {
path: 'my_path',
...bodybuilder().query('term', 'my_path.my_field', 'my_value').build()
})
I've read this in a couple of issues here, but haven't seen any benchmarks. Have you tested the performance difference? |
Thanks for the prompt response!
That's correct for most queries, but not quite true for For example, this is a valid query, where the nested query is in the filter context of the top-level query: GET my_index/_search
{
"query": {
"bool": {
"filter": {
"nested": {
"path": "my_path",
"query": {
"term": {
"my_path.my_field": "my_value"
}
}
}
}
}
}
}
This works great, thank you!
I have not tested this, but I run this query in context of other conditionals, so I would have to move all of my conditionals to the query context, which is not something I will do. One of the biggest performance differences is that Elasticsearch caches frequently used filters whereas queries generate a score and are more expensive to compute. |
I have been trying to follow this issue (as I have a similar one) and I cannot get
to run in the repl at https://bodybuilder.js.org/ is that expected or is it something we can fix? |
@AncientSwordRage, you must enter .build() at the end bodybuilder().orFilter('nested', {
path: 'my_path',
...bodybuilder().query('term', 'my_path.my_field', 'my_value').build()
}).build() |
I solved the problem using: bodybuilder()
.filter('term', 'agency_id', '115')
.filter('nested', {
path: 'brokers',
...bodybuilder()
.query('term', 'brokers.broker_customer_service_type_id', 2)
.query('term', 'brokers.broker_id', 167415)
.build()
})
.filter('range', 'profiles.beds', {gte: 2})
.orFilter('term', 'profiles.rent', true)
.build() |
@johannes-scharlach's solve worked for me. If there's no plans on adding nested filter functionality in the near future, this should probably be documented as a workaround :~) |
From what I can tell, it's not possible to perform a nested query inside of a filter context with BodyBuilder:
This doesn't work because
b.query
does not exist, and instead only various filter functions are available. I believe this has to do with being in the "filter context" because of the outerorFilter()
.If I try to use
filter
, however:This results in a
400 Bad Request
from Elasticsearch because filters inside of a nested field is not supported. I am running against Elasticsearch v6.2.If I use an
orQuery
at the top-level this works fine:However this doesn't work for me because I want my boolean condition to live in the filter context with my other conditions and it's cheaper to perform.
The text was updated successfully, but these errors were encountered: