对于我的项目之一,我需要从ES索引确定缺少字段的所有记录。
请在下面查看存储在我的ES索引中的数据示例:
{
"schema": "https://sample.org/schemas/user_v0.0.1.json",
"barcode": "210000001",
"birth_date": "1961-11-24",
"first_name": "John",
"last_name": "Doe",
"subscriptions": [
{
"end_date": "2021-03-30",
"start_date": "2020-03-30"
}
]
}, {
"schema": "https://sample.org/schemas/user_v0.0.1.json",
"barcode": "210000002",
"birth_date": "1980-03-17",
"first_name": "Bob",
"last_name": "Smith",
"subscriptions": []
}, {
"schema": "https://sample.org/schemas/user_v0.0.1.json",
"barcode": "210000003",
"birth_date": "1980-03-17",
"first_name": "Patty",
"last_name": "Smith"
}
我想确定我的哪些用户没有任何订阅。在我的示例中,应返回“鲍勃·史密斯”和“帕蒂·史密斯”。我需要使用Python ElasticSearch DSL查询来做到这一点。
目前,我可以过滤搜索以仅检索用户,但是尽管进行了多次尝试,但我仍未找到仅获得用户“must_not” +“exists”订阅的方法。
results = Search()\
.filter('term', schema='https://sample.org/schemas/user_v0.0.1.json')
# complete filter with : "Must not exists subscription"
.source('barcode')
.scan()
谢谢你的帮助
最佳答案
我对Python DSL不熟悉,但是用于查找那些没有任何订阅的用户的REST查询是:
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "subscriptions",
"query": {
"exists": {
"field": "subscriptions"
}
}
}
}
]
}
}
关于python - 通过elasticsearch_dsl使用 “must_not exists”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60971169/