【问题标题】:ElasticSearch only query nested if it existsElasticSearch 仅查询嵌套(如果存在)
【发布时间】:2016-05-07 22:01:16
【问题描述】:

我有这个搜索查询在根标题和描述中找到查询搜索词“红狗”,并匹配嵌套的 cmets 文档。

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

不幸的是,当我将 cmets 持久保存到 ElasticSearch 时,它们有时为空,是否可以执行某种“如果文档存在则包含”条件?

更新

我仍然遇到同样的错误

[nested] failed to find nested object under path [Comments]

当我尝试使用存在查询时

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

【问题讨论】:

  • exists 过滤器应该做你想做的事。 elasticsearch.org/guide/en/elasticsearch/reference/current/…
  • 马克,你能发布你的完整地图吗?
  • 完成,请看原帖底部。还包括我的原始设置
  • 您的映射定义可能存在问题。我创建了以下要点,它运行查询没有任何错误(在 ES 1.4.4 上)gist.github.com/jayhilden/899112a5d0fe09c5f1e9
  • 我刚刚检查过它确实适用于“en”索引。它不适用于所有索引(“_all”)。你知道这是为什么吗?是否需要跨所有索引设置默认映射?

标签: elasticsearch


【解决方案1】:

原来我有一个奇迹索引,它没有我输入的映射。我删除了插件,它很好地搜索了所有索引。

【讨论】:

    【解决方案2】:

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

    例如:

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

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 2018-03-08
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多