Home > Blockchain >  ElasticSearch result get HITS count based on each word in the searchquery
ElasticSearch result get HITS count based on each word in the searchquery

Time:08-04

As we can see in the below simple ElasticSearch for text "Baby Oil" I get HIT-TOTAL of 8616. Is there a way I can get the HITs in details like below . Technically I want permutation of hit count for all words in search.

HIT TOTAL of 8618

HIT BABY of 5000

HIT OIL of 3618

HIT BABY AND OIL of 4500

enter image description here

Update

After using the "fielddata": true , we get the count for all the keywords, instead of keywords used in the search

enter image description here

CodePudding user response:

You can use Filter aggregation

A multi-bucket aggregation where each bucket contains the documents that match a query.

Query

{
  "query": {
    "query_string": {
      "default_field": "rawtext",
      "query": "baby oil"
    }
  },
  "aggs": {
    "count": {
      "filters": {
        "filters": {
          "baby": {
            "match": {
              "rawtext": "baby"
            }
          },
          "oil": {
            "match": {
              "rawtext": "oil"
            }
          },
          "baby oil": {
            "match": {
              "rawtext":{
                "query": "baby oil",
                "operator": "and"
              }
            }
          }
        }
      }
    }
  }
}

Result

"aggregations" : {
    "count" : {
      "buckets" : {
        "baby" : {
          "doc_count" : 2
        },
        "baby oil" : {
          "doc_count" : 1
        },
        "oil" : {
          "doc_count" : 3
        }
      }
    }
  }
  • Related