elasticsearch - 基于条件的弹性搜索查询(if else 或 Union case)
问题描述
我正在研究弹性查询(多个条件/如果基于其他)。我的问题如下
文件 1
{
"id": "1234",
"isQualified": true,
"pending_subjects": null,
"student": {
"first_name": "TEST First 1234",
"last_name": "TEST Last 1234",
"dob": "01012005"
},
"stud_ext_curr_status" :"A+ve"
}
文件 2
{
"id": "12345",
"isQualified": true,
"pending_subjects": null,
"student": {
"first_name": "TEST First 12345",
"last_name": "TEST Last 12345",
"dob": "01022004"
}
"stud_ext_curr_status" :"A"
}
文件 3
{
"id": "55676",
"isQualified": true,
"pending_subjects": null,
"student": {
"first_name": "TEST First 565",
"last_name": "TEST Last 12368845",
"dob": "01022004"
}
"stud_ext_curr_status" :"C-ve"
}
其他文件
{
"id": "1111",
"isQualified": false,
"pending_subjects": [
"MATH",
"ECONOMICS"
],
"student": {
"first_name": "TEST First 1111",
"last_name": "TEST Last 1111",
"dob": "02012004"
}
"stud_ext_curr_status" :"A+ve"
}
文件 4
{
"id": "2222",
"isQualified": false,
"pending_subjects": [
"SCIENCE"
],
"student": {
"first_name": "TEST First 21",
"last_name": "TEST Last 21",
"dob": "02012004"
}
"stud_ext_curr_status" :"A+ve"
}
所以,我想有记录(无论是isQualified
假的还是真的,但有一些具体的条件如下)。
condition 1) if isQualified is false then
a) Filter all records/documents having non null pending_subjects elements
b) Filter above case but for the case pending_subjects element having "SCIENCE"
there is need to check stud_ext_curr_status element further to include only if it has "A+ve"
2) if isQualified is true then filter the document having `stud_ext_curr_status` element value C-ve
所以,基本上输出应该是由条件 1 和 2 产生的文档联合
解决方案
您可以在 should 和 must 子句中组合不同的组合
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"isQualified": {
"value": true
}
}
},
{
"term": {
"stud_ext_curr_status.keyword": {
"value": "C-ve"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"isQualified": {
"value": false
}
}
},
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "pending_subjects"
}
}
],
"must_not": [
{
"term": {
"pending_subjects": {
"value": "science"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"pending_subjects": {
"value": "science"
}
}
},
{
"term": {
"stud_ext_curr_status.keyword": {
"value": "A+ve"
}
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
推荐阅读
- django - 当 DEBUG 为 False 时如何在 Django 中获取最后执行的查询?
- mysql - 对面的mysql数据透视表
- node.js - MissingRequiredParameter:参数中缺少必需的键“thingName”
- python - Codeforces 上的“输出格式错误,文件意外结束 - 预期令牌”错误
- c# - 如何使用 EF Core 使 C# 中的字段唯一?
- symfony - Symfony 中的延迟学说事件侦听器以发送 webhook
- kotlin - 如何在双回合中修复 NumberFormatException
- python - 如何使用upper()制作字符串的第一个字母大写 - PYTHON
- azure - devops:实现 Azure AD 安全组
- protocol-buffers - ProtoBuf 有效负载的建议扩展是什么?