[英]Use "must_not exists" using elasticsearch_dsl
对于我的一个项目,我需要从 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"
}
我想确定我的哪些用户没有任何订阅。 在我的示例中,应该返回“Bob Smith”和“Patty 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()
谢谢你的帮助
我继续搜索和测试,似乎我找到了问题的解决方案
query = Search()\
.filter('term', schema='https://sample.org/schemas/user_v0.0.1.json')\
.filter('bool', must_not=[Q('exists', field="subscriptions")])\
.source('barcode')\
.scan()
我希望它可以帮助某人!
我不熟悉 Python DSL,但是用于查找没有任何订阅的用户的 REST 查询是:
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "subscriptions",
"query": {
"exists": {
"field": "subscriptions"
}
}
}
}
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.