【发布时间】:2015-07-23 16:25:53
【问题描述】:
我将 elasticsearch-model gem 用于具有has_many 关系的模型。为了匹配文档,假设模型是Article,关系是has_many categories。所以我将客户序列化程序编写如下(直接来自文档):
def as_indexed_json(options={})
self.as_json(
include: { categories: { only: :title},
})
end
序列化似乎有效。示例文章的 as_indexed_json 的结果包含一个 "categories" => {"title"=> "one", "title"=> "two", "title"=> "three"} 块。
我正在苦苦挣扎并且无法在文档中找到的是如何搜索这个嵌套字段。
这是我尝试过的:
从elasticsearch documentation on nested query 我想它应该是这样的:
r = Article.search query: {
nested: {
path: "categories",
query: {match: {title: "one"}}
}
}
但是当我执行r.results.first 时,我得到一个错误:nested object under path [categories] is not of nested type]...
我尝试添加将序列化程序中的一行更改为:include: { categories: { type: "nested", only: :title},但这并没有改变任何东西,它仍然说类别不是嵌套类型。
当然,我也试过像这样只查询没有任何嵌套的字段:
r = Article.search query: {match: {categories: 'one'}}
但这不会返回任何结果。
全文搜索如下:
r = Article.search query: {match: {_all: 'one'}}
返回结果,但我当然只想在类别字段中搜索“一个”。
任何帮助将不胜感激!
【问题讨论】:
标签: ruby-on-rails elasticsearch elasticsearch-model