Ruby on rails 4 搜索多个嵌套字段不起作用(ES+;ROR)
我正在尝试做一些聚合,我将使用这些聚合作为过滤器,但我在搜索我正在聚合的字段时遇到问题。这是我的映射:Ruby on rails 4 搜索多个嵌套字段不起作用(ES+;ROR),ruby-on-rails-4,elasticsearch,Ruby On Rails 4,elasticsearch,我正在尝试做一些聚合,我将使用这些聚合作为过滤器,但我在搜索我正在聚合的字段时遇到问题。这是我的映射: settings :index => { :number_of_shards => 1 } do mapping do indexes :id, index: :not_analyzed indexes :name indexes :summary indexes :description indexes :
settings :index => { :number_of_shards => 1 } do
mapping do
indexes :id, index: :not_analyzed
indexes :name
indexes :summary
indexes :description
indexes :occasions, type: 'nested' do
indexes :id, type: 'integer'
indexes :occasion_name, type: 'string', index: :not_analyzed
end
indexes :courses, type: 'nested' do
indexes :id, type: 'integer'
indexes :course_name, type: 'string', index: :not_analyzed
end
#
indexes :allergens, type: 'nested' do
indexes :id, type: 'integer'
indexes :allergen_name, type: 'string', index: :not_analyzed
end
#
indexes :cookingtechniques, type: 'nested' do
indexes :id, type: 'integer'
indexes :name, type: 'string', index: :not_analyzed
end
indexes :cuisine, type: 'nested' do
indexes :id, type: 'integer'
indexes :cuisine_name, type: 'string', index: :not_analyzed
end
end
end
这就是我定义查询的地方:
def multi_match_query(query)
{
bool: {
should: [
{
nested:{
path: 'occasions',
query: {
multi_match:
{
query: query,
type: "best_fields",
fields: ["occasions.occasion_name"]
}
}
}
},
{
nested:{
path:'courses',
query: {
multi_match:
{
query: query,
type: "best_fields",
fields: ["course_name"]
}
}
}
},
{
multi_match: {
query: query,
fields:["name^9", "summary^8", "cuisine_name^7", "description^6"],
}
}
]
}
}
end
到目前为止,我能够正确显示我的聚合和搜索所有字段,但“课程名称”除外。另一个聚合字段是可搜索的(名称)。我还想在查询中添加其他嵌套字段,并尝试添加它们,但同样,唯一可搜索的嵌套字段是名称,所有其他嵌套字段都不可搜索。我查阅了几本ES参考书,但没有一本像我正在尝试的查询那样让我觉得查询中不能有多个嵌套字段
编辑:这是我的json映射:
{
"recipes":{
"mappings":{
"recipe":{
"properties":{
"allergens":{
"type":"nested",
"properties":{
"allergen_name":{
"type":"string",
"index":"not_analyzed"
},
"id":{
"type":"integer"
}
}
},
"cookingtechniques":{
"type":"nested",
"properties":{
"id":{
"type":"integer"
},
"name":{
"type":"string",
"index":"not_analyzed"
}
}
},
"courses":{
"type":"nested",
"properties":{
"course_name":{
"type":"string",
"index":"not_analyzed"
},
"id":{
"type":"integer"
}
}
},
"cuisine":{
"type":"nested",
"properties":{
"cuisine_name":{
"type":"string",
"index":"not_analyzed"
},
"id":{
"type":"integer"
}
}
},
"description":{
"type":"string"
},
"id":{
"type":"string",
"index":"not_analyzed"
},
"name":{
"type":"string"
},
"occasions":{
"type":"nested",
"properties":{
"id":{
"type":"integer"
},
"occasion_name":{
"type":"string",
"index":"not_analyzed"
}
}
},
"summary":{
"type":"string"
}
}
}
}
}
}
来自RoR的搜索请求的查询:
{:query=>
{:bool=>
{:should=>
[{:nested=>{:path=>"occasions", :query=>{:multi_match=>{:query=>"salad", :type=>"best_fields", :fields=>["occasions.occasion_name"]}}}},
{:nested=>{:path=>"courses", :query=>{:multi_match=>{:query=>"salad", :type=>"best_fields", :fields=>["courses.course_name"]}}}},
{:multi_match=>{:query=>"salad", :fields=>["name^9", "summary^8", "cuisine_name^7", "description^6"]}}]}}
任何帮助都将不胜感激 我终于找到了问题所在……在我的映射中,我指定每个嵌套字段的名称都被索引,但是“index::not_analysis”。这是必需的,因为它们包含多个单词,当我提取要在视图中显示的名称时(通过groovy脚本),如果我不指定“not_analysis”,ES将通过生成名称数组使名称中的每个单词都可以搜索,这些单词将以随机顺序排列,因此我无法轻松地重建名称。。。但是,如果搜索“christmas”而不是“christmas”,则不会对索引进行分析。在比较字符串1:1时,ES将发现Nothing,这就是我的搜索无法找到任何内容的原因,因为我没有像在DB中一样指定要搜索的内容
@拉胡洛克,谢谢你的帮助 它还没有解决?你能分享你的地图吗?我怀疑课程不是嵌套字段。curl-XGET'localhost:9200//_mapping/'我用映射json更新了我的帖子。再次感谢您的帮助,非常感谢!谢谢映射很好。你有没有试过用这个领域的绝对路径。i、 “课程。课程名称”而不是“课程名称”?是的,我试过了。我用RoR的搜索请求中的查询更新了我的帖子。可以像我的查询那样搜索多个嵌套字段吗?如果是的话,我会知道去别的地方寻找问题。