简体   繁体   中英

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. A Person has_many Objects, and an Object has_many Values.

I have managed to get the filtering on the object name (params[:object]) to work, but not for 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

使用gtgte而不是from来指定范围的下限

filter :range, “objects.object_values.value”  => {from: params[:value] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM