Search code examples
elasticsearchluceneelasticsearch-queryelasticsearch-nested

Bool Filter not showing just the filtered data in Elastic Search


I have an index "tag_nested" which has data of following type :

{
    "jobid": 1,
    "table_name": "table_A",
    "Tags": [
      {
        "TagType": "WorkType",
        "Tag": "ETL"
      },
      {
        "TagType": "Subject Area",
        "Tag": "Telecom"
      }
    ]
}

When I fire the query to filter data on "Tag" and "TagType" by firing following query :

POST /tag_nested/_search
{
    "query": {
        "bool": {
            "must": {"match_all": {}},
            "filter": [
                {"term": {
                    "Tags.Tag.keyword": "ETL"
                }},
                 {"term": {
                    "Tags.TagType.keyword": "WorkType"
                }}
            ]
        }
    }
}

It gives me the following output. The problem I am facing is while the above query filters documents which doesn't have filtered data BUT it shows all the "Tags" of that document instead of just the filter one

{
        "_index" : "tag_nested",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "jobid" : 1,
          "table_name" : "table_A",
          "Tags" : [
            {
              "TagType" : "WorkType",
              "Tag" : "ETL"
            },
            {
              "TagType" : "Subject Area",
              "Tag" : "Telecom"
            }
          ]
        }
      }

Instead of above result I want my output to be like :

{
        "_index" : "tag_nested",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "jobid" : 1,
          "table_name" : "table_A",
          "Tags" : [
            {
              "TagType" : "WorkType",
              "Tag" : "ETL"
            }
          ]
        }
      }

Solution

  • Already answered here, here and here.

    TL;DR you'll need to make your Tags field of type nested, resync your index & use inner_hits to only fetch the applicable tag group.