Question

How can I use indexing on elastic search when there is no active record? but model.rb specify just like this

class Article
  MIN_TERM_CHARS = 2
  MAX_NGRAM_CHARS = 20

  include SyncAttr
  include Tire::Model::Persistence


  index_name { Thread.current[:index_name] || "test" }
    document_type "document"

   # TODO: set index_options to doc for some fields for optimization

   # TODO: maybe use simple analyzer for 'id' fieldsart
  property :artikelnummer, type: 'string', index: 'not_analyzed'
  property :eannummer, type: 'string', index: 'not_analyzed'

  property :bezeichnung, type: 'multi_field', fields: {
    bezeichnung: {type: 'string'},
    ngram: {:type => 'string', :index_analyzer => 'ngram_index_analyzer', :search_analyzer => 'ngram_search_analyzer'},
    suggest: {:type => 'string', :analyzer => 'suggest_analyzer'}
  }

I want to index my data. How can I achieve this? I am using tire/karmi for this

Was it helpful?

Solution

The karmi/tire gem has been (re)tired:

It has been retired in September 2013, since a new library, https://github.com/elasticsearch/elasticsearch-ruby, has been released at that time.

The new elasticsearch Ruby gem for Elasticsearch doesn't support all the high-fructose high-level DSL of Tire, or the automatic compatibility of search results with Rails' helpers such as url_for or rich integration for your models.

Look at elasticsearch-ruby for a more current ruby wrapper to elastic search API.

Edit

Just to clarify - tire is still usable, but its author recommends that you move to the new gem:

If you're using Tire in a project, or contemplate to start using it, these are the important points:

  1. The library will continue to work as it did until now. In fact, it will certainly work better, since bugs will be fixed and important features will be added.

  2. All the URLs will continue to work, and be properly redirected by the browser, Git or Bundler. There is no need to change them unless you want to.

  3. The project has been renamed to increase the visibility of this migration, not just for fun.

Basic API usage of the new gem (from the documentation):

Usage with the elasticsearch gem

When you use the client from the elasticsearch-ruby package, the library modules have been already included, so you just call the API methods:

require 'elasticsearch'

client = Elasticsearch::Client.new log: true

client.index  index: 'myindex', type: 'mytype', id: 1, body: { title: 'Test' }
# => {"_index"=>"myindex", ... "created"=>true}

client.search index: 'myindex', body: { query: { match: { title: 'test' } } }
# => {"took"=>2, ..., "hits"=>{"total":5, ...}}

OTHER TIPS

To index non model-backed objects, you can use the elasticsearch-ruby API (with the client.create syntax), as referenced by Uri. Elasticsearch-ruby is just a Ruby connector to Elasticsearch, so anything you can do with a regular curl request, you can do with the API, including setting up custom mappings, adding documents, and performing searches. I'm not sure if there's a way to index non model-backed objects using Tire, but I know that Tire can search any index, including indices not set-up by Tire (such as custom indices that you've set up using elasticsearch-ruby).

You can also use Tire to index model-backed objects while bypassing ActiveRecord completely (rather than using ActiveRecord callbacks) by including the Tire::Persistence module. To get it to work:

  • Include the Tire::Persistence module in your model classes (as you've done).

  • Disable ActiveRecord in config/application.rb, replacing the line require 'rails/all' with:

require "rails" [ # 'active_record', 'action_controller', 'action_mailer', 'active_resource', 'rails/test_unit', ].each do |framework| begin require "#{framework}/railtie" rescue LoadError end end

  • Delete config/database.yml

  • remove 'gem sqlite3', or whatever database gem you've used, from the Gemfile.

  • Run bundle install

See this blog post for more details, and this stackoverflow question with a complete example of a working model that uses Elasticsearch to index objects rather than ActiveRecord.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top