Unexpected result with nested boolean query

Hi,

I created a new index

curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
    "mappings": {
        "my_type" : {
            "properties" : {
                "article" : {
                    "type" : "nested"
                }
            }
        }
    }
}
'

with this data

curl -X PUT \
  http://localhost:9200/my_index/my_type/1 \
  -d '{
  "article" : [
    {
      "status" : "approved",
      "text1" :  "window",
      "text2" : "glas"
    },
    {
      "status" : "pending",
      "text1" :  "door",
      "text2" : "wood"
    }
  ]
}'

Know I query the index with a filter on status and a query term which should be either found in text1 or text2. If I run this query:

curl -X POST \
  http://localhost:9200/my_index/_search \
  -d '{
  "query": {
    "nested": {
      "path": "article",
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "article.status": "pending"
                    }
                  }
                ]
              }
            }
          ],
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "article.text1": "window"
                    }
                  },
                  {
                    "match": {
                      "article.text2": "window"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}'

The document is not found as there is no article which has status pending or contains text window. But when I execute this query

curl -X POST \
  http://localhost:9200/my_index/_search \
  -d '{
  "query": {
    "nested": {
      "path": "article",
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "article.status": "pending"
                    }
                  }
                ]
              }
            }
          ],
          "should": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "article.text1": "window"
                    }
                  },
                  {
                    "match": {
                      "article.text2": "window"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}'

The document is found, but I don't know why. My expectation is, that first every article is filtered by the status first. Because the filter status is pending, only the second article is queried, and because the second does not contain the word window, nothing should be found, and it is not important if query is should or must.

Can somebody explain why there is a difference.

Thx,
Martin

Found solution by myself. Even if the should part is false, results are returned if filter is true.

1 Like

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