简体   繁体   中英

How to highlight frequency of keywords in ElasticSearch?

Let's say, I am searching three phrases "Microsoft", "Facebook", "Google".

How can I make ES return the frequency of each terms in returned results?

Thank you!

You are probably looking for terms aggregations:

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

Essentially, it will bucket the results by what terms it finds

I think the explain API may help you. If you run the following query for instance:

GET /your_index/your_type/_search
{   
    "explain": true, 
    "query" : {
        "match": {
           "company" : "Google"
        }
    }
}

A result may be:

{
   "took": 9,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
       "total": 1,
       "max_score": 11.7377,
       "hits": [
       {
            "_shard": 1,
            "_node": "n0eQxWrIIPYPlmcXA",
            "_index": "your_index",
            "_type": "your_type",
            "_id": "76991",
            "_score": 11.7377,
            "_source": {
            "company": "Google",
               "price": "2008"
            },
            "_explanation": {
                "value": 11.7377,
                "description": "weight(id:76991 in 6552) [PerFieldSimilarity], result of:",
             "details": [
              {
                 "value": 11.7377,
                 "description": "fieldWeight in 6552, product of:",
                 "details": [
                    {
                       "value": 1,
                       "description": "tf(freq=1.0), with freq of:",
                       "details": [
                          {
                             "value": 1,
                             "description": "termFreq=1.0"
                          }
                       ]
                    },
                    {
                       "value": 11.7377,
                       "description": "idf(docFreq=2, maxDocs=138180)"
                    },
                    {
                       "value": 1,
                       "description": "fieldNorm(doc=6552)"
                    }
                 ]
              }
           ]
        }
     }
   ]
 }
}

Under the _explanation section you may see the document frequencies:

tf - how many times the term repeats in the specific scored document

idf - how many times the term repeats along all documents (what you are looking for, I guess)

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM