我遇到了一个关于使用 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
这是 Elasticsearch 上可用的映射模式:
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!!!!
我错过了什么?
最佳答案
验证关联是否包含在 ElasticSearchTest.new(...).to_indexed_json
的输出中。
看看 Elasticsearch, Tire, and Nested queries / associations with ActiveRecord包含完整演练的答案。
关于ruby-on-rails - ElasticSearch:如何正确映射关联以便它们也可搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15089300/