ElasticSearch 仅在嵌套存在时查询
ElasticSearch only query nested if it exists
我有这个搜索查询来查找根标题和描述中的查询搜索词 "red dog",并且还匹配嵌套的评论文档。
GET /_all/video/_search
{
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"red dog",
"fields":[
"Title",
"Description"
],
"type": "cross_fields",
"operator":"and"
}
},
{
"nested":{
"path":"Comments",
"query":{
"multi_match":{
"query":"red dog",
"fields":[
"Comments.Description",
"Comments.Description.folded"
],
"type": "cross_fields",
"operator":"and"
}
}
}
}
]
}
}
}
对我来说不幸的是,当我将评论保存到 ElasticSearch 时,评论有时为空,是否可以执行某种 "include if document exists" 条件?
更新
我仍然遇到同样的错误
[nested] failed to find nested object under path [Comments]
当我尝试使用 exists 进行查询时
GET /_all/video/_search
{
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"lisa",
"fields":[
"Title",
"Description"
],
"type":"cross_fields",
"operator":"and"
}
},
{
"nested":{
"path":"Comments",
"query":{
"filtered":{
"query":{
"match_all":{}
},
"filter":{
"exists":{
"field":"Comments.Description"
}
}
}
}
}
}
]
}
}
}
我的所有地图
{
"en":{
"mappings":{
"video":{
"properties":{
"Comments":{
"type":"nested",
"properties":{
"Description":{
"type":"string",
"analyzer":"english",
"fields":{
"folded":{
"type":"string",
"analyzer":"folding"
}
}
}
}
},
"Description":{
"type":"string",
"analyzer":"english",
"fields":{
"folded":{
"type":"string",
"analyzer":"folding"
}
}
},
"Title":{
"type":"string",
"analyzer":"english",
"fields":{
"folded":{
"type":"string",
"analyzer":"folding"
}
}
}
}
}
}
}
}
还有我的设置
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
}
}
原来我有一个 marvel 索引,它没有我输入的映射。我删除了插件,它在所有索引中搜索都很好。
最简单的方法是 "denormalize" 您的数据,这样您就有一个 属性 包含计数和一个布尔值(如果它存在或不存在)。然后你就可以搜索这些属性了。
例如:
{
"id": 31939,
"hasAttachments": true,
"attachmentCount": 2,
"attachments": [
{
"type": "Attachment",
"name": "txt.txt",
"mimeType": "text/plain"
},
{
"type": "Inline",
"name": "jpg.jpg",
"mimeType": "image/jpeg"
}
]
}
我有这个搜索查询来查找根标题和描述中的查询搜索词 "red dog",并且还匹配嵌套的评论文档。
GET /_all/video/_search
{
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"red dog",
"fields":[
"Title",
"Description"
],
"type": "cross_fields",
"operator":"and"
}
},
{
"nested":{
"path":"Comments",
"query":{
"multi_match":{
"query":"red dog",
"fields":[
"Comments.Description",
"Comments.Description.folded"
],
"type": "cross_fields",
"operator":"and"
}
}
}
}
]
}
}
}
对我来说不幸的是,当我将评论保存到 ElasticSearch 时,评论有时为空,是否可以执行某种 "include if document exists" 条件?
更新
我仍然遇到同样的错误
[nested] failed to find nested object under path [Comments]
当我尝试使用 exists 进行查询时
GET /_all/video/_search
{
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"lisa",
"fields":[
"Title",
"Description"
],
"type":"cross_fields",
"operator":"and"
}
},
{
"nested":{
"path":"Comments",
"query":{
"filtered":{
"query":{
"match_all":{}
},
"filter":{
"exists":{
"field":"Comments.Description"
}
}
}
}
}
}
]
}
}
}
我的所有地图
{
"en":{
"mappings":{
"video":{
"properties":{
"Comments":{
"type":"nested",
"properties":{
"Description":{
"type":"string",
"analyzer":"english",
"fields":{
"folded":{
"type":"string",
"analyzer":"folding"
}
}
}
}
},
"Description":{
"type":"string",
"analyzer":"english",
"fields":{
"folded":{
"type":"string",
"analyzer":"folding"
}
}
},
"Title":{
"type":"string",
"analyzer":"english",
"fields":{
"folded":{
"type":"string",
"analyzer":"folding"
}
}
}
}
}
}
}
}
还有我的设置
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
}
}
原来我有一个 marvel 索引,它没有我输入的映射。我删除了插件,它在所有索引中搜索都很好。
最简单的方法是 "denormalize" 您的数据,这样您就有一个 属性 包含计数和一个布尔值(如果它存在或不存在)。然后你就可以搜索这些属性了。
例如:
{
"id": 31939,
"hasAttachments": true,
"attachmentCount": 2,
"attachments": [
{
"type": "Attachment",
"name": "txt.txt",
"mimeType": "text/plain"
},
{
"type": "Inline",
"name": "jpg.jpg",
"mimeType": "image/jpeg"
}
]
}