[英]Elasticsearch term AND range filter using tire
I am trying build a search function in rails based on elasticsearch+tire enabling search for Persons with filtering for associated Objects and their Values. 我正在尝试基于elasticsearch + tire在rails中构建搜索功能,以通过过滤关联对象及其值来搜索Persons。 A Person has_many Objects, and an Object has_many Values.
一个人has_many对象,一个对象has_many值。
I have managed to get the filtering on the object name (params[:object]) to work, but not for object+value. 我设法使对对象名称(params [:object])的筛选起作用,但对object + value却不起作用。 How should I construct the range filter for the values and the mapping so that the value is dependent on the object?
我应该如何构造值和映射的范围过滤器,以使值取决于对象?
Person controller 人控
mapping do
indexes :objects do
indexes :_id
indexes :object_values do
indexes :value
end
end
indexes :name, type: 'string', analyzer: 'snowball'
end
def self.search(params)
tire.search do
query do
boolean do
must { string params[:query]} if params[:query].present?
end
end
filter :term, {"objects._id" => params[:object]} if params[:object].present?
filter :range, “objects.object_values.value” => {from: params[:value] } if params[:value].present?
end
end
def to_indexed_json
{
:name => name,
:objects => objects.map { |o| {
:_type => 'object',
:_id => o.id,
:object_values => o.object_values.map {|ov| {
:_type => 'object_value',
:_id => ov.id,
:value => ov.value } },
} }
}.to_json
end
使用gt
或gte
而不是from
来指定范围的下限
filter :range, “objects.object_values.value” => {from: params[:value] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.