mongoid returns document not found even if its present + tire + mongoid -


i m using tire , mongoid in rails 4 application.

class agent   include mongoid::document   include mongoid::timestamps   include mongoid::taggable   include tire::model::search   include tire::model::callbacks   ...   mapping     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)       query         string "name:#{params}"         string "tags_array:#{params}"       end     end   end   ... 

there 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) first problem agent not searchable '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) second problem mongoid gives error specifying object specified ids dont exist. if 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 class agent id(s) {"$oid"=>"521da715f94adc957d000005"}, {"$oid"=>"521da715f94adc957d000004"}.   summary:     when calling agent.find id or array of ids, each parameter must match document in database or error raised. search id(s): {"$oid"=>"521da715f94adc957d000005"}, {"$oid"=>"521da715f94adc957d000004"} ... (2 total) , following ids not found: {"$oid"=>"521da715f94adc957d000005"}, {"$oid"=>"521da715f94adc957d000004"}.  agent.all.collect(&:id)    => ["521da715f94adc957d000007", "521da715f94adc957d000006", "521da715f94adc957d000005", "521da715f94adc957d000004"]  

3) if reindex agent objects, ids in elastic search entirely different 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 class agent id(s) ge6mref8t4epdd8lqutsjq, 4azbinljso2lurxn9-wytg. 

have defined mapping correctly? user should able search based on agent name or/and tags. partial name should allowed.

==update

mapping elasticsearch 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 searching solution above problems last 2 days. finally, found solution @ https://github.com/karmi/tire/issues/775.

explanation of problem , solution

> agent.all.collect(&:name)   => ["first", "second", "third", "agent smith"]   > agent.all.collect(&:id)   => ["52204be3f94adc86b2000007", "52204be3f94adc86b2000006", "52204be3f94adc86b2000005", "52204be3f94adc86b2000004"]   > agent.all.collect(&:tags)   => ["press pune", "press", "pune", "matrix"]   $ = agent.search('pu') => #<tire::results::collection:0xbb24cf0 @response={"took"=>1, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>2, "max_score"=>0.5, "hits"=>[{"_index"=>"agents", "_type"=>"agent", "_id"=>"{\"$oid\"=>\"52204be3f94adc86b2000005\"}", "_score"=>0.5, "_source"=>{"name"=>"third", "tags_array"=>["pune"]}}, {"_index"=>"agents", "_type"=>"agent", "_id"=>"{\"$oid\"=>\"52204be3f94adc86b2000007\"}", "_score"=>0.5, "_source"=>{"name"=>"first", "tags_array"=>["press pune"]}}]}}, @options={:load=>true, :size=>10}, @time=1, @total=2, @facets=nil, @max_score=0.5, @wrapper=tire::results::item>   2 results present above search.  $ a.results =>   mongoid::errors::documentnotfound:  problem:  document(s) not found class agent id(s) {"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000007"}.   summary:    when calling agent.find id or array of ids, each parameter must match document in database or error raised. search id(s): {"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000007"} ... (2 total) , following ids not found: {"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000007"}.  resolution:    search id in database or set mongoid.raise_not_found_error configuration option false, cause nil returned instead of raising error when searching single id, or matched documents when searching multiples.     .... /home/prasad/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-85e146637503/lib/mongoid/findable.rb:88:in `find' /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:156:in `__find_records_by_ids'     .... /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:144:in `block in __get_results_with_load' /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:131:in `each' /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:131:in `__get_results_with_load' /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:27:in `results' (irb):2   # method used in tire retrieve objects based on id(s). id or array of ids.  def __find_records_by_ids(klass, ids)   @options[:load] === true ? klass.find(ids) : klass.find(ids, @options[:load]) end 

https://github.com/mongoid/mongoid/blob/master/changelog.md mongoid 4 changelog specifies that

#2497 calling to_json no longer tampers return value driver, , proper returns{ "$oid" : object_id.to_s } instead of string representation 

tire expected array of ids got hash ids: {"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000007"} , tried find object

agent.find({"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000007"}) #and hence error.  

below code works:

agent.find("52204be3f94adc86b2000004", "52204be3f94adc86b2000007")    agent.find(["52204be3f94adc86b2000004", "52204be3f94adc86b2000007"])  agent.all.collect(&:name).as_json  => ["first", "second", "third", "agent smith"]   agent.all.collect(&:id).as_json  => [{"$oid"=>"52204be3f94adc86b2000007"}, {"$oid"=>"52204be3f94adc86b2000006"}, {"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000004"}]  

the temporary hack: add initializer below content

require "tire" module tire  class index    def get_id_from_document(document) #document object. ie agent object in above case      case         when document.is_a?(hash)          document[:_id] || document['_id'] || document[:id] || document['id']       #if not hash , responds 'id' , object_id not equal id "agent.last.object_id => 96643940"        when document.respond_to?(:id) && document.id != document.object_id          document.id.to_s   # document.id.as_json        end      end    end end 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -