如何计算每个字段的Elasticsearch匹配数

时间:2019-06-05 17:13:06

标签: elasticsearch

我需要对多个字段进行搜索,以返回在每个字段中找到搜索的次数。

例如:

GET generalindex/_search
{
  "query": {
    "simple_query_string":{
      "query": "+test"
    }
  },
  "_source": [
    "companyName",
    "description"
  ],
    "highlight": {    
      "fields": {
        "companyName": {},
        "description": {}
    }
  }
}

我希望得到的结果是这样的:

{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "generalindex",
        "_type" : "elsindex",
        "_id" : "1275",
        "_score" : 0.8630463,
        "_source" : {
          "companyName" : "Company Test 4",
          "description" : "This is test  Test 4's test"
        },
        "highlight" : {
          "companyName" : [
            "Company <em>Test</em> 4"
          ],
          "description" : [
            "This is <em>test</em>  <em>Test</em> 4's <em>test</em>"
          ]
        }
        "count": {
              "companyName" : 1,       
              "description" : 3        
         }
      },
      {
        "_index" : "generalindex",
        "_type" : "elsindex",
        "_id" : "1271",
        "_score" : 0.8630463,
        "_source" : {
          "companyName" : "Company Test 2",
          "description" : "This is company Test 2's Test"
        },
        "highlight" : {
          "companyName" : [
            "Company <em>Test</em> 2"
          ],
          "description" : [
            "This is company <em>Test</em> 2's <em>Test</em>"
          ]
        },
        "count": {
              "companyName" : 1,       
              "description" : 2        
         }
      },
     ......
    ]
  }
}

我可以在其中检索在每个字段中找到“测试”一词的次数。

我可以将number_of_fragments设置为0并手动计算Highlights字段返回的 事件,但是我希望找到一个更干净的解决方案(此外,如果文档文本可能会导致误报,已经包含标签)

0 个答案:

没有答案