简体   繁体   English

使用elasticsearch-rails自动更新索引

[英]Automatically update index with elasticsearch-rails

I'm implementing the completion suggester using the elasticsearch-rails gem. 我正在使用elasticsearch-rails gem实现完成建议器。 Everything works except update or delete. 一切正常,除了更新或删除。

For example when I update the title of an article and try to research again, the same title still exist. 例如,当我更新文章的标题并尝试再次研究时,仍然存在相同的标题。

I have included Elasticsearch::Model::Callbacks 我已经包含了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

we had that same problem. 我们遇到了同样的问题。

The only way to change the autocompletion data is to call the optimize API. 更改自动完成数据的唯一方法是调用优化API。 Optimize will cause a segment merge. 优化将导致段合并。 Completion suggesters are stored in their own datastructure calles FST. 完成建议器存储在他们自己的数据结构calles FST中。 They are not part of the regular index, so just refreshing would not work. 它们不是常规索引的一部分,所以只是刷新不起作用。 The datastructure used to store completion suggestions is only created at index time, when a new segment is written. 用于存储完成建议的数据结构仅在编写新段时在索引时创建。 All the old data will be available until a full cleanup happens, which is only guaranteed when segments are merged. 在完全清理之前,所有旧数据都将可用,这仅在合并段时才能得到保证。

So call optimize: 所以调用优化:

http://localhost:9200/indexName/_optimize?max_num_segments=number_of_segments HTTP://本地主机:9200 / INDEXNAME / _optimize max_num_segments = number_of_segments

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

The smaller the number of segments the more performant completion will be. 段数越少,性能完成性越高。

And then check again. 然后再次检查。 For me it worked! 对我来说它有效!

Optimizing is slow and I/O heavy, so you cannot run it all the time, but maybe once a day or so... 优化速度很慢,I / O很重,所以你不能一直运行它,但可能每天运行一次......

Good luck! 祝好运!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM