Note that there are some explanatory texts on larger screens.

plurals
  1. POmongoid returns document not found even if its present + tire + mongoid
    text
    copied!<p>I m using tire and mongoid in a rails 4 application. </p> <pre><code>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 ... </code></pre> <p>There are 4 agents as</p> <pre><code>Agent.all.collect(&amp;:tags) =&gt; ["pune", "pune", "press", "pune press"] Agent.all.collect(&amp;:name) =&gt; ["agent smith", "first", "second", "third"] </code></pre> <p>I have 3 issues as</p> <p>1) the first problem is that the agent is not searchable by 'name'.</p> <pre><code>results = Agent.search('first') =&gt; #&lt;Tire::Results::Collection:0xb26dc4c @response={"took"=&gt;1, "timed_out"=&gt;false, "_shards"=&gt;{"total"=&gt;5, "successful"=&gt;5, "failed"=&gt;0}, "hits"=&gt;{"total"=&gt;0, "max_score"=&gt;nil, "hits"=&gt;[]}}, @options={:load=&gt;true, :size=&gt;10}, @time=1, @total=0, @facets=nil, @max_score=0.0, @wrapper=Tire::Results::Item&gt; results.results =&gt; [] </code></pre> <p>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 </p> <pre><code>results = Agent.search('press') =&gt; #&lt;Tire::Results::Collection:0xb296da4 @response={"took"=&gt;1, "timed_out"=&gt;false, "_shards"=&gt;{"total"=&gt;5, "successful"=&gt;5, "failed"=&gt;0}, "hits"=&gt;{"total"=&gt;2, "max_score"=&gt;0.30685282, "hits"=&gt;[{"_index"=&gt;"agents", "_type"=&gt;"agent", "_id"=&gt;"{\"$oid\"=&gt;\"521da715f94adc957d000005\"}", "_score"=&gt;0.30685282, "_source"=&gt;{"name"=&gt;"second", "tags_array"=&gt;["press"]}}, {"_index"=&gt;"agents", "_type"=&gt;"agent", "_id"=&gt;"{\"$oid\"=&gt;\"521da715f94adc957d000004\"}", "_score"=&gt;0.19178301, "_source"=&gt;{"name"=&gt;"third", "tags_array"=&gt;["pune press"]}}]}}, @options={:load=&gt;true, :size=&gt;10}, @time=1, @total=2, @facets=nil, @max_score=0.30685282, @wrapper=Tire::Results::Item&gt; results.results Mongoid::Errors::DocumentNotFound: Problem: Document(s) not found for class Agent with id(s) {"$oid"=&gt;"521da715f94adc957d000005"}, {"$oid"=&gt;"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"=&gt;"521da715f94adc957d000005"}, {"$oid"=&gt;"521da715f94adc957d000004"} ... (2 total) and the following ids were not found: {"$oid"=&gt;"521da715f94adc957d000005"}, {"$oid"=&gt;"521da715f94adc957d000004"}. Agent.all.collect(&amp;:id) =&gt; ["521da715f94adc957d000007", "521da715f94adc957d000006", "521da715f94adc957d000005", "521da715f94adc957d000004"] </code></pre> <p>3) If i reindex the Agent objects, the ids in the elastic search are entirely different than the mongodb object ids</p> <pre><code>Agent.index_name =&gt; "agents" Tire.index('agents').delete =&gt; true Agent.import =&gt; #&lt;Tire::Model::Import::Strategy::Mongoid:0xb2dad9c @klass=Agent, @options={:per_page=&gt;1000}, @index=#&lt;Tire::Index:0xb2dabe4 @name="agents", @response=#&lt;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=&gt;"application/json; charset=UTF-8", :content_length=&gt;"426"}&gt;&gt;&gt; results = Agent.search('press') =&gt; #&lt;Tire::Results::Collection:0xb31bcac @response={"took"=&gt;5, "timed_out"=&gt;false, "_shards"=&gt;{"total"=&gt;5, "successful"=&gt;5, "failed"=&gt;0}, "hits"=&gt;{"total"=&gt;2, "max_score"=&gt;1.0, "hits"=&gt;[{"_index"=&gt;"agents", "_type"=&gt;"agent", "_id"=&gt;"gE6MreF8T4ePdD8lqutSJQ", "_score"=&gt;1.0, "_source"=&gt;{"name"=&gt;"second", "tags_array"=&gt;["press"]}}, {"_index"=&gt;"agents", "_type"=&gt;"agent", "_id"=&gt;"4azbinLjSO2LuRXn9-WYtg", "_score"=&gt;0.19178301, "_source"=&gt;{"name"=&gt;"third", "tags_array"=&gt;["pune press"]}}]}}, @options={:load=&gt;true, :size=&gt;10}, @time=5, @total=2, @facets=nil, @max_score=1.0, @wrapper=Tire::Results::Item&gt; results.results =&gt;Mongoid::Errors::DocumentNotFound: Problem: Document(s) not found for class Agent with id(s) gE6MreF8T4ePdD8lqutSJQ, 4azbinLjSO2LuRXn9-WYtg. </code></pre> <p>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.</p> <p>==UPDATE</p> <p>mapping from elasticsearch as a result of localhost:9200/_mapping?pretty=1</p> <pre><code>"agents" : { "agent" : { "properties" : { "id" : { "type" : "string", "index" : "not_analyzed", "omit_norms" : true, "index_options" : "docs" }, "name" : { "type" : "string" }, "tags_array" : { "type" : "string" } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload