【问题标题】:Get Values of Unique Sentences - ElasticSearch获取唯一句子的值 - ElasticSearch
【发布时间】:2016-05-22 19:10:26
【问题描述】:

我的索引文档中有一个字段是一个句子。我打算在索引中的所有文档中找到唯一句子的值。该字段是一个“字符串”字段并被分析。我已经尝试过 cardinality 聚合,但它给了我唯一句子的计数,但不是实际的唯一值。我该如何解决这个问题?

这是我的搜索查询

{
   "fields":[
      "incident.name"
   ],
   "aggs":{
      "unique_vuls":{
         "cardinality":{
            "field":"incident.name"
         }
      }
   }
}

【问题讨论】:

  • 你需要 terms 聚合。
  • @AndreiStefan - 我尝试了术语聚合,但它给了我一个唯一单词列表,而不是构成 incident.name 字段的整个句子。
  • 是的,因为您的“姓名”字段需要使用关键字分析器进行 not_analyzed 或分析。或者将字段转换为不分析其中一个子字段的多字段。
  • @AndreiStefan - 这行得通。谢谢你。我现在发布答案以供他人受益。

标签: elasticsearch


【解决方案1】:

更新和回答:根据@AndreiStefan 的建议,我将该字段重新映射为multi-field 并重新索引数据。随后,我使用incident.name.raw字段进行查询,并能够获得索引中的所有唯一句子。

这是映射的sn-p:

{
   "name":{                          #incident.name field
      "type":"string",
      "index":"analyzed",
      "fields":{
         "raw":{
            "type":"string",
            "index":"not_analyzed"
         }
      }
   }
}

这是带有terms聚合的搜索查询的sn-p:

{
   "aggs":{
      "unique_incidents":{
         "terms":{
            "field":"incident.name.raw"
         }
      }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2015-12-11
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多