Strange result where a filter query contains a should clause


(jiangguoqiang) #1

Hi.
When learning DSL, I have the following scene.

First, my mapping like this:

PUT /gb
{
"mappings": {
"tweet" : {
"properties" : {
"tweet" : {
"type" : "string",
"analyzer": "english"
},
"date" : {
"type" : "date"
}
}
}
}
}

Then, I put three documents:

PUT /gb/tweet/1
{
"tweet": "my first tweet",
"date": "2016-01-01"
}
PUT /gb/tweet/2
{
"tweet": "my second tweet",
"date": "2016-01-02"
}
PUT /gb/tweet/3
{
"tweet": "my third tweet",
"date": "2016-01-03"
}

Finally, I use the following two querys to search my data:

GET /gb/tweet/_search
{
"query": {
"bool": {
"must": {"match": {"tweet": "my tweet"}},
"should": {"match": {"tweet": "first"}}
}
}
}

GET /gb/tweet/_search
{
"query": {
"bool": {
"filter": {
"bool": {
"must": {"match": {"tweet": "my tweet"}},
"should": {"match": {"tweet": "first"}}
}
}
}
}
}
only return the first document

The first query returns all three documents, while the second query only returns the first query.
what's the difference between the two query?


(Alexander Reelsen) #2

Hey,

see the bool filter docs, stating

If this query is used in a filter context and it has should clauses then at least one should clause is required to match.

So, in the last example, should is actually becoming a must clause (because there is only a single clause).

Hope this makes sense!

--Alex


(jiangguoqiang) #3

Thanks!


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.