ElasticSearch仅查询嵌套(如果存在)

时间:2015-03-06 15:44:12

标签: elasticsearch

我有这个搜索查询来查找查询搜索词" 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时,注释有时会为空,是否可以执行某种“如果文档存在则包含”#34;条件?

更新

我仍然得到同样的错误

[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"
                    ]
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

事实证明我有一个奇迹索引,它没有我的类型的映射。我删除了插件,它可以很好地搜索所有索引。

答案 1 :(得分:0)

最简单的方法是对#34;反规范化"您的数据,以便您拥有包含计数的属性和一个布尔值(如果存在或不存在)。然后你可以搜索这些属性。

例如:

{
   "id": 31939,
   "hasAttachments": true,
   "attachmentCount": 2,
   "attachments": [
      {
         "type": "Attachment",
         "name": "txt.txt",
         "mimeType": "text/plain"
      },
      {
         "type": "Inline",
         "name": "jpg.jpg",
         "mimeType": "image/jpeg"
      }
   ]  
}