2014-10-02 50 views
0

我想用elasticsearch多字搜索,所有的領域都與分配分析儀在文檔中進行檢查。Elasticsearch多字,多領域的搜索與分析

所以,如果我有一個映射:

{ 
"settings": { 
    "analysis": { 
     "analyzer": { 
     "folding": { 
      "tokenizer": "standard", 
      "filter": [ "lowercase", "asciifolding" ] 
     } 
     } 
    } 
    }, 
    "mappings" : { 
    "typeName" :{ 
     "date_detection": false, 
     "properties" : { 
     "stringfield" : { 
      "type" : "string", 
      "index" : "folding" 
     }, 
     "numberfield" : { 
      "type" : "multi_field", 
      "fields" : { 
      "numberfield" : {"type" : "double"}, 
      "untouched" : {"type" : "string", "index" : "not_analyzed"} 
      } 
     }, 
     "datefield" : { 
      "type" : "multi_field", 
      "fields" : { 
      "datefield" : {"type" : "date", "format": "dd/MM/yyyy||yyyy-MM-dd"}, 
      "untouched" : {"type" : "string", "index" : "not_analyzed"} 
      } 
     } 
     } 
    } 
    } 
} 

正如你看到的我有不同類型的字段,但我知道的結構。 我想要做的就是用字符串開始搜索,以便使用分析儀檢查所有字段。

例如,如果查詢字符串是:

John Smith 2014-10-02 300.00 

我想搜索「約翰」,「史密斯」,「2014年10月2日」和「300.00」,在所有的領域,計算相關性分數也是如此。更好的解決方案是在單個文檔中具有更多字段匹配的解決方案。

到目前爲止,我是能夠通過使用multi_field中的所有字段進行搜索,但在這種情況下,我無法解析300.00,因爲300存儲在multi_field的字符串的一部分。 如果我在「_all」字段中搜索,則不使用分析器。

我應該如何修改我的映射或我的查詢能夠做到多單詞搜索,其中日期和數字在多字查詢字符串被認可? 現在,當我執行搜索時,發生錯誤,因爲整個字符串不能被解析爲數字或日期。如果我使用multi_search的字符串表示,那麼300.00將不會是結果,因爲字符串表示形式是300.

(我希望類似於Google搜索,其中日期,數字和字符串在多字查詢)

任何想法?

謝謝!

回答

-1

使用whitespaceanalyzer過濾器,然後將這種analyzersearch_analyzer到字段mapping將拆分零件查詢和他們每個人將被應用到索引找到最佳匹配。並且使用ngram作爲index_analyzer會很好的改善結果。 我使用以下設置查詢:

"query": { 
      "multi_match": { 
       "query": "sample query", 
       "fuzziness": "AUTO", 
       "fields": [ 
        "title", 
        "subtitle", 
       ] 
      } 
     } 

而對於映射和設置:

{ 
"settings" : { 
    "analysis": { 
     "analyzer": { 
      "autocomplete": { 
       "type": "custom", 
       "tokenizer": "whitespace", 
       "filter": [ 
        "standard", 
        "lowercase", 
        "ngram" 
       ] 
      } 
     }, 
     "filter": { 
      "ngram": { 
       "type": "ngram", 
       "min_gram": 2, 
       "max_gram": 15 
      } 
     } 
    }, 
"mappings": { 
     "title": { 
      "type": "string", 
      "search_analyzer": "whitespace", 
      "index_analyzer": "autocomplete" 
     }, 
     "subtitle": { 
      "type": "string" 
     } 
    } 
} 

參見以下answerarticle的更多細節。