1
votes

I am using Tire for implementing Elasticsearch. My environment is Linux,Rails 3.2 with Ruby 1.9.3. I am able to index using Elasticsearch with the help of gem Tire(0.5.8). But now to improve performance i want to get the data to be displayed from indexed data and not hit Database(i want to use db for INSERT and UPDATE only).

To achieve this i am using below code:

result = Tire.search 'jobs', load: true do

  query do
    string "is_active:1"
  end
  filter :range,  status: {gte: 0 }
  sort { by :created_at, 'desc' }
 end

  return result.results

Since i am using load:true it hits database(MySQL) which i do not want.But i am removing the load attribute then it gives me no result. I am able to check that all of the columns of mentioned table is indexed("xxx:9200/jobs/_mapping?pretty=1").

Please suggest how can i achieve it.

Thanks.

2
What does just result.inspect return?concept47
Thanks @concept47 for response.Now i am able to get the elastic model in place of ActiveRecord. The result.inspect returned is as mentioned below: #<Tire::Search::Search:0x8a1a4d4 @indices=[\"jobs\"], @types=[], @options={:load=>false, :page=>1, :per_page=>20}, @path=\"/jobs/_search\", @query=#<Tire::Search::Query:0x8813d98 @value={:query_string=>{:query=>\"is_active:1\"}}>, @filters=[{:range=>{:status=>{:gte=>0}}}], @sort={:created_at=>\"desc\"}] Now,how can achieve associations to replace eager loading code.Orangescrum

2 Answers

0
votes

The load: true will make the call to your database for each returned result. Without it, your results are these Item objects that act like instances of your model.

What you'll need to do is make sure you have your mapping set for your models, then make sure you import your indexes.

class User
    include Mongoid::Document
    field :name, type: String
    mapping do
        indexes :name
    end
 end

Then after your model is set with the indexes you'll run rake tire:import CLASS=User That will index your user class. After you would do

search = Tire.search(User.index_name) do
    query { string 'billy' }
end
puts search.results.first.name

Lots of good docs here https://github.com/karmi/tire

0
votes
load: true

will refer to database for associations. You can add the associations in the mapping to save it in elasticsearch.