[英]Elasticsearch - How to return distinct documents for certain fields
我有下一個elasticsearch查詢,我需要知道如何只獲得某些字段的不同結果。 (就像sql distinct:SELECT DISTINCT column1,column2,... FROM table_name:wink:
這是我的查詢
{
"_source": ["part", "manufacturer", "shortdesc"],
"query": {
"match": {
"part": "2n2222"
}
}
}
以下是我得到的結果:
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "13921",
"_score" : 207.16005,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "13923",
"_score" : 207.16005,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "811202",
"_score" : 202.03964,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "534059",
"_score" : 202.03964,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "534062",
"_score" : 202.03964,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "144303",
"_score" : 202.03964,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "557240",
"_score" : 202.03964,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Infineon"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "13924",
"_score" : 201.24086,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "557235",
"_score" : 201.24086,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "55566",
"_score" : 201.24086,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "50873",
"_score" : 201.24086,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "13915",
"_score" : 199.76857,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "591924",
"_score" : 199.76857,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "526043",
"_score" : 199.76857,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "423282",
"_score" : 198.89282,
"_source" : {
"part" : "2N2222A",
"manufacturer" : "Microsemi Corporation"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "565951",
"_score" : 193.51782,
"_source" : {
"part" : "P2N2222A",
"manufacturer" : "ON Semiconductor"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "13920",
"_score" : 192.1505,
"_source" : {
"part" : "P2N2222A",
"manufacturer" : "ON Semiconductor"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "2885944",
"_score" : 191.28773,
"_source" : {
"part" : "Q2N2222A",
"manufacturer" : "Freescale Semiconductor"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "765656",
"_score" : 191.28773,
"_source" : {
"part" : "2N2222AL",
"manufacturer" : "Microsemi"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "491090",
"_score" : 190.78474,
"_source" : {
"part" : "2N2222AUB",
"manufacturer" : "Microsemi Corporation"
}
}
如果記錄包含相同的部件和制造商,則認為該記錄是重復的。 我需要為這些字段獲取不同的值。
非常感謝你的幫助。
我相信你需要在查詢中使用聚合來獲得不同的對行為。 請參閱此示例以獲取不同值查詢的示例。
鏈接問題與您的案例之間的主要區別在於您有兩個字段,並且您需要所有不同的對,而不是兩個字段的不同值。
編輯:剛試過這個,它似乎有一個像你想要做的行為。 您可以通過刪除/禁用術語聚合的doc_count計數並使用_source來優化它,就像您在問題中所做的那樣。 您還可以添加查詢和匹配子句以過濾到給定的零件/制造商。
EDIT2:在問題中添加了對請求的查詢/匹配。
GET YOURINDEX/_search
{
"query": {
"match": {
"part.keyword": "2n2222"
}
},
"size": 0,
"aggs": {
"actions": {
"terms": {
"field": "part.keyword"
},
"aggs": {
"emails": {
"terms": {
"field": "manufacturer.keyword"
}
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.