2016-05-20 46 views
0

我的索引文檔中有一個字段是一個句子。我打算在索引中的所有文檔中找到唯一句子的值。該字段是一個「字符串」字段並進行分析。我嘗試了cardinality聚合,但它給了我一些獨特的句子,但不是實際的唯一值。我該如何解決這個問題?獲取唯一句子的值 - ElasticSearch

這是我的搜索查詢

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

你需要[條款](https://www.elastic.co /guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)。 –

+0

@AndreiStefan - 我嘗試了術語聚合,但是它給了我一個唯一的單詞列表,而不是組成'incident.name'字段的整個句子。 –

+1

是的,因爲您的'姓名'字段需要不通過關鍵字分析器分析或分析。或者將該字段轉換爲其中一個子字段未被分析的多字段。 –

回答

1

更新&答:每@ AndreiStefan的建議下,我重新映射的字段作爲multi-field並重新建立索引的數據。隨後,我使用incident.name.raw字段查詢,並能夠獲得索引中的所有唯一句子。

這裏是映射的片段:

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

這裏的搜索查詢與terms聚集片段:

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