3

I've stumbled into an issue concerning the index mapping of models (ActiveRecord) using ElasticSearch and Tire. I'm using the same system they talk about in the documentation to map association fields. The mapping seems right, but I can't search for stuff there, apparently:

class ElasticSearchTest < ActiveRecord::Base
  belongs_to :elastic_search_belongs_to_test

  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :title
    indexes :body
    indexes :elastic_search_belongs_to_test do
      indexes :title
      indexes :body
    end
  end

this is the mapping schema available on elastic search:

 curl http://localhost:9200/elastic_search_tests/elastic_search_test_mapping?pretty=1

 >> {
   "elastic_search_test" : {
     "properties" : {
       "body" : {
         "type" : "string"
        },
        "elastic_search_belongs_to_test" : {
          "properties" : {
             "body" : {
               "type" : "string"
             },
             "title" : {
               "type" : "string"
             }
          }
        },
        "title" : {
          "type" : "string"
        }
      }
   }
}

seems good. These are my examples:

t1 = ElasticSearchTest.create title: "title1", body: "body1",
                              elastic_search_belongs_to_test: ElasticSearchBelongsToTest.new(title: "title2", body: "body2"))

ElasticSearchTest.index.refresh
ElasticSearchTest.search("title1") #=> returns t1 in results
ElasticSearchTest.search("title2") #=> does not return t1 in results!!!!

What am I missing?

ChuckE
  • 5,610
  • 4
  • 31
  • 59

1 Answers1

2

Verify that the associations are included in the output of ElasticSearchTest.new(...).to_indexed_json.

Have a look at the Elasticsearch, Tire, and Nested queries / associations with ActiveRecord answer which contains a full walktrough.

Community
  • 1
  • 1
karmi
  • 14,059
  • 3
  • 33
  • 41
  • shouldn't these be automatically added based on the assumption that, if I defined these in the index mapping, then I also want to write them? – ChuckE Feb 27 '13 at 09:23
  • Yes, the default `to_indexed_json` method should take them into account. – karmi Feb 28 '13 at 06:55
  • what does it mean if it is not? Because that's exactly what happened. Is there a specific case where this doesn't happen? – ChuckE Feb 28 '13 at 09:07