Hello everyone,
I'm doing a request using should
and filter
clauses like this
GET case/_search
{
"_source": false,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "france",
"fields": [
"translations.en.title",
"translations.en.title2"
]
}
},
{
"nested": {
"path": "nested_articles",
"query": {
"multi_match": {
"query": "france",
"fields": [
"nested_articles.level_title",
"nested_articles.article_title.",
]
}
}
}
}
],
"filter": {
"range": {
"adoption_date": {
"lte": "2013-03-21T23:00:00.000Z"
}
}
}
}
}
}
After doing some research, i found out that if i put filter
clause with should
clause like above, should
clause became optional and elasticsearch returns the result matched the filter
clause, regardless should
clause
That is when i came up with two solutions.
The first one is putting should
clause inside a must
clause, and then combine this must
clause with filter
clause (like bellow)
GET case/_search
{
"query": {
"bool": {
"must": {
"bool": {
"should": [
{
"multi_match": {
"query": "france",
"fields": [
"translations.en.title",
"translations.en.title2"
]
}
},
{
"nested": {
"path": "nested_articles",
"query": {
"multi_match": {
"query": "france",
"fields": [
"nested_articles.level_title.en",
"nested_articles.article_title.en",
"nested_articles.content.en"
]
}
}
}
}
]
}
},
"filter": {
"range": {
"adoption_date": {
"lte": "2020-03-21T23:00:00.000Z"
}
}
}
}
}
}
The secoud one is using minimum_should_match
, in this case is 1 (like bellow)
GET case/_search
{
"query": {
"bool": {
"must": {
"bool": {
"should": [
{
"multi_match": {
"query": "france",
"fields": [
"translations.en.title",
"translations.en.title2"
]
}
},
{
"nested": {
"path": "nested_articles",
"query": {
"multi_match": {
"query": "france",
"fields": [
"nested_articles.level_title.en",
"nested_articles.article_title.en",
"nested_articles.content.en"
]
}
}
}
}
]
}
},
"filter": {
"range": {
"adoption_date": {
"lte": "2020-03-21T23:00:00.000Z"
}
}
},
"minimum_should_match": 1
}
}
}
The number of results was returned is differents. The first solution has 124 hits and the second one has 122 hits.
What is the difference between these two query ?
Are there any better solution than these two solution above ?
Thank you in advance for your answer.