Search code examples
elasticsearchelasticsearch-pluginelasticsearch-dsl

Elasticsearch query filter combination issue


Im trying to understand why the below elasticsearch query does not work.

EDIT:

The fields mentioned in the query are from different indices. For example Filter has classification field which is in a different index to the fields mentioned in the query string.

The expectation of the filter query is that when the user searches specifically on classification field i.e. secret or protected then the values are displayed. Else if the user searches for any other field from a different index for example firstname or person, then it should not consider any filter applied as firstname or person is not part of the filter

{
    "query": {
        "bool": {
                    "filter": {
                        "terms": {
                            "classification": [
                                "secret",
                                "protected"
                            ]
                        }
            },
            "must": {
                "query_string": {
                    "query": "*john*",
                    "fields": [
                        "classification",
                        "firstname",
                        "releasability",
                        "person"
                    ]
                }
            }
        }
    }
}

The result expected is john in the field person is returned. This works when there is no filter applied in the above code as

{
    "query": {
     
                    

                "query_string": {
                   "query": "*john*",
                    "fields": [
                        "classification",
                        "firstname",
                        "releasability",
                        "person"
                    ]
                }
            
        
    }
}

The purpose of the filter is only to filter records when the said fields contain the values mentioned, otherwise it should work for all values.

Why is it not producing the results for john and only producing results for classification values only?


Solution

  • Adding a working example with sample index data and search query.

    To know more about Bool query refer this official documentation

    Index Data: Index data in my_index index

    {
        "name":"John",
        "title":"b"
    }
    {
        "name":"Johns",
        "title":"a"
    }
    

    Index data in my_index1 index

    {
        "classification":"protected"
    }
    {
        "classification":"secret"
    }
    

    Search Query :

    POST http://localhost:9200/_search

    {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "filter": [
                  {
                    "terms": {
                      "classification": [
                        "secret",
                        "protected"
                      ]
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "query_string": {
                      "query": "*john*",
                      "fields": [
                        "name",
                        "title"
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    Search Result:

     "hits": [
          {
            "_index": "my_index",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.0,
            "_source": {
              "name": "John",
              "title": "b"
            }
          },
          {
            "_index": "my_index",
            "_type": "_doc",
            "_id": "2",
            "_score": 1.0,
            "_source": {
              "name": "Johns",
              "title": "a"
            }
          },
          {
            "_index": "my_index1",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.0,
            "_source": {
              "classification": "secret"
            }
          },
          {
            "_index": "my_index1",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.0,
            "_source": {
              "classification": "protected"
            }
          }
        ]