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

标签 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

这是 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/

相关文章:

lucene - 通过id的Elasticsearch GET返回所有STORED字段

ruby-on-rails - 如何让 Unicorn 在开发日志中显示 post 参数?

ruby-on-rails - rails : How to render a layout around a collection?

ruby-on-rails - 无法在 Chrome 中预览音频/vnd.wave

python - 选择 numpy 数组中特定维度的索引

python - Elasticsearch Python - 索引分析器和搜索分析器

ruby-on-rails - 如何按多个字符串降序对 Ruby 二维数组进行排序?

mysql - 使用索引导致 MySQL 查询无限时间卡住

python - 使用 numpy 将 bool 数组索引到多维数组中

elasticsearch - Vagrant 和 Elasticsearch 的端口问题