![](/img/trans.png)
[英]Elastic search apply boost only for certain documents based on their field value
[英]How to get unique documents in elastic search based on a field, and 'group by' the result based on other fields
我剛開始使用彈性搜索,需要解決一個對我來說太復雜的問題。 我在索引中有數千個文檔,我必須從中查詢預定義數量的文檔(也可以是幾千個),我必須從基於另一個字段的唯一文檔中找到基於某些字段的文檔組(唯一文檔的數量最多可達幾百個)。
我的索引中的文檔如下所示:
{
"complexProperty1" : {
"A" : "example",
"B" : "1",
"D" : true,
"E" : "case",
"F" : ["guide1","guide2"]
},
"complexProperty2" : {
"X" : "10",
"Y" : ["specimen1","specimen2"],
"Z" : "blueprint"
}
}
許多文檔將complexProperty1.A
作為“示例” 。 我想將它們包含一次,生成的文檔需要按complexProperty1.D
和complexProperty1.E
分組,即對於每對complexProperty1.D
和complexProperty1.E
,我有一個文檔列表(我只需要這些文檔在我的結果)。 我正在使用 Nest 來實現這一點。
您可以從一堆原始terms
聚合開始,然后構建回到NEST DSL的方式:
POST complexities/_doc
{
"complexProperty1": {
"A": "example",
"B": "1",
"D": true,
"E": "case",
"F": [
"guide1",
"guide2"
]
},
"complexProperty2": {
"X": "10",
"Y": [
"specimen1",
"specimen2"
],
"Z": "blueprint"
}
}
GET complexities/_search
{
"size": 0,
"aggs": {
"by_A": {
"terms": {
"field": "complexProperty1.A.keyword"
},
"aggs": {
"by_D": {
"terms": {
"field": "complexProperty1.D"
}
},
"by_E": {
"terms": {
"field": "complexProperty1.E.keyword"
}
}
}
}
}
}
為了獲得基礎文檔,您可以 append 為每個子聚合添加一個top_hits
,包括。 頂層:
{
"size": 0,
"aggs": {
"by_A": {
"terms": {
"field": "complexProperty1.A.keyword"
},
"aggs": {
"top_hits_only": {
"top_hits": {
"_source": "*"
}
},
"by_D": {
"terms": {
"field": "complexProperty1.D"
},
"aggs": {
"top_hits_only": {
"top_hits": {
"_source": "*"
}
}
}
},
"by_E": {
"terms": {
"field": "complexProperty1.E.keyword"
},
"aggs": {
"top_hits_only": {
"top_hits": {
"_source": "*"
}
}
}
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.