2016-10-07 131 views
0

我正在用elasticsearch-model gem構建Rails web應用程序。我想通過過濾爲我的Place模型構建搜索查詢。我成功地通過名稱進行搜索,但是我希望能夠用城市來篩選我的搜索(在此示例中爲倫敦)。Elasticsearch Rails過濾器

現在我的查詢看起來是這樣的:

"query": { 
    "bool": { 
     "must": { 
     "match": { 
      "name": { 
      "query": term, 
      "operator": "and", 
      "fuzziness": 1 
      } 
     } 
     }, 
     "filter": { 
      "term": { 
       "city": "London" 
      } 
     } 
    } 
    } 

,比我簡單的調用Place.search("here goes query").records.to_a

不帶過濾器的部分搜索工作正常,但是當我添加濾鏡我沒有得到任何結果。

這是地方搜索映射:

settings analysis: { 
    filter: { 
     ngram_filter: { 
      type: "nGram", 
      min_gram: 2, 
      max_gram: 20 
     } 
    }, 
    analyzer: { 
     ngram_analyzer: { 
      type: "custom", 
      tokenizer: "standard", 
      filter: [ 
       "lowercase", 
       "asciifolding", 
       "ngram_filter" 
      ] 
     }, 
     whitespace_analyzer: { 
      type: "custom", 
      tokenizer: "whitespace", 
      filter: [ 
       "lowercase", 
       "asciifolding" 
      ] 
     } 
    } 
} do 
    mappings dynamic: 'false' do 
    indexes :name, 
      type: "string", 
      analyzer: "ngram_analyzer", 
      search_analyzer: "whitespace_analyzer" 

這裏是我使用的是看如何過濾鏈接:Elasticsearch doc

+0

一個commong缺陷是數據被分解成什麼彈性搜尋所謂的「令牌」的基礎上的地方標記生成器和過濾器來匹配他們所創造的令牌而不是最初索引的數據。例如,在索引一個值爲「AD-13」的字段後,我得到了「ad」和「13」的標記,因此「AD-13」沒有得到任何結果。 – bkunzi01

+0

我認爲我修正了一點,我添加了'索引:city,type:「string」'。然而,它只適用於由單個單詞組成的小寫字母,我認爲我必須使用分析器來修復它。 –

+0

分析儀肯定是問題。您可以指定不分析字段,這是我所做的,以避免將空格值打破爲單獨的標記,從而導致搜索不正確。 – bkunzi01

回答

0

我在映射型城市來定義字符串,不執行我在構建令牌時對該領域的任何分析。

所以在映射我說:

indexes :city, 
     type: "string", 
     index: "not_analyzed"