-
-
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
should nested within must #51
Comments
Nested queries haven't been developed yet, but I think it is possible to add this. #16 is a good example of how this was done for nesting aggregations and I think a similar approach could work here. |
I may have misinterpreted the question but is the question about supporting nested bool structure rather than a nested query accessing a nested object? By the way -- really love this project. |
is there a way to build has_child with "or" in bodybuilder { |
@gitemconte I got into the same issue... and yes, I guess we should create a PR for this soon... but for now, as a workaround, I am manually doing it by using the So, you could do something like this:
I know is not nice... but as a workaround, for now, it let me do the query I needed... NOTE: There is a little edge case in the case of not having a must clause yet... so you can easily add the must close by adding a couple of
Again, I know is not a nice approach but it will do the its job for now... |
I have been working on a library for building elasticsearch DSL heavily inspired by elastic.js which unfortunately was abandoned. The requirement, const bob = require('elastic-builder');
const boolQry = bob
.boolQuery()
.must(bob.matchQuery('status', 'active'))
.must(
bob.boolQuery()
.should(bob.matchQuery('city', 'New York'))
.should(bob.matchQuery('city', 'Toronto'))
.minimumShouldMatch(1)
)
.toJSON(); Link: http://github.com/sudo-suhas/elastic-builder Hope this helps 😄 |
I tried on a similar requirement: works fine.. on ElasticSearch 2.4 and 5.2
|
@gitemconte I made a approach similar to @diegoprd one, I needed something like this in my filters:
To get this result dinamically with bodybuilder I did:
Obviously this is not a good solution but it works, and I hope it helps someone, if you have any question just ask. I started using bodybuilder and my project needs many nested queries, @danpaz we can create a PR for this? How can we help? |
I'd be happy to look at a PR to address this as I see it's caused confusion for folks. I just haven't had time myself to dig into it, and as @gophry points out sometimes the additional nesting isn't exactly needed. Your query may be different, though, not sure. In any case there seems to be a need for this feature. Btw you may be able to use the undocumented getFilter method to improve your workaround. Take a look at the source code for that, let me know if you have any questions about it. |
it works in this way:
|
@dongyunqi2018 I am facing a similar issue your solution is not working for me, |
bodybuilder()
.query('term', 'is_del', 1)
.query('bool', b =>
b.orQuery('term', 'monitor_url.keyword', 'http://tx-g.xxxxxx.com/d/INzdsdYnu?idfa=__IDFA__&os=__OS__×tamp=__TS__&callback=__CALLBACK_URL__')
.orQuery('term', 'download_url.keyword', 'https://count.xxxxxxx.com/d.php?id=738896&urlos=android&from_type=web&time=1733'))
.size(1)
.sort([{
"created_at": "desc"
}])
.build() |
I am using this with filter query like this ` bodybuilder()
Is this making a valid Elastic search query if yes should i delete my Index and recreate it ? |
Query looks fine, what's the issue you're having ? Also what's the mapping for the index ? |
I should be getting results for where com_ref ="some value" and (fr_id = 1 or fr_id =2) right? |
try removing the bodybuilder()
.orFilter('term', 'fr_id ', 511168)
.orFilter('term', 'fr_id ', 511258).build() What do you get ? Also what's in |
Yes |
The only way there wouldn't be any results, if no data exist with those |
Attaching a file of the dump with data only for fr_id and com_ref |
I just realised, there is an empty string at the end bodybuilder()
.orFilter('term', 'fr_id', 511168)
.orFilter('term', 'fr_id', 511258).build() The above worked for me results : {
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [{
"_index": "client_basic_detail",
"_type": "_doc",
"_id": "4efffaf1-ffed-4ac0-ad59-a60914312dbf",
"_score": 0,
"_source": {
"com_ref": "de3e75da-8ac6-40bc-8586-f4b2e1e01383",
"fr_id": 511168
}
},
{
"_index": "client_basic_detail",
"_type": "_doc",
"_id": "bbe5b6c3-8ba8-40ea-9569-2852b2539a80",
"_score": 0,
"_source": {
"com_ref": "de3e75da-8ac6-40bc-8586-f4b2e1e01383",
"fr_id": 511258
}
}
]
}
} |
And then for the above query bodybuilder()
.filter('term', 'com_ref.keyword', "de3e75da-8ac6-40bc-8586-f4b2e1e01383")
.filter('bool', b => b
.orFilter('term', 'fr_id', 511168)
.orFilter('term', 'fr_id', 511258)).build() ==> {
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"term": {
"com_ref.keyword": "de3e75da-8ac6-40bc-8586-f4b2e1e01383"
}
},
{
"bool": {
"should": [
{
"term": {
"fr_id": 511168
}
},
{
"term": {
"fr_id": 511258
}
}
]
}
}
]
}
}
}
}
} ==> {
"took": 24,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [{
"_index": "client_basic_detail",
"_type": "_doc",
"_id": "4efffaf1-ffed-4ac0-ad59-a60914312dbf",
"_score": 0,
"_source": {
"com_ref": "de3e75da-8ac6-40bc-8586-f4b2e1e01383",
"fr_id": 511168
}
},
{
"_index": "client_basic_detail",
"_type": "_doc",
"_id": "bbe5b6c3-8ba8-40ea-9569-2852b2539a80",
"_score": 0,
"_source": {
"com_ref": "de3e75da-8ac6-40bc-8586-f4b2e1e01383",
"fr_id": 511258
}
}
]
}
} |
Oh Man i feel so stupid right now . i was pulling my hairs for this . |
@KillerAyce no worries, well I overlooked too. Glad we figured out, all the best ! |
I am curious if it is possible with this module to set up a complex elastic query that has should queries nested within a must.
Simply, I am trying to recreate this SQL query:
SELECT * FROM test where status = 'active' and (city = 'New York' or city = 'Toronto')
I would like to get back something similar to the below, but I have only been able to have the MUST and the SHOULD on the same level. Is this functionality possible? It seems like a simple query case, but maybe I am wrong.
The text was updated successfully, but these errors were encountered: