Elasticsearch rails not geting proper response

400 Views Asked by At

I am using elasticsearch-rails and elasticsearch-model gem for searching words in my rails app.

I want to make my search case insensitive and must be independent of pluralization. I researched a lot on google but got a hunch on how to do it using analyzer but not success. So I had to post a new question.

Here is my model where I want to search to take place

require 'elasticsearch/model'

class Article < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :title, analyzer: 'english', index_options: 'offsets'
      indexes :text, analyzer: 'english'
    end
  end

  def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['title^10', 'text']
          }
        },
        highlight: {
          pre_tags: ['<em>'],
          post_tags: ['</em>'],
          fields: {
            title: {},
            text: {}
          }
        }
      }
    )
  end
end

# Delete the previous articles index in Elasticsearch
Article.__elasticsearch__.client.indices.delete index: Article.index_name rescue nil

# Create the new index with the new mapping
Article.__elasticsearch__.client.indices.create \
  index: Article.index_name,
  body: { settings: Article.settings.to_hash, mappings: Article.mappings.to_hash }

# Index all article records from the DB to Elasticsearch
Article.import

#Article.import force: true

My questions are how do I do search word? tshirt, T-shirt, Tshirts should all match. Any links for further research is also helpful. Thank you

1

There are 1 best solutions below

5
On

One of the method for searching in a table is:

User.where("firstname ILIKE ? OR lastname ILIKE ?", "%#{params[q]}%", "%#{params[q]}%" );

where

User is Model

params[q] is search query parameter.