3

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

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

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

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

4

2 回答 2

3

为了将来参考,这里有一个完整的例子来说明如何使用更新

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]
于 2013-03-10T11:42:22.813 回答
2

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

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

update type, id, :doc => {:title => 'new title'}
于 2013-03-10T10:35:04.460 回答