2015-12-21 127 views
0

我的索引中有一個相對簡單的構造。一位顧客擁有自己的房產,他擁有自己的身份,電子郵件和電話作爲嵌套屬性。到目前爲止,當我查詢嵌套對象時,所有查詢都可以正常工作。我正在開發一些類似於全球搜索的內容,允許用戶輸入任何信息,並向他展示相應的客戶。爲了這個目的,我遍歷了所有我擁有的嵌套屬性(因爲我不知道他究竟在尋找什麼)。我的查詢是這樣的:Elasticsearch嵌套查詢的操作符

{ 
"sort": ["_score"], 
"query": { 
    "bool": { 
     "must": [{ 
      "nested": { 
       "path": "identity", 
       "query": { 
        "bool": { 
         "must": [{ 
          "multi_match": { 
           "query": "my search", 
           "fields": "identity.*" 
          } 
         }] 
        } 
       } 
      } 
     }, { 
      "nested": { 
       "path": "phones", 
       "query": { 
        "bool": { 
         "must": [{ 
          "multi_match": { 
           "query": "my search", 
           "fields": "phones.*" 
          } 
         }] 
        } 
       } 
      } 
     }] 
    } 
} 

但是看來,當你有多個嵌套查詢「與」運營商應用,因此我沒有得到正確的信息。如果我只搜索一個嵌套對象,我總會得到正確的結果。我試着把「操作符」=>「或」幾乎放在任何地方,但沒有任何效果,並且文檔沒有太多說明這種情況(或者至少我找不到任何東西)。我錯過了什麼?

+0

你也可以去掉「排序」:「_score」],這是默認的。 – Tomer

回答

0

您應該用「應該」替換「必須」以獲取或行爲,查看更多here

+0

嗯,這似乎真的爲查詢添加了邏輯「或」,但它打破了很多其他情況。例如,如果一個有2個客戶(john和dom),並且我搜索john,我會得到john和dom作爲結果。如果用'should'改變'must',會發生這種情況。 – user2362462

+0

您可以分享2份文件的樣本嗎?一個與john和一個與dom? – Tomer

+0

發表上面的代碼:) – user2362462

0

好吧,這裏是一個示例:

{ 
"uid": "uid", 
"oid": "oid", 
"tags": ["VIP"], 
"identity": { 
    "_id": { 
     "$oid": "567901d6ab265d4cf7000001" 
    }, 
    "first": "John", 
    "gender": "male", 
    "last": "", 
    "middle": "", 
    "prefix": "", 
    "suffix": "" 
}, 
"phones": [{ 
    "_id": "23232323-642f-4ce1-ab1b-542bbd1468ea", 
    "created_at": null, 
    "name": "mobile", 
    "updated_at": null, 
    "value": "987654321" 
}], 
"emails": [{ 
    "_id": "12121212-642f-4ce1-ab1b-542bbd1468ea", 
    "created_at": null, 
    "name": "home", 
    "updated_at": null, 
    "value": "[email protected]" 
}] 
} 

{ 
"uid": "uid", 
"oid": "oid", 
"tags": ["VIP"], 
"identity": { 
    "_id": { 
     "$oid": "567901d6ab265d4cf7000001" 
    }, 
    "first": "Dom", 
    "gender": "male", 
    "last": "", 
    "middle": "", 
    "prefix": "", 
    "suffix": "" 
}, 
"phones": [{ 
    "_id": "23232323-642f-4ce1-ab1b-542bbd1468ea", 
    "created_at": null, 
    "name": "mobile", 
    "updated_at": null, 
    "value": "987654321" 
}], 
"emails": [{ 
    "_id": "12121212-642f-4ce1-ab1b-542bbd1468ea", 
    "created_at": null, 
    "name": "home", 
    "updated_at": null, 
    "value": "[email protected]" 
}] 
} 

所以,如果我使用must條款。我做約翰/ DOM我沒有得到任何結果(請記住搜索,我搜索了所有嵌套的文件和手機不包含任何'john/dom',如果我只搜索身份/電子郵件,我會得到正確的結果,因爲電子郵件也包含名稱)。

如果我切換到should子句,我做同樣的查詢我回來了約翰和dom。

最後的查詢看起來是這樣的:

{ 
"_source": ["oid", "uid"], 
"sort": ["_score"], 
"query": { 
    "bool": { 
     "should": [{ 
      "nested": { 
       "path": "identity", 
       "query": { 
        "bool": { 
         "must": [{ 
          "multi_match": { 
           "query": "john", 
           "operator": "and", 
           "fields": ["identity.*"], 
           "type": "cross_fields", 
           "tie_breaker": 1.0 
          } 
         }] 
        } 
       } 
      } 
     }, { 
      "nested": { 
       "path": "emails", 
       "query": { 
        "bool": { 
         "must": [{ 
          "multi_match": { 
           "query": "john", 
           "operator": "and", 
           "fields": ["emails.*"], 
           "type": "cross_fields", 
           "tie_breaker": 1.0 
          } 
         }] 
        } 
       } 
      } 
     }, { 
      "nested": { 
       "path": "phones", 
       "query": { 
        "bool": { 
         "must": [{ 
          "multi_match": { 
           "query": "john", 
           "operator": "and", 
           "fields": ["phones.*"], 
           "type": "cross_fields", 
           "tie_breaker": 1.0 
          } 
         }] 
        } 
       } 
      } 
     }], 
     "filter": [{ 
      "term": { 
       "oid": "oid" 
      } 
     }] 
    } 
} 
}