Note that there are some explanatory texts on larger screens.

plurals
  1. POElasticsearch/tire Nested Queries with persistant objects
    primarykey
    data
    text
    <p>I'm trying to use Tire to perform a nested query on a persisted model. The model (Thing) has Tags and I'm looking to find all Things tagged with a certain Tag</p> <pre><code>class Thing include Tire::Model::Callbacks include Tire::Model::Persistence index_name { "#{Rails.env}-thing" } property :title, :type =&gt; :string property :tags, :default =&gt; [], :analyzer =&gt; 'keyword', :class =&gt; [Tag], :type =&gt; :nested end </code></pre> <p>The nested query looks like</p> <pre><code>class Thing def self.find_all_by_tag(tag_name, args) self.search(args) do query do nested path: 'tags' do query do boolean do must { match 'tags.name', tag_name } end end end end end end end </code></pre> <p>When I execute the query I get a "not of nested type" error </p> <pre><code>Parse Failure [Failed to parse source [{\"query\":{\"nested\":{\"query\":{\"bool\":{\"must\":[{\"match\":{\"tags.name\":{\"query\":\"TestTag\"}}}]}},\"path\":\"tags\"}},\"size\":10,\"from\":0,\"version\":true}]]]; nested: QueryParsingException[[test-thing] [nested] nested object under path [tags] is not of nested type]; }]","status":500} </code></pre> <p>Looking at the source for Tire it seems that mappings are created from the options passed to the "property" method, so I don't think I need a separate "mapping" block in the class. Can anyone see what I am doing wrong?</p> <p><strong>UPDATE</strong></p> <p>Following Karmi's answer below, I recreated the index and verified that the mapping is correct:</p> <pre><code>thing: { properties: { tags: { properties: { name: { type: string } type: nested } } title: { type: string } } </code></pre> <p>However, when I add new Tags to Thing</p> <pre><code>thing = Thing.new thing.title = "Title" thing.tags &lt;&lt; {:name =&gt; 'Tag'} thing.save </code></pre> <p>The mapping reverts to "dynamic" type and "nested" is lost. </p> <pre><code>thing: { properties: { tags: { properties: { name: { type: string } type: "dynamic" } } title: { type: string } } </code></pre> <p>The query fails with the same error as before. How do I preserve the nested type when adding new Tags? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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