看看這段代碼吧:
GET /_search
{
"查詢"。{
"匹配" : {
"productName" : "一個產品名稱"
}
}
"大小": 0,
"aggs": {
"my_buckets": {
"復合": {
"來源"。[
{ "_id": {"條款": {"field": "supplierId"}}。
{ "supplierTitle": {"條款": {"欄位": "supplierTitle"}}
]
}
}
}
}
這樣做的目的是:查找所有具有符合查詢要求的productName欄位的專案,并從這些專案中使用專案的_id和supplierTitle屬性制作面。因此,結果告訴我們由某個_id和某個supplierTitle組成的組合的數量(具有這種組合的專案的數量)。現在的問題是,我怎樣才能過濾/搜索這些面,而不是回傳所有的面,而是只回傳用戶想要的帶有SupplierTitle的面(如果我能在SupplierTitle欄位上運行一個自定義匹配查詢,那就太好了)。有什么建議嗎?我研究這個問題已經有一段時間了,但沒有什么成果。
uj5u.com熱心網友回復:
你可以在查詢本身中使用must子句來過濾供應商。
查詢
{
"query": {
"bool": {
"必須"。[
{
"匹配"。{
"productName.keyword": "abc"
}
},
{
"匹配"。{
"supplierTitle": "s1"
}
}
]
}
},
"大小"。0,
"aggs": {
"my_buckets": {
"復合": {
"來源"。[
{
"_id": {
"條款": {
"欄位": "supplierId"
}
}
},
{
"supplierTitle": {
"條款": {
"欄位": "supplierTitle.keyword"
}
}
}
]
}
}
}
}
復合聚合不能作為子聚合使用。你可以使用multi-terms聚合,它在最新版本中可用
。查詢
{
"查詢"。{
"bool": {
"必須"。[
{
"匹配"。{
"productName.keyword": "abc"
}
}
]
}
},
"大小"。0,
"aggs": {
"filter_supplier": {
"過濾器": {
"術語": {
"supplierTitle.keyword": "s1"
}
},
"aggs": {
"my_buckets": {
"multi_terms": {
"條款"。[
{
"欄位": "supplierId"
},
{
"欄位": "supplierTitle.keyword"
}
]
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/315190.html
標籤:
