現在學習到了實體搜索,加入我們想搜索例如Person、product這樣的實體,在這種情況下,一些關鍵的信息其實分佈在數據的多個屬性裏的。比如person的數據存儲
{
"firstname": "Peter",
"lastname": "Smith"
}
或者是地址,比如這樣
```
{
"street": "5 Poland Street",
"city": "London",
"country": "United Kingdom",
"postcode": "W1V 3DG"
}
這裏聽起來更像是 Multiple Query Strings(查找多個字段),但是在這兩種情況中存在着巨大的差異。在Multiple Query Strings中,我用多個分開的訪問字符串來訪問每個屬性。在這種情況中,我們想搜索多個屬性,且通過僅僅一個搜索字符串。
我們的用戶可能想搜索person "Peter Smith"。但是每一個單詞出現在不同的屬性裏,而用dis_mas/best_fields 訪問(這些都是針對單個屬性的查詢)絕對是一錯誤的方式。
一個樸素的方法
我們可以訪問每個屬性並且將每一個屬性匹配的score綜合起來,例如
{
"query": {
"bool": {
"should": [
{ "match": { "street": "Poland Street W1V" }},
{ "match": { "city": "Poland Street W1V" }},
{ "match": { "country": "Poland Street W1V" }},
{ "match": { "postcode": "Poland Street W1V" }}
]
}
}
}
很明顯,爲每一個屬性重複這些訪問字符串很快變的很囉嗦。當然我們可以用 multi_match來替代,並且設type和most_fileds 去告訴它把所有屬性的匹配度分值綜合起來
{
"query": {
"multi_match": {
"query": "Poland Street W1V",
"type": "most_fields",
"fields": [ "street", "city", "country", "postcode" ]
}
}
}
用most_fields方法所產生的問題
most_fileds 方法來進行實體的搜索所產生的問題看起來不太明顯
https://www.elastic.co/guide/en/elasticsearch/guide/current/_cross_fields_entity_search.html
這裏不太會翻譯 有需要的自己看吧。。。囧