0

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

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”类型且状态设置为“出售”。

这样做我不能得到任何被破坏的出版物

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

通过执行以下操作,我可以获得所有具有状态为“销售”且自然为“epub”格式的出版物:

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

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

4

1 回答 1

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
于 2013-08-30T12:25:46.600 回答