Rails - expert on implementing Elasticsearch and the search API

I am using the search API and now you need to add a completion assistant , I am using elasticsearch-rails stone.

When I search for an article, everything works http: // localhost: 9200 / articles / _search

"query": { "multi_match": { "query": "test", "fields": [ "title", "tags", "content" ] } } } 

But since I implemented the completion assistant, I had to edit as_indexed_json to work, but now the search API no longer works, only suggestions.

Here is my article model:

  def self.search(query) __elasticsearch__.search( { query: { multi_match: { query: query, fields: ['title', 'content', 'tags'] } } }) end def self.suggest(query) Article.__elasticsearch__.client.suggest(:index => Article.index_name, :body => { :suggestions => { :text => query, :completion => { :field => 'suggest' } } }) end def as_indexed_json(options={}) { :name => self.title, :suggest => { :input => self.title, :output => self.title, :payload => { :content => self.content, :tags => self.tags, :title => self.title } } }.as_json end 

Is it possible that _search and _suggest work together with the same model?

+6
source share
1 answer

I just dig into elasticsearch, but as I understand it, you can add what you had before changing the serializer function and recreate the indexes, they will live well together in db. For instance:

 def as_indexed_json(options={}) { :name => self.title, :suggest => { :input => self.title, :output => self.title, :payload => { :content => self.content, :tags => self.tags, :title => self.title } } }.as_json.merge(self.as_json) # or the customized hash you used 

To avoid index redundancy, you can look at aliases and routing .

+5
source

Source: https://habr.com/ru/post/983394/


All Articles