弹性搜索嵌套 [英] Elastic Search nested
问题描述
我正在通过轮胎宝石进行弹性搜索.
I'm using Elastic search through tire gem.
给出此结构以索引我的资源模型
Given this structure to index my resource model
mapping do
indexes :_id
indexes :version, analyzer: 'snowball', boost: 100
indexes :resource_files do
indexes :_id
indexes :name, analyzer: 'snowball', boost: 100
indexes :resource_file_category do
indexes :_id
indexes :name, analyzer: 'snowball', boost: 100
end
end
end
如何检索具有具有给定resource_file_category ID的resource_files的所有资源?
How can i retrieve all the resources that have resource_files with a given resource_file_category id?
我看过弹性搜索文档,我认为可能是使用has子过滤器 http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html
i've looked in the elastic search docs and i think could be using the has child filter http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html
我已经尝试过这种方式
filter :has_child, :type => 'resource_files', :query => {:filter => {:has_child => {:type => 'resource_file_category', :query => {:filter => {:term => {'_id' => params[:resource_file_category_id]}}}}}}
但是我不确定是否可以/有效地制作一个嵌套的has_child过滤器",或者是否有更好/更简单的方法来做到这一点...欢迎任何建议;)
but i'm not sure if is possible/valid to make a "nested has_child filter" or if is there a better/simpler way to do this... any advice is welcome ;)
推荐答案
恐怕我不知道您的映射定义是什么意思.如果您只发布以下输出,则更容易阅读:
I'm afraid I don't know what your mapping definition means. It'd be easier to read if you just posted the output of:
curl -XGET 'http://127.0.0.1:9200/YOUR_INDEX/_mapping?pretty=1'
但是您可能想要这样的东西:
But you probably want something like this:
curl -XGET 'http://127.0.0.1:9200/YOUR_INDEX/YOUR_TYPE/_search?pretty=1' -d '
{
"query" : {
"term" : {
"resource_files.resource_file_catagory._id" : "YOUR VALUE"
}
}
}
'
注意: _id
字段可能应映射为 {"index":"not_analyzed"}
,以便不进行分析,而是存储准确的价值.否则,如果您对'FOO BAR'
进行 term
查询,则不会找到该文档,因为存储的实际术语为: ['foo','bar']
Note: The _id
fields should probably be mapped as {"index": "not_analyzed"}
so that they don't get analyzed, but instead store the exact value. Otherwise if you do a term
query for 'FOO BAR'
the doc won't be found, because the actual terms that are stored are: ['foo','bar']
注意:
Note: The has_child
query is used to search for parent docs who have child docs (ie docs which specify a parent type and ID) that match certain search criteria.
这篇关于弹性搜索嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!