【问题标题】:How do I reindex just one element with Tire and elasticsearch?如何使用轮胎和弹性搜索重新索引一个元素?
【发布时间】:2013-03-10 09:41:04
【问题描述】:

我似乎不知道如何编辑索引元素。谷歌也没有给我答案。所以,我不确定这是否可能?我之前所做的只是重新索引整个事情,但是随着数据的增长,它变得越来越慢。如何在编辑后仅重新索引一个元素。

这就是我现在正在做的事情。

def add_ele(ele)

    Tire.index ELE_INDEX do
        store :id => ele.ele_id, :title => ele.title, :tags => ele.tags, :itunes_description => ele.itunes_description

        refresh
    end
end

def delete_ele_index
    Tire.index ELE_INDEX do
        delete
    end
end

所以我只是删除了整个索引并循环访问数据库中的所有元素以再次添加它们。非常感谢您的帮助。

【问题讨论】:

    标签: ruby elasticsearch tire


    【解决方案1】:

    供以后参考,这里有一个完整的如何使用更新的例子

    require 'tire'
    require 'yajl/json_gem'
    
    Tire.index 'articles' do
      delete
      create
    
      articles = [
        { :id => '1', :type => 'article', :title => 'one',   :tags => ['ruby']           },
        { :id => '2', :type => 'article', :title => 'two',   :tags => ['ruby', 'python'] },
        { :id => '3', :type => 'article', :title => 'three', :tags => ['java']           },
        { :id => '4', :type => 'article', :title => 'four',  :tags => ['ruby', 'php']    }
      ]
    
      Tire.index 'articles' do
        import articles
      end
    
      refresh
    
      Tire.index 'articles' do
        update 'article', 1, :doc => { :title => 'tone' }
      end
    
      refresh
    
      s = Tire.search 'articles' do
        query do
          string 'title:T*'
        end
    
        filter :terms, :tags => ['ruby']
    
        sort { by :title, 'desc' }
      end
    
      s.results.each do |document|
        puts "* #{ document.title } [tags: #{document.tags.join(', ')}]"
      end
    end
    
    gives
    
    * two [tags: ruby, python]
    * tone [tags: ruby]
    

    【讨论】:

    • 我是否也需要调用刷新。我必须调用 refresh 才能使其正常工作。
    【解决方案2】:

    您不需要删除索引 - 如果您再次存储具有相同 id 的文档,它将更新现有的。

    还有更新 api,如果您只想更改几个字段,它可以让您不必计算和传输整个文档。在最简单的形式中,您只需传递一个包含要更新的属性的哈希,例如:

    update type, id, :doc => {:title => 'new title'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-05
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多