Search code examples
elasticsearchkibanaelastic-stackelk

Elastic search terms_enum with index_filter not returning expected result


We have an index(newblog) in Elastic search which contains these records:

    {
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "newblog",
        "_id": "arer2121",
        "_score": 1,
        "_source": {
          "id": "arer2121",
          "title": "Python 101 Elasticsearch",
          "author": "John Doe",
          "rating": 3,
          "time": 1662012000000,
          "keywords": [
            "java",
            "python"
          ]
        }
      },
      {
        "_index": "newblog",
        "_id": "spb111",
        "_score": 1,
        "_source": {
          "id": "spb111",
          "title": "Spring boot 101 tuto",
          "author": "Jeff Green",
          "rating": 2,
          "time": 1614585600000,
          "keywords": [
            "java",
            "python",
            "spring boot"
          ]
        }
      },
      {
        "_index": "newblog",
        "_id": "gjv12121",
        "_score": 1,
        "_source": {
          "id": "gjv12121",
          "title": "Java 101 tuto",
          "author": "Anthony Davis",
          "rating": 1,
          "time": 1577869200000,
          "keywords": [
            "java"
          ]
        }
      }
    ]
  }
}

We are trying to fetch specific record from the index using this query:

 GET newblog/_terms_enum
{
  "field":"keywords.keyword",
  "string":"",
  "case_insensitive": true,
  "index_filter":
  {
    "match": {
      "title.keyword": {
        "query": "Spring boot"
      }
    }
  }
}

With this query, we expect only 'Spring boot 101 tuto' to be displayed, but when we run this, it is fetching all the three records. If we dont use index_filter, then the query is giving only specific records, but our use case is to first look at all the keywords - java, python, spring and then filter Spring boot from it.

We are using Elastic 8.3.2.


Solution

  • Unfortunately, index_filter with terms_enum doesn't give the expected result, however we can get the result with search and aggregation.

    GET blog/_search
    {
      "query": {
        "match_phrase": {
          "title": "Spring boot"
        }
      }, 
      "size":0,
      "aggs": {
        "keywords": {
          "terms": { "field": "keywords" }
        }
      }
    }
    

    Reference link: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html