![](/img/trans.png)
[英]Elasticsearch - How to get the list of documents with Unique combination of two fields?
[英]Elasticsearch - How to get popular words list of documents
我有一個包含需要審核的文檔的臨時索引。 我想按它們包含的單詞對這些文檔進行分組。
例如,我有這些文件:
1 - “aaa bbb ccc ddd eee fff”
2 - “bbb mmm aaa fff xxx”
3 - “hhh aaa fff”
所以,我想得到最流行的詞,最好是計數:“aaa” - 3,“fff” - 3,“bbb” - 2,等等。
這可以通過彈性搜索實現嗎?
做一個簡單的術語聚合搜索將滿足您的需求:
(其中mydata
是您的字段名稱)
curl -XGET 'http://localhost:9200/test/data/_search?search_type=count&pretty' -d '{
"query": {
"match_all" : {}
},
"aggs" : {
"mydata_agg" : {
"terms": {"field" : "mydata"}
}
}
}'
將返回:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"mydata_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "aaa",
"doc_count" : 3
}, {
"key" : "fff",
"doc_count" : 3
}, {
"key" : "bbb",
"doc_count" : 2
}, {
"key" : "ccc",
"doc_count" : 1
}, {
"key" : "ddd",
"doc_count" : 1
}, {
"key" : "eee",
"doc_count" : 1
}, {
"key" : "hhh",
"doc_count" : 1
}, {
"key" : "mmm",
"doc_count" : 1
}, {
"key" : "xxx",
"doc_count" : 1
} ]
}
}
}
可能是因為這個問題和公認的答案已經有些年頭了,但現在有更好的方法。
接受的答案沒有考慮到最常見的詞通常是無趣的這一事實,例如“the”、“a”、“in”、“for”等停用詞。
這通常適用於包含類型text
而不是keyword
數據的字段。
這就是為什么 ElasticSearch 實際上有一個專門用於此目的的聚合,稱為“ 重要文本聚合” 。
從文檔:
text
字段但是,它可能比其他類型的查詢花費更長的時間,因此建議在使用 query.match 或之前的sampler類型聚合過濾數據后使用它。
因此,在您的情況下,您將發送這樣的查詢(省略過濾/采樣):
{
"aggs": {
"keywords": {
"significant_text": {
"field": "myfield"
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.