I m using tire and mongoid in a rails 4 application.
class Agent
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Taggable
include Tire::Model::Search
include Tire::Model::Callbacks
...
mapping do
indexes :id, index: :not_analyzed
indexes :name, type: 'string', analyzer: 'pattern'
indexes :tags_array, type: 'string', analyzer: 'pattern'
end
...
def self.search(params)
tire.search(load: true) do
query do
string "name:#{params}"
string "tags_array:#{params}"
end
end
end
...
There are 4 agents as
Agent.all.collect(&:tags)
=> ["pune", "pune", "press", "pune press"]
Agent.all.collect(&:name)
=> ["agent smith", "first", "second", "third"]
I have 3 issues as
1) the first problem is that the agent is not searchable by 'name'.
results = Agent.search('first')
=> #<Tire::Results::Collection:0xb26dc4c @response={"took"=>1, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}, @options={:load=>true, :size=>10}, @time=1, @total=0, @facets=nil, @max_score=0.0, @wrapper=Tire::Results::Item>
results.results
=> []
2) the second problem is that the mongoid gives error specifying that the object with specified ids dont exist. If i search based on tag
results = Agent.search('press')
=> #<Tire::Results::Collection:0xb296da4 @response={"took"=>1, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>2, "max_score"=>0.30685282, "hits"=>[{"_index"=>"agents", "_type"=>"agent", "_id"=>"{\"$oid\"=>\"521da715f94adc957d000005\"}", "_score"=>0.30685282, "_source"=>{"name"=>"second", "tags_array"=>["press"]}}, {"_index"=>"agents", "_type"=>"agent", "_id"=>"{\"$oid\"=>\"521da715f94adc957d000004\"}", "_score"=>0.19178301, "_source"=>{"name"=>"third", "tags_array"=>["pune press"]}}]}}, @options={:load=>true, :size=>10}, @time=1, @total=2, @facets=nil, @max_score=0.30685282, @wrapper=Tire::Results::Item>
results.results
Mongoid::Errors::DocumentNotFound:
Problem:
Document(s) not found for class Agent with id(s) {"$oid"=>"521da715f94adc957d000005"}, {"$oid"=>"521da715f94adc957d000004"}.
Summary:
When calling Agent.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): {"$oid"=>"521da715f94adc957d000005"}, {"$oid"=>"521da715f94adc957d000004"} ... (2 total) and the following ids were not found: {"$oid"=>"521da715f94adc957d000005"}, {"$oid"=>"521da715f94adc957d000004"}.
Agent.all.collect(&:id)
=> ["521da715f94adc957d000007", "521da715f94adc957d000006", "521da715f94adc957d000005", "521da715f94adc957d000004"]
3) If i reindex the Agent objects, the ids in the elastic search are entirely different than the mongodb object ids
Agent.index_name
=> "agents"
Tire.index('agents').delete
=> true
Agent.import
=> #<Tire::Model::Import::Strategy::Mongoid:0xb2dad9c @klass=Agent, @options={:per_page=>1000}, @index=#<Tire::Index:0xb2dabe4 @name="agents", @response=#<Tire::HTTP::Response:0xb30c4b4 @body="{\"took\":630,\"items\":[{\"create\":{\"_index\":\"agents\",\"_type\":\"agent\",\"_id\":\"h0k78SupT9GGTT3I6qV3Bw\",\"_version\":1,\"ok\":true}},{\"create\":{\"_index\":\"agents\",\"_type\":\"agent\",\"_id\":\"LuJMwJSFRquezRUc1HUpEg\",\"_version\":1,\"ok\":true}},{\"create\":{\"_index\":\"agents\",\"_type\":\"agent\",\"_id\":\"gE6MreF8T4ePdD8lqutSJQ\",\"_version\":1,\"ok\":true}},{\"create\":{\"_index\":\"agents\",\"_type\":\"agent\",\"_id\":\"4azbinLjSO2LuRXn9-WYtg\",\"_version\":1,\"ok\":true}}]}", @code=200, @headers={:content_type=>"application/json; charset=UTF-8", :content_length=>"426"}>>>
results = Agent.search('press')
=> #<Tire::Results::Collection:0xb31bcac @response={"took"=>5, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>2, "max_score"=>1.0, "hits"=>[{"_index"=>"agents", "_type"=>"agent", "_id"=>"gE6MreF8T4ePdD8lqutSJQ", "_score"=>1.0, "_source"=>{"name"=>"second", "tags_array"=>["press"]}}, {"_index"=>"agents", "_type"=>"agent", "_id"=>"4azbinLjSO2LuRXn9-WYtg", "_score"=>0.19178301, "_source"=>{"name"=>"third", "tags_array"=>["pune press"]}}]}}, @options={:load=>true, :size=>10}, @time=5, @total=2, @facets=nil, @max_score=1.0, @wrapper=Tire::Results::Item>
results.results
=>Mongoid::Errors::DocumentNotFound:
Problem:
Document(s) not found for class Agent with id(s) gE6MreF8T4ePdD8lqutSJQ, 4azbinLjSO2LuRXn9-WYtg.
Have i defined the mapping correctly? the user should be able to search based on agent name or/and tags. partial name should also be allowed.
==UPDATE
mapping from elasticsearch as a result of localhost:9200/_mapping?pretty=1
"agents" : {
"agent" : {
"properties" : {
"id" : {
"type" : "string",
"index" : "not_analyzed",
"omit_norms" : true,
"index_options" : "docs"
},
"name" : {
"type" : "string"
},
"tags_array" : {
"type" : "string"
}
}
}
}
I was searching a solution for the above problems for the last 2 days. Finally, found the solution at https://github.com/karmi/tire/issues/775.
Explanation of the problem and solution
https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md the mongoid 4 changelog specifies that
Tire expected an array of ids but got an hash as ids: {"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000007"} and tried to find the object as
Below code works:
the temporary hack: add a initializer with below content