2015-10-28 54 views
3

simple_query_string我有一個指數建立了我的所有文檔:彈性搜索數倍提升

{ 
    "mappings" { 
    "book" { 
     "_source": { "enabled": true }, 
     "properties": [ 
     "title": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" }, 
     "description": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" }, 
     "author": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" } 
     ] 
    } 
    } 
} 

我通過推到這個所謂的「」的指標。

我想要做的是執行搜索與以下要求。假設用戶輸入類似「黃色大鏟」

  1. 執行搜索用戶輸入的關鍵字的方式有三種:
    1. 由於是作爲一個整體一句話:「簡單的黃色鏟」
    2. 作爲集和關鍵字:「簡單+黃+鏟」
    3. 爲一組或關鍵字:「簡單|黃色|鏟」
  2. 確保關鍵字組按優先順序執行(BO osted):
    1. 全文第一
    2. AND'd第二
    3. 邏輯與的第三

使用簡單的查詢作品找到一個單一的搜索:

{ 
    "query": { 
    "simple_query_string": { 
     "query": "\"simple yellow shovel\"" 
    } 
    } 
} 

如何用boosting執行多重搜索? 或者我應該在索引字段上使用類似「匹配」的查詢嗎?

+0

爲什麼你有你的財產'not_analyzed'?這沒有意義。始終分析您要運行文本搜索的字段。而且你沒有優先考慮你的搜索領域。我認爲匹配的標題應該比描述中的匹配具有更高的優先級? –

+0

我會修復'not_analyzed'部分。這是一個很大的「哎呀」。優先排序爲了簡潔起見我省略了。 –

回答

4

我不知道我是否得到了這一個正確的。我假設的 作者>標題優先順序>描述

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "bool": { 
      "must": [ 
       { 
       "multi_match": { 
        "query": "simple yellow shovel", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "type": "phrase", 
        "boost": 10 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "bool": { 
      "must": [ 
       { 
       "multi_match": { 
        "query": "simple", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 5 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "yellow", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 5 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "shovel", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 5 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "simple", 
      "fields": [ 
       "author^7", 
       "title^3", 
       "description" 
      ], 
      "boost": 2 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "yellow", 
      "fields": [ 
       "author^7", 
       "title^3", 
       "description" 
      ], 
      "boost": 2 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "shovel", 
      "fields": [ 
       "author^7", 
       "title^3", 
       "description" 
      ], 
      "boost": 2 
      } 
     } 
     ] 
    } 
    } 
} 

會有人請確認這一點?您可以參考Boost Query鏈接瞭解更多信息。這是你想要的?

我希望這有助於!

編輯:用dis_max

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "dis_max": { 
      "tie_breaker": 0.7, 
      "queries": [ 
       { 
       "bool": { 
        "must": [ 
        { 
         "multi_match": { 
         "query": "simple yellow shovel", 
         "fields": [ 
          "author^7", 
          "title^3", 
          "description" 
         ], 
         "type": "phrase", 
         "boost": 10 
         } 
        } 
        ] 
       } 
       }, 
       { 
       "bool": { 
        "must": [ 
        { 
         "dis_max": { 
         "tie_breaker": 0.7, 
         "queries": [ 
          { 
          "multi_match": { 
           "query": "simple", 
           "fields": [ 
           "author^7", 
           "title^3", 
           "description" 
           ], 
           "boost": 5 
          } 
          }, 
          { 
          "multi_match": { 
           "query": "yellow", 
           "fields": [ 
           "author^7", 
           "title^3", 
           "description" 
           ], 
           "boost": 5 
          } 
          }, 
          { 
          "multi_match": { 
           "query": "shovel", 
           "fields": [ 
           "author^7", 
           "title^3", 
           "description" 
           ], 
           "boost": 5 
          } 
          } 
         ] 
         } 
        } 
        ] 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "simple", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 2 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "yellow", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 2 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "shovel", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 2 
       } 
       } 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

這似乎改寫給我更好的結果ATLEAST在我的數據集。這是一個很好的理解來源dismax

請發揮很大的作用,看看你是否得到預期的結果。 使用Explain API的幫助。

+0

我認爲這可以使用'dis_max'查詢重寫。應該更優雅,配置更好。 –

+0

即使'dis_max'更好,這是一個**優秀的*第一步。至少讓我指出了正確的道路。如果有人用'dis_max'迴應並且它能夠工作,那將會贏得答案 - 否則這至少讓我開始了! –

+0

感謝@EvaldasBuinauskas建議dis_max。我已經更新了我的答案。讓我知道如果它不起作用 – ChintanShah25

2

我已經使用Dis Max Query重寫了這個。請記住,你可以嘗試不同的類型來獲得更好的結果。看到這些:

  1. best_fields
  2. most_fields
  3. cross_fields

查詢:

POST /your_index/your_type/_search 
{ 
    "query": { 
    "dis_max": { 
     "tie_breaker": 0.7, 
     "boost": 1.2, 
     "queries": [ 
     { 
      "multi_match": { 
      "query": "simple yellow showel", 
      "type": "phrase", 
      "boost": 3, 
      "fields": [ 
       "title^3", 
       "author^2", 
       "description" 
      ] 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "simple yellow showel", 
      "operator": "and", 
      "boost": 2, 
      "fields": [ 
       "title^3", 
       "author^2", 
       "description" 
      ] 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "simple yellow showel", 
      "fields": [ 
       "title^3", 
       "author^2", 
       "description" 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

派息最多的查詢會挑文件的得分是所有三個查詢最多。我們給"type": "phrase""operator": "and"增加了額外的提升,同時我們保持最後的查詢不變。