-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
fixes invalid query nesting generation #163
Conversation
Nope doesn't seem to work. /// this works
bodyBuilder().query('has_parent', 'parent_type', 'blog', (q) => {
return q.query('term', 'tag', 'something') |
849ff11
to
400c71c
Compare
hi guys, this still is a problem in current release 2.2.7 |
@richardwhatever Can you create an issue with the problem ? That statement doesn't really help us diagnose the issue |
Sure, when i try and run code like this:
BodyBuilder errors saying "TypeError: n.query is not a function" but I can't add a query under a filter. Elasticsearch needs it to be a query there, not filter. |
Create a separate issue for this |
i.e The problem also lies with your query. You are nesting a query inside a filter ... Have you tried ? bb.aggregation('filter', fieldName, aggName, (a) => {
return a.filter('nested', {path: 'skus'}, (n) => {
return n.filter('terms', { 'material': 'wood' })
})
.aggregation('terms', fieldName, aggName)
}) |
I can't seem to get nested queries to work For example, I want:
searchQuery = 'george'
filterKey = 'gender'
filterValue = 'male'
bodybuilder()
.query('bool', b => b
.orQuery('match', 'name', searchQuery)
.orQuery('match', 'email', searchQuery))
.filter('term', filterKey, filterValue)
.build() This results in: {
query: {
bool: {
filter: {
term: { gender: "male" }
},
query: { // <-- I want this to be "must"
bool: {
should: [
{ match: { name: "george" } },
{ match: { email: "george" } }
]
}
}
}
}
} and the corresponding Elasticsearch error:
What is the correct syntax to nest queries as described above? |
Just don't nest it inside the bool query and you should be fine. (Structure
will be slightly different to what you asked for, but results will be the
same)
…On Fri, 27 Apr 2018, 23:59 Elías, ***@***.***> wrote:
I can't seem to get nested queries to work
For example, I want:
- Match "george" in the name field *OR* the email field.
- Filter for gender=male.
searchQuery = 'george'
filterKey = 'gender'
filterValue = 'male'
bodybuilder()
.query('bool', b => b
.orQuery('match', 'name', searchQuery)
.orQuery('match', 'email', searchQuery))
.filter('term', filterKey, filterValue)
.build()
This results in:
{
query: {
bool: {
filter: {
term: { gender: "male" }
},
query: { // <-- I want this to be "must"
bool: {
should: [
{ match: { name: "george" } },
{ match: { email: "george" } }
]
}
}
}
}
}
and the corresponding Elasticsearch error:
Error: [parsing_exception] [bool] query does not support [query]
What is the correct syntax to nest queries as described above?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#163 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFEH-v4_1jy6307kHImMa4pguKJClaQcks5ts5S4gaJpZM4Rwa-5>
.
|
@eliath not sure what version you are running, but when I run it with the latest. Use http://bodybuilder.js.org/ to test you queries against the latest build. searchQuery = 'george'
filterKey = 'gender'
filterValue = 'male'
bodybuilder()
.query('bool', b => b
.orQuery('match', 'name', searchQuery)
.orQuery('match', 'email', searchQuery))
.filter('term', filterKey, filterValue)
.build() ==> {
"query": {
"bool": {
"filter": {
"term": {
"gender": "male"
}
},
"should": [
{
"match": {
"name": "george"
}
},
{
"match": {
"email": "george"
}
}
]
}
}
} And if you want a must you can't use an orQuery Should be => searchQuery = 'george'
filterKey = 'gender'
filterValue = 'male'
bodybuilder()
.query('bool', b => b
.query('match', 'name', searchQuery)
.query('match', 'email', searchQuery))
.filter('term', filterKey, filterValue)
.build() => {
"query": {
"bool": {
"filter": {
"term": {
"gender": "male"
}
},
"must": [
{
"match": {
"name": "george"
}
},
{
"match": {
"email": "george"
}
}
]
}
}
} |
Thanks for your help. I tried your suggestion, it makes sense but doesn't solve my problem because of the ElasticSearch behavior. bodybuilder()
.orQuery('match', 'name', 'george')
.orQuery('match', 'email', 'george')
.filter('term', 'gender', 'male')
.build() Resulting body: {
query: {
bool: {
filter: {
term: { gender: "male" }
},
should: [
{ match: { name: "george" } },
{ match: { email: "george" } }
]
}
}
} Looks good, but because there is a From ElasticSearch docs on
So the result of using this body to search is the union of:
What I want is the intersection of the above two criteria. Any ideas? Otherwise I will specify the clause manually as indicated in this comment |
have u tried adding a minimum_should_match ? bodybuilder()
.orQuery('match', 'name', 'george')
.orQuery('match', 'email', 'george')
.queryMinimumShouldMatch(1)
.filter('term', 'gender', 'male')
.build() this would give u the intersection of gender+name OR gender+email OR gender+name+email |
Or seeing as you don't seem to care about the scoring |
|
bodybuilder now generates valid nested query
fixes #142
fixes #162