ElasticSearch:如何正确映射关联以便它们也可以搜索?

时间:2013-02-26 12:36:12

标签: ruby-on-rails indexing elasticsearch tire

我偶然发现了一个关于使用ElasticSearch和Tire进行模型索引映射(ActiveRecord)的问题。我正在使用他们在文档中讨论的相同系统来映射关联字段。映射似乎是正确的,但我不能在那里搜索东西,显然:

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

这是弹性搜索可用的映射模式:

 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"
        }
      }
   }
}

似乎很好。这些是我的例子:

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!!!!

我错过了什么?

1 个答案:

答案 0 :(得分:2)

验证关联是否包含在ElasticSearchTest.new(...).to_indexed_json的输出中。

查看包含完整漫游的Elasticsearch, Tire, and Nested queries / associations with ActiveRecord答案。