elasticsearch - 在 ElasticSearch 中匹配同一嵌套文档上的多个属性

标签 elasticsearch nosql

我正在尝试在 ElasticSearch 中的嵌套文档上完成归结为 bool 值 AND 的任务。假设我有以下两个文档。

{
    "id": 1,
    "secondLevels": [
        {
            "thirdLevels": [
                {
                    "isActive": true,
                    "user": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7a75746f737e696e687e695b7f74767a727535787476" rel="noreferrer noopener nofollow">[email protected]</a>"
                }
            ]
        },
        {
            "thirdLevels": [
                {
                    "isActive": false,
                    "user": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f5f3e5f2c0e4efede1e9eeaee3efed" rel="noreferrer noopener nofollow">[email protected]</a>"
                }
            ]
        }
    ]
}
{
    "id": 2,
    "secondLevels": [
        {
            "thirdLevels": [
                {
                    "isActive": true,
                    "user": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="334640564173575c5e525a5d1d505c5e" rel="noreferrer noopener nofollow">[email protected]</a>"
                }
            ]
        }
    ]
}

在本例中,我只想匹配具有 isActive: trueuser: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8a8c9a8dbf9b90929e9691d19c9092" rel="noreferrer noopener nofollow">[email protected]</a> 的嵌套文档的文档(在本例中为 ID:2)。

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "secondLevels.thirdLevels",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "term": {
                                            "secondLevels.thirdLevels.isActive": true
                                        }
                                    },
                                    {
                                        "term": {
                                            "secondLevels.thirdLevels.user": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed989e889fad8982808c8483c38e8280" rel="noreferrer noopener nofollow">[email protected]</a>"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

但是,似乎发生的情况是我的查询显示了两个文档,因为第一个文档有一个 thirdLevel 具有 isActive: true ,另一个 thirdLevel 具有适当的用户。

有什么方法可以在查询/过滤时严格执行此操作,还是必须在脚本中执行此操作?

最佳答案

通过嵌套对象和嵌套查询,您已经完成了大部分工作。

您现在要做的就是添加 inner hits标记并也使用 source filtering用于将整个 secondLevels 文档移开:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "secondLevels.thirdLevels",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "secondLevels.thirdLevels.isActive": true
                    }
                  },
                  {
                    "term": {
                      "secondLevels.thirdLevels.user": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="037670667143676c6e626a6d2d606c6e" rel="noreferrer noopener nofollow">[email protected]</a>"
                    }
                  }
                ]
              }
            },
            "inner_hits": {
              "size": 100
            }
          }
        }
      ]
    }
  }
}

关于elasticsearch - 在 ElasticSearch 中匹配同一嵌套文档上的多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46437137/

相关文章:

python - 2个集合之间的关系 Arangodb

nosql - Amazon DynamoDB 表设计和查询

python - 尝试发送 Elasticsearch 地理查询时无法散列类型 'dict'

python - os.walk() 缓存/加速

java - 找不到类型为 : class org. json.JSONObject 的返回值的转换器

elasticsearch - 重新使用内置语言过滤器?

android - Couchbase lite 查询和过滤

mongodb - CouchDB 或 MongoDB 中的哪一个适合我的需求?

elasticsearch - 使用Kibana在 Elasticsearch 中过滤邮件正文

scala - 在scala eclipse插件中构建查询/过滤器以进行 Elasticsearch