我希望按標題從我的檔案中挑選 4 種不同的食譜。
我試過這樣的事情:
body: {
'_source': {
'includes': 4,
},
'size': 4,
'query': {
bool: {
should: [
{match: {Title: {query: 'Eggs', operator: 'OR'}}},
{match: {Title: {query: 'Apple', operator: 'OR'}}},
{match: {Title: {query: 'Onion', operator: 'OR'}}},
{match: {Title: {query: 'spaghetti', operator: 'OR'}}},
],
},
},
}
但沒有成功。它只找到意大利面食譜,但如果我正在尋找洋蔥食譜,我可以在另一個查詢中找到它們,因此我的合并查詢毫無意義:(
uj5u.com熱心網友回復:
不可能在查詢部分的每個子句中獲取“n”個檔案。您需要使用top_hits聚合來實作它。
{
"query": {
"bool": {
"should": [
{
"match": {
"Title": {
"query": "Eggs",
"operator": "OR"
}
}
},
{
"match": {
"Title": {
"query": "Apple",
"operator": "OR"
}
}
},
{
"match": {
"Title": {
"query": "Onion",
"operator": "OR"
}
}
},
{
"match": {
"Title": {
"query": "spaghetti",
"operator": "OR"
}
}
}
]
}
},
"aggs": {
"recipies": {
"terms": {
"field": "Title.keyword",
"size": 10000
},
"aggs": {
"docs": {
"top_hits": {
"size": 4
}
}
}
}
}
}
術語聚合將像 group by 一樣作業,top_hits 將回傳每個組的前“4”個檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/363501.html
標籤:弹性搜索
