首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用elasticsearch-rails自动更新索引

使用elasticsearch-rails自动更新索引
EN

Stack Overflow用户
提问于 2015-01-13 08:40:07
回答 1查看 2.2K关注 0票数 4

我正在使用elasticsearch-rails gem实现完成建议程序。除了更新和删除之外,一切都可以正常工作。

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

我已经包含了Elasticsearch::Model::Callbacks

型号:

代码语言:javascript
复制
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

控制器:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-01-28 20:24:57

我们也有同样的问题。

更改自动完成数据的唯一方法是调用optimize API。优化将导致线段合并。补全建议存储在它们自己的数据结构调用FST中。它们不是常规索引的一部分,因此仅仅刷新是行不通的。用于存储完成建议的数据结构仅在索引时创建,当新段被写入时。所有旧数据都将可用,直到完全清理发生为止,这只有在合并段时才能保证。

所以调用optimize:

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/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27913227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档