2016-10-07 133 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"