3

我偶然发现了一个关于使用 ElasticSearch 和轮胎的模型(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!!!!

我错过了什么?

4

1 回答 1

2

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

查看Elasticsearch、Tire 和 Nested queries/association with ActiveRecord答案,其中包含完整的演练。

于 2013-02-27T07:46:23.520 回答