0

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

class Thing
  include Tire::Model::Callbacks
  include Tire::Model::Persistence

  index_name { "#{Rails.env}-thing" }

  property :title, :type => :string
  property :tags, :default => [], :analyzer => 'keyword', :class => [Tag], :type => :nested
end

The nested query looks like

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

When I execute the query I get a "not of nested type" error

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}

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?

UPDATE

Following Karmi's answer below, I recreated the index and verified that the mapping is correct:

thing: {
  properties: {
    tags: {
      properties: {
        name: {
          type: string
        }
        type: nested
      }
    }
    title: {
      type: string
    }
   }

However, when I add new Tags to Thing

thing = Thing.new
thing.title = "Title"
thing.tags << {:name => 'Tag'}
thing.save

The mapping reverts to "dynamic" type and "nested" is lost.

thing: {
  properties: {
    tags: {
      properties: {
        name: {
          type: string
        }
        type: "dynamic"
      }
    }
    title: {
      type: string
    }
   }

The query fails with the same error as before. How do I preserve the nested type when adding new Tags?

1 Answer 1

1

Yes, indeed, the mapping configuration in property declarations is passed on in the Persistence integration.

In a situation like this, there's always the and and only first question: how does the mapping look like for real?

So, use eg. the Thing.index.mapping method or the Elasticsearch's REST API: curl localhost:9200/things/_mapping to have a look.

Chances are, that your index was created with the dynamic mapping, based on the JSON you have used, and you have changed the mapping later. In this case, the index creation logic is skipped, and the mapping is not what you expect.


There's a Tire issue opened about displaying warning when the index mapping is different from the mapping defined in the model.

2
  • thank you very much for your help, and an excellent library. I'm still having issues preserving the nested document type when adding new tags. I've updated the question with details, can you shed any light? Jan 14, 2013 at 11:47
  • I got this working, thank you. I had to call Thing.create_elasticsearch_index in the setup of each test to make sure that the mapping was properly created. Jan 14, 2013 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.