1
votes

Good day. I have elasticsearch in my rails app using tire. I have many names in my db. And I want to search for them like search_query: "alex ivan", and the output should be ["Alexander Ivanov", "Alex Ivanenko] etc. (Real names from db) I tried to make it with this article but it's not searching. So I've made a quickhack:

params[:search_query] = params[:search_query].split(" ").map{|a|a<<("*")}.join(" ") 

Is it a good decision or I can do it with analyzers etc. ?

1

1 Answers

1
votes

Here's what I did using analyzers for doing a search on names of businesses when I used ElasticSearch. Place this inside your mapping block and modify the index appropriately -- I think this will give you what you want:

indexes :name, :type => 'multi_field', :fields => {
  :name => { :type => 'string', :analyzer => 'standard' },
  :"name.exact" => { :type => 'string', :index => :not_analyzed }
}

Then inside your search and query blocks, something like:

search do
  query do

    # either a must match for exact match
    boolean(:minimum_number_should_match => 1) do  
      must { string "name:#{<variable>}" }
    end

    # or a broader match
    string "name:#{<variable>}*"
  end
end