Automatically update index with elasticsearch-rails

I am implementing a completion helper using a gem elasticsearch-rails

. Everything works except update or uninstall.

For example, when I update the title of an article and try to do research again, the same title still exists.

I have included Elasticsearch::Model::Callbacks

Model:

require 'elasticsearch/model'

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

  def self.suggest(query)
    Article.__elasticsearch__.client.suggest(:index => Article.index_name, :body => {
        :suggestions => {
            :text => query,
            :completion => {
                :field => 'suggest'
            }
        }
    })
  end

 settings :index => { :number_of_shards => 1 } do
   mappings :dynamic => 'false' do
     indexes :title, :type => 'string', :analyzer => 'english'
     indexes :suggest, :type => 'completion', :index_analyzer => 'simple', :search_analyzer =>       'simple', :payloads => true
   end
  end

  def as_indexed_json(options={})
    {
        :name => self.title,
        :suggest => {
            :input => [self.title, self.content],
            :output => self.title,
            :payload => {
                :id => self.id,
                :content => self.content
            }
        }
    }
  end
end

      

Controller:

class ArticlesController < ApplicationController
  def update
    @article = Article.find(params[:id])

    if @article.update_attributes(article_params)
       render :json => @article
    else
       render :json => @article.errors
    end
  end
  # ...
end

      

+3


source to share


1 answer


we had the same problem.

The only way to change the autocomplete data is by invoking API optimization. Optimization will result in segment merge. Completion advisors are stored in their own FST data codes. They are not part of the regular index, so just updating will not work. The danstructure used to store completion clauses is only created at index time when a new segment is written. All old data will be available until a complete cleanup occurs, which is only guaranteed when the segments are merged.

So optimize the call:

http: // localhost: 9200 / indexName / _optimize? max_num_segments = number_of_segments

http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/merge-process.html#optimize-api

The fewer the number of segments, the more successful the execution will be.



And then check again. It worked for me!

Optimization is slow and I / O heavy, so you can't run it all the time, but maybe once a day or so ...

Good luck!

https://groups.google.com/forum/?fromgroups#!searchin/elasticsearch/completion $ 20suggester $ 20delete / elasticsearch / 8Rfg4kGV0ps / YG0V9jM7JhcJ

http://www.elasticsearch.org/blog/you-complete-me/

+3


source







All Articles