简体   繁体   中英

ruby on rails: ElasticSearch / Tire dynamic search on multiple indices

I've done a bunch of searching and I haven't been able to get an answer to this question - hopefully this isn't a repeat (apologies if it is)...

Preface: I'm using Rails & Tire to perform ElasticSearch.

I have an object, Place, with attributes "name", "city", "state", and "zip". They are indexed as follows:

indexes :name, :type => 'multi_field', :fields => { 
  :name => { :type => 'string', :analyzer => 'snowball' }, 
  :"name.exact" => { :type => 'string', :index => :not_analyzed } 
} 
indexes :city 
indexes :state 
indexes :zip 

There are three conditions for searching: 1. Name only, 2. (City, State OR Zip), 3. Name AND (City, State OR Zip).

My code for the "query" block is:

  if (City, State).present? 
    boolean do 
      must { string "name:#{name}*" } if name.present? 
      must { string "city:#{city_state}*" } 
      must { string "state:#{city_state}*" } 
    end 
  elsif (Zip).present? 
    boolean do 
      must { string "name:#{name}*" } if name.present? 
      must { string "zip:#{query_parameters["zip"]}*" } 
    end 
  else 
    string "name:#{name}*" } 
  end 

The aforementioned search conditions #1 and #2 work as expected against multiple tests. However, condition 3 does not - it seems to only pay attention to the "name" field. I'm assuming it has something to do with using the "city_state" variable to search on both "city" and "state"... But I'm doing this because a user can enter either "Chicago" or "Illinois" in the City, State / Zip text box and the search should still work, using either the geographic center of Chicago or the geographic center of Illinois, respectively.

Anything obvious I'm doing wrong?

However, condition 3 does not - it seems to only pay attention to the "name" field

Errr, isn't

string "name:#{name}*"

telling it to do exactly that?

or did you mean to just do

string "#{name}"

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