【问题标题】:ElasticSearch: How to map associations properly so that they are also searchable?ElasticSearch:如何正确映射关联以便它们也可搜索?
【发布时间】:2013-02-11 21:57:18
【问题描述】:

我偶然发现了一个关于使用 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!!!!

我错过了什么?

【问题讨论】:

    标签: ruby-on-rails indexing elasticsearch tire


    【解决方案1】:

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

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

    【讨论】:

    • 不应该自动添加这些假设,如果我在索引映射中定义了这些,那么我也想写它们吗?
    • 是的,默认的to_indexed_json 方法应该考虑到它们。
    • 如果不是是什么意思?因为这正是发生的事情。有没有这种情况不会发生的具体情况?
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2011-11-16
    • 2014-11-05
    相关资源
    最近更新 更多