如何使用Retire查询嵌套索引

时间:2015-01-08 18:18:00

标签: ruby-on-rails elasticsearch tire

我有Article型号:

class Article < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  settings default_options do
    mapping do
      indexes :id, index: :not_analyzed
      indexes :roles do
        indexes :machine_name, analyzer: 'keyword'
      end
      indexes :published_at, type: 'date', include_in_all: false
    end
  end
end

其中default_options为:

index: { store: { type: Rails.env.test? ? :memory : :niofs },
         analysis: {
           analyzer: {
             default: {
               tokenizer: "standard",
               filter: ["asciifolding", "lowercase", "snowball"],
               char_filter: ["html_strip"]
             }
           }
        }

我只是在过滤角色时尝试搜索文章,但我不知道该怎么做。我一直在尝试这样的事情没有成功:

Tire.search("article") do
  query { string 'foo bar baz' }
  filter :nested, { path:'roles',
                    query: {
                      filtered: {
                        query: {
                          match_all: {}
                        },
                        filter: {
                          term:{'roles.machine_name' => ['da']}
                        }
                      }
                    }
                  }
end

这给了我那个错误:

QueryParsingException[[development-oaciq::application-article] [nested] nested object under path [roles] is not of nested type];

1 个答案:

答案 0 :(得分:0)

找到question后,似乎不需要nested过滤器,可以这样做:

Tire.search("article") do
  query do
    string 'foo bar baz'
    term 'roles.machine_name', 'test'
  end
end