3

I can't seem to figure out how to edit indexed element. Google doesn't give me an answer either. So, I'm not sure if it's possible at all? What I have done before is just to reindex the whole thing, but as the data grows it becomes slower and slower. How do I reindex just one element after it's been edited.

Here's what I'm doing right now.

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

So I just delete the whole index and loop through all the elements in the database to add them back again. Thanks very much for the help.

toy
  • 11,711
  • 24
  • 93
  • 176

2 Answers2

3

For future reference, here a complete example of how to use update

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]
peter
  • 41,770
  • 5
  • 64
  • 108
  • Do I need to call refresh as well. I have to call refresh just to make it work. – toy Mar 10 '13 at 12:45
2

You don't need to delete the index - if you store a document with the same id a second time, it will update the existing one.

There is also the update api, which saves you from having to compute & transfer the whole document if you just want to change a few fields. In its simplest form you just pass a hash containing the attributes to update, for example:

update type, id, :doc => {:title => 'new title'}
Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174