【问题标题】:Automatically update index with elasticsearch-rails使用 elasticsearch-rails 自动更新索引
【发布时间】:2015-03-10 21:11:06
【问题描述】:

我正在使用 elasticsearch-rails gem 实现完成建议。除更新或删除外,一切正常。

例如当我更新一篇文章的标题并尝试再次研究时,相同的标题仍然存在。

我已经收录了Elasticsearch::Model::Callbacks

型号:

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

控制器:

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

【问题讨论】:

  • 尝试在控制台中创建/更新/删除,看看是否在弹性搜索调用中遇到任何错误。
  • 当您调用update_attributes 时,article_params 包含什么?

标签: ruby-on-rails autocomplete elasticsearch


【解决方案1】:

我们遇到了同样的问题。

更改自动完成数据的唯一方法是调用优化 API。 优化将导致段合并。 完成建议存储在他们自己的称为 FST 的数据结构中。它们不是常规索引的一部分,因此仅刷新是行不通的。 用于存储完成建议的数据结构仅在索引时创建,即写入新段时。在完全清除之前,所有旧数据都将可用,只有在合并段时才能保证。

所以调用优化:

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

段数越少,完成的性能越好。

然后再次检查。对我来说它有效!

优化很慢而且 I/O 很繁重,所以你不能一直运行它,但可能一天一次左右......

祝你好运!

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

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

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多