1
votes

Here is my ES Queries:

=== Create the index ===

PUT /sample

=== Insert Data ===

PUT /sample/docs/1
{"data": "And the world said, 'Disarm, disclose, or face serious consequences'—and therefore, we worked with the world, we worked to make sure that Saddam Hussein heard the message of the world."}
PUT /sample/docs/2
{"data": "Never give in — never, never, never, never, in nothing great or small, large or petty, never give in except to convictions of honour and good sense. Never yield to force; never yield to the apparently overwhelming might of the enemy"}

=== Query to Get Results ===

POST sample/docs/_search
{
  "query": {
    "match": {
      "data": "never"
    }
  },
  "highlight": {
    "fields": {
      "data":{}
    }
  }
}

=== Retrieved Result ===

...
        "highlight": {
          "data": [
            "<em>Never</em> give in — <em>never</em>, <em>never</em>, <em>never</em>, <em>never</em>, in nothing great or small, large or petty, <em>never</em> give",
            " in except to convictions of honour and good sense. <em>Never</em> yield to force; <em>never</em> yield to the apparently overwhelming might of the enemy"
          ]
        }

=== Desired Result===

Required Term Frequency of searched term by document as example below

Doc Id: 2
Term Frequency :{
    "never": 8
}

I have tried Bucket Aggregation, Terms Aggregation and other Aggregations but I'm not getting this result.

Thanks for help in advance!

1

1 Answers

0
votes

You should use the Term Vector, which supports querying a particular term based on the frequency.

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html

In such case, your query would be

GET /sample/docs/_termvectors
{
    "doc": {
      "data": "never"
    },
    "term_statistics" : true,
    "field_statistics" : true,
    "positions": false,
    "offsets": false,
    "filter" : {
      "min_term_freq" : 8
    }
}