我正在尝试做一些我将用作过滤器的聚合,但是我在搜索我正在聚合的字段时遇到问题。这是我的映射:
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
到目前为止,我能够正确显示我的聚合并搜索除“course_name”之外的所有字段。另一个聚合字段是可搜索的(occasion_name)。我想在查询中添加其他嵌套字段,我尝试添加它们但是再次,可搜索的唯一嵌套字段是occasion_name,所有其他嵌套字段都不是。我查了几本我参考的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"]}}]}}
非常感谢任何帮助!
答案 0 :(得分:0)
我终于找到了什么错误...在我的映射中,我指定每个嵌套字段的名称都已编入索引但“index :: not_analyzed”。这是必需的,因为它们包含多个单词,当我提取要在我的视图中显示的名称时(通过groovy脚本),如果我没有指定'not_analyzed',ES将通过创建一个数组来使该名称的每个单词都可搜索名称和单词将是随机顺序,所以我不能轻易地重建名称...但是,如果我搜索“圣诞节”而不是“圣诞节”时不会分析索引,ES会发现因为它比较字符串而注意到1:1,这就是为什么我的搜索无法找到任何内容,因为我没有指定我正在搜索的内容与数据库中的完全相同。
@rahulroc,谢谢你的帮助!