基于弹性搜索中其他字段值的文档中的特定单词计数

时间:2019-02-06 07:19:01

标签: elasticsearch

我想使用弹性搜索(6.4)在下面的对象中将parentId为1的单词计数示例“ Hello”

{
    "id" : "7",
    "parentId":"1",
    "transcription" : "hello hello hello hello hello hello hi  search 5555"
}

1 个答案:

答案 0 :(得分:0)

使用_termvectors

使用termvectors,您可以在字段级别获得统计信息。

URL :索引名/ _doc / docid / _termvectors?fields = transcription

输出

{
  "terms": {
    "5555": {
      "term_freq": 1,
      "tokens": [
        {
          "position": 8,
          "start_offset": 48,
          "end_offset": 52
        }
      ]
    },
    "hello": {
    "term_freq": 6,             <----- This is your word count.
      "tokens": [
        {
          "position": 0,
          "start_offset": 1,
          "end_offset": 6
        },
        {
          "position": 1,
          "start_offset": 7,
          "end_offset": 12
        },
        {
          "position": 2,
          "start_offset": 13,
          "end_offset": 18
        },
        {
          "position": 3,
          "start_offset": 19,
          "end_offset": 24
        },
        {
          "position": 4,
          "start_offset": 25,
          "end_offset": 30
        },
        {
          "position": 5,
          "start_offset": 31,
          "end_offset": 36
        }
      ]
    },
    "hi": {
      "term_freq": 1,
      "tokens": [
        {
          "position": 6,
          "start_offset": 37,
          "end_offset": 39
        }
      ]
    },
    "search": {
      "term_freq": 1,
      "tokens": [
        {
          "position": 7,
          "start_offset": 41,
          "end_offset": 47
        }
      ]
    }
  }
}