How can I do substring match without fuzzy search


(Arup Rakshit) #1

I have indexed 2 columns of my table name and description. I am using a lib to call the ES to get my result. But the result it is giving me is not what I want. I want a substring match, but it does fuzzy search. I do have 2 projects with name as "Project V.02", "Project V.011". When I search with "01", I want only 1 to come as a result, but I get both. What I need to change in my query to achieve this?

class Project < ApplicationRecord
  searchkick
  
  scope :search_by_keyword, ->(keyword) {
    if keyword.present?
      search keyword, fields: %i(name description)
    else
      search "*"
    end
  }

  def search_data
    {
      name: name,
      description: description,
    }
  end
end

Result and curl query to ES is:

2.4.1 :018 > Project.pluck(:name)
   (0.5ms)  SELECT "projects"."name" FROM "projects"
 => ["Project V.02", "Project V.011"]
2.4.1 :019 > Project.search_by_keyword('01').each { |p| p.name }
  Project Search (9.8ms)  curl http://localhost:9200/projects_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"name.analyzed":{"query":"01","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"name.analyzed":{"query":"01","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"name.analyzed":{"query":"01","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"name.analyzed":{"query":"01","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"description.analyzed":{"query":"01","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"description.analyzed":{"query":"01","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"description.analyzed":{"query":"01","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"description.analyzed":{"query":"01","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"size":1000,"from":0,"timeout":"11s","_source":false}'
  Project Load (0.9ms)  SELECT "projects".* FROM "projects" WHERE "projects"."id" IN (2, 1)
 => [#<Project id: 2, name: "Project V.02", zipcode: "21221", timeframe: "4-6 weeks", supplier: nil, user_id: 1, created_at: "2017-12-23 10:10:40", updated_at: "2017-12-23 10:10:40", status: "in_active", description: "Lorem Ipsum is simply dummy text of the printing a...">, #<Project id: 1, name: "Project V.011", zipcode: "30333", timeframe: "4-6 weeks", supplier: nil, user_id: 1, created_at: "2017-12-23 04:25:08", updated_at: "2017-12-30 13:25:27", status: "in_active", description: "Lorem Ipsum is simply dummy text of the printing a...">]
2.4.1 :020 >

I tried this suggestion https://www.reddit.com/r/rails/comments/7nupu5/basic_searckick_question/ but that didn't work.


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.