Opensearch 攝取與此示例類似的檔案(它只是一個最小示例):
PUT nested_test/_doc/4
{
"log": "This is a fourth log message",
"function": "4 test function",
"related_objects": [
{ "type": "user", "id": "10" },
{ "type": "offer", "id": "120" }
]
}
PUT nested_test/_doc/5
{
"log": "This is a fifth log message",
"function": "5 test function",
"related_objects": [
{ "type": "user", "id": "120" },
{ "type": "offer", "id": "90" }
]
}
對于這些檔案中的許多,我想過濾那些具有特定相關物件(例如type=user 和 id=120)的檔案。對于上面的示例資料,這應該只回傳 id 為 5 的檔案。使用如下的簡單過濾器(DQL 語法)不起作用:
related_objects.type:user and related_objects.id:120
因為這也將匹配檔案 5,因為有一個用戶型別的相關物件和一個 ID 為 120 的相關物件,盡管它不是 ID 為 120 的相關用戶物件,但它是相關的報價。
uj5u.com熱心網友回復:
如果使用Array[object],則欄位型別為nested,檔案參考
uj5u.com熱心網友回復:
Elasticsearch 查詢示例:
{
"query" : {
"nested" : {
"path" : "related_objects",
"query" : {
"bool" : {
"must" : [
{
"term" : {"related_objects.type" : "MYTYPE"}
},
{
"term" : {"related_objects.id" : "MYID"}
}
]
}
}
}
}
}
基本上只需進入嵌套查詢并將所有 AND 條件指定為 bool 查詢中的 MUST 子句。
uj5u.com熱心網友回復:
一旦該欄位被宣告為嵌套欄位,就可以運行一個簡單的 DQL 查詢來獲取所需的資訊:
related_objects:{type:"user" and id:120}
這要求該欄位之前已定義為嵌套:
PUT my-index-000001
{
"mappings": {
"properties": {
"related_objects": {
"type": "nested"
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498382.html
