【问题标题】:tire terms filter not working轮胎术语过滤器不起作用
【发布时间】:2013-04-15 20:26:34
【问题描述】:

我正在尝试使用轮胎/弹性搜索实现“类似范围”的功能。为什么这不起作用,即使我有状态为“Test1”或“Test2”的条目?结果始终为空。

  collection = @model.search(:page => page, :per_page => per_page) do |s|
    s.query {all}
    s.filter :terms, :status => ["Test1", "Test2"]
    s.sort {by :"#{column}", "#{direction}"}
  end 

该方法在没有过滤器的情况下工作正常。过滤方法有问题吗?!我已经检查了轮胎doku....它应该可以工作。

谢谢! :)

【问题讨论】:

    标签: elasticsearch tire


    【解决方案1】:

    您的问题很可能是由使用 status 字段的默认映射引起的,这会将其标记化——小写、拆分为单词等。

    比较这两个:

    http://localhost:9200/myindex/_analyze?text=Text1&analyzer=standard
    
    http://localhost:9200/myindex/_analyze?text=Text1&analyzer=keyword
    

    您的解决方案是在映射中使用keyword 分析器(或将字段设置为not_analyzed)。当字段不是“枚举”类型的数据时,您可以使用multi-field 功能。

    一个工作的 Ruby 版本应该是这样的:

    require 'tire'
    
    Tire.index('myindex') do
      delete
      create mappings: {
        document: {
          properties: {
            status: { type: 'string', analyzer: 'keyword' }
          }
        }
      }
    
      store status: 'Test1'
      store status: 'Test2'
    
      refresh
    end
    
    search = Tire.search 'myindex' do
      query do
        filtered do
          query { all }
          filter :terms, status: ['Test1']
        end
      end
    end
    
    puts search.results.to_a.inspect
    

    注意:在没有提供索引映射、示例数据等的情况下,几乎不可能提供合理的建议(这种情况是一个例外)。

    【讨论】:

    • 感谢 Karmi,我已经更换了分析仪,一切都很好。厉害支持! :)
    猜你喜欢
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多