ChuckE
ChuckE

Reputation: 5688

ElasticSearch: How to map associations properly so that they are also searchable?

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?

Upvotes: 3

Views: 2446

Answers (1)

karmi
karmi

Reputation: 14419

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.

Upvotes: 2

Related Questions