使用嵌套对象进行搜索:无法获得良好的语法来获取我想要的记录

时间:2013-08-29 17:57:32

标签: elasticsearch tire

我的发布模型有这个映射:

mapping do
    indexes :id, type: 'integer'
    indexes :publisher_id, type: 'integer'
    indexes :state
    indexes :title
    indexes :created_at, type: 'date'
    indexes :updated_at, type: 'date'
    indexes :formats, type: 'nested' do
        indexes :pid, type: 'integer'
        indexes :key_type
        indexes :value, type: 'string', analyzer: 'whitespace'
        indexes :state
        indexes :protection
        indexes :nature
        indexes :start_sale_at, type: 'date'
        indexes :issued_on, type: 'date'
        indexes :format_aliases do
            indexes :value, analyzer: 'whitespace'
        end
        indexes :cost do
            indexes :id, type: 'integer'
            indexes :country
            indexes :format
            indexes :amount, type: 'integer'
        end
    end
    indexes :collections do
        indexes :collection_id, type: 'integer'
        indexes :collection_title
    end
    indexes :contributors do
        indexes :contributor_id, type: 'integer'
        indexes :contributor_first_name, analyzer: 'whitespace'
        indexes :contributor_last_name, analyzer: 'whitespace'
    end
end

我希望得到所有的状态未被“销毁”的出版物,其中一种格式性质是'epub'类型,状态设置为'sell'。

我可以不做任何被破坏的出版物

tire.search do
    query do
        boolean do
            must_not {term :state, 'destroyed'}
        end
    end
end

我可以通过这样做获得所有出版物的状态为'sell'且nature为'epub'的格式:

tire.search do
    nested path: 'formats' do
        query do
            match 'formats.nature', 'epub'
            match 'formats.state', 'sell'
        end
   end
end

但我不能将两者合并在一起以获得完整的解决方案。

1 个答案:

答案 0 :(得分:0)

刚刚得到解决方案:

nested_filter = Tire::Search::Query.new do
  filtered do
    query { all }
    filter :term, {'formats.nature' => 'epub'}
    filter :term, {'formats.state' => 'sell'}
  end
end

tire.search(load: true) do
  query do
    filtered do
      query do
        boolean do
          must_not { term :state, 'processing'}
        end
      end
      filter :nested, {path: 'formats'}.merge({ query: nested_filter.to_hash})
    end
  end
end