首页 > 解决方案 > 基于条件的弹性搜索查询(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 产生的文档联合

标签: elasticsearch

解决方案


您可以在 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"
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

推荐阅读