在Elasticsearch中搜索具有所有嵌套子项与给定查询匹配的对象 [英] Searching objects having all nested children matching a given query in Elasticsearch

查看:68
本文介绍了在Elasticsearch中搜索具有所有嵌套子项与给定查询匹配的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出具有以下映射的对象:

Given an object with the following mapping:

{
    "a": {
        "properties": {
            "id": {"type": "string"}
            "b": {
                "type": "nested",
                "properties": {
                    "key": {"type": "string"}
                }
            }
        }
    }
}

我想检索具有与给定查询匹配的所有嵌套子级的该对象的所有实例.

I want to retrieve all the instances of this object having all nested children matching a given query.

例如,假设我要检索所有具有"key" ="yes"的所有子代的实例.鉴于以下情况:

For example, suppose I want to retrieve all the instances having all children with "key" = "yes". Given the following instances:

{
    "id": "1",
    "b": [
        {
            "key": "yes"
        },
        {
            "key": "yes"
        }
    ] 
},
{
    "id": "2",
    "b": [
        {
            "key": "yes"
        },
        {
            "key": "yes"
        },
        {
            "key": "no"
        }
    ] 
},

我只想检索第一个("id" ="1"的那个).

I want to retrieve only the first one (the one with "id" = "1").

使用过滤器或查询对我来说都很好.我已经尝试使用"不过滤"和"必须_not布尔过滤器".这个想法是使用双重否定法来仅提取没有与给定字段不同的字段的对象.但是,我无法正确记下该查询.

Both using filters or queries is fine to me. I already tried to use the "not filter" and the "must_not bool filter". The idea was to use a double negation to extract only objects that doesn't have fields that are different to the given one. However, I was not able to write down this query correctly.

我意识到这不是搜索引擎的常见查询,但就我而言,它可能很有用.

I realize that this is not a common query for a search engine, but, in my case, it can be useful.

是否可以使用嵌套对象编写此查询(所有嵌套查询")?如果不是这样,是否可以使用父子项编写此查询?

Is it possible to write this query ("forall nested query") using nested objects? In case it is not, would it be possible to write this query using parent-child?

更新

如果我们知道我们要避免的"key"的所有值(在示例中为"no"),Andrei Stefan给出了很好的答案.

Andrei Stefan gave a good answer in case we know all the values of "key" that we want to avoid, ("no", in the example).

如果您不知道要避免的值,而您只想将嵌套对象与"key" ="yes"匹配,我也很感兴趣.

I am interested also in the case you don't know the values you want to avoid, and you just want to match nested object with "key"="yes".

推荐答案

为此,您需要一个扁平化的数据结构-值数组.最简单的方法(不要过多更改当前映射)是使用 include_in_parent 属性,并针对此特定要求查询父级中包含的字段:

You need a flattened data structure for this - an array of values. The simplest way and not to change the current mapping too much, is to use include_in_parent property and to query the field that's being included in the parent for this particular requirement:

{
  "mappings": {
    "a": {
      "properties": {
        "id": {
          "type": "string"
        },
        "b": {
          "type": "nested",
          "include_in_parent": true,
          "properties": {
            "key": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

然后您的查询将如下所示:

And then your query would look like this:

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "query": {
              "query_string": { "query": "b.key:(yes NOT no)"}
            }
          }
        ]
      }
    }
  }
}

另一种方法是将字段的类型从嵌套"更改为对象",但是通过这种方式,您将失去使用嵌套的优点>字段:

The alternative is to change the type of the field from nested to object but in this way you'll loose the advantages of using nested fields:

{
  "mappings": {
    "a": {
      "properties": {
        "id": {
          "type": "string"
        },
        "b": {
          "type": "object",
          "properties": {
            "key": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

查询保持不变.

这篇关于在Elasticsearch中搜索具有所有嵌套子项与给定查询匹配的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆