Better to use "must" and "terms", or "should", in a bool query?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Better to use "must" and "terms", or "should", in a bool query?

Nick Hoffman
Hi guys. These 2 queries produce the same results via different approaches. Is there any reason why one should be used over another?

Thanks,
Nick

{
"size": 10,
"fields": ["id", "name"],
"sort": [ { "name.orig": "asc" } ],
"query": {
"bool": {
"must": [
{
"terms": {
"catalog_ids": ["4dddd736349c302bd100004e", "511a70be63051f6a64000217"],
"minimum_should_match": 1
}
}
]
}
}
}

{
"size": 10,
"fields": ["id", "name"],
"sort": [ { "name.orig": "asc" } ],
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{ "term": { "catalog_ids": "4dddd736349c302bd100004e" } },
{ "term": { "catalog_ids": "511a70be63051f6a64000217" } }
]
}
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/5df1fbf3-9c3a-4f1d-aef0-f7cbec83913d%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Better to use "must" and "terms", or "should", in a bool query?

kazoompa-3
You will only see the difference when you have more complex queries that combine MUST and SHOULD together. In your example you could have just change MUST to SHOULD (i.e. used TERMS). By the definition, SHOULD is like an OR and without a MUST clause it implies 'at least' one should match.

R

On Friday, January 10, 2014 11:17:57 AM UTC-5, Nick Hoffman wrote:
Hi guys. These 2 queries produce the same results via different approaches. Is there any reason why one should be used over another?

Thanks,
Nick

{
"size": 10,
"fields": ["id", "name"],
"sort": [ { "name.orig": "asc" } ],
"query": {
"bool": {
"must": [
{
"terms": {
"catalog_ids": ["4dddd736349c302bd100004e", "511a70be63051f6a64000217"],
"minimum_should_match": 1
}
}
]
}
}
}

{
"size": 10,
"fields": ["id", "name"],
"sort": [ { "name.orig": "asc" } ],
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{ "term": { "catalog_ids": "4dddd736349c302bd100004e" } },
{ "term": { "catalog_ids": "511a70be63051f6a64000217" } }
]
}
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/c8973ae2-2b7c-44d1-99f3-5c587acbdc51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.