查询中的弹性搜索多个 AND & OR 运算符

时间:2021-07-12 20:50:20

标签: elasticsearch

我的索引应用了以下映射:

PUT /testing

PUT /testing/_mapping?pretty
{

  "properties": {
    "empID": {
      "type":"long"
    },
    "state":{
         "type":"text"
    },
    "Balance":{
        "type":"long"
    },
    "loanid":{    
      "type":"long"
    },
    "rating":{
      "type":"text"
    },
    "category":{
      "type":"text"
    }
  }

}

添加到索引中的示例文档

POST testing/_doc?pretty
 {
         "empID":1,
         "state":"NY",
         "Balance":55,
         "loanid":89,
         "rating":"A",
         "category":"PRO"
      }

POST /testing/_doc?pretty
{
         "empID":1,
         "state":"TX",
         "Balance":56,
         "loanid":65,
         "rating":"B",
         "category":"TRIAL"
      }

POST /testing/_doc?pretty
  {
         "empID":2,
         "state":"TX",
         "Balance":34,
         "loanid":76,
         "rating":"C",
         "category":"PAID"
      }


POST /testing/_doc?pretty
{
         "empID":3,
         "state":"TX",
         "Balance":72,
         "loanid":23,
         "rating":"D",
         "category":"FREE"
      }

POST /testing/_doc?pretty
{
         "dealID":3,
         "state":"NY",
         "Balance":23,
         "loanid":67,
         "rating":"E",
         "category":"FREE"
      }      

POST /testing/_doc?pretty
      {
         "empID":2,
         "state":"NY",
         "Balance":23,
         "loanid":98,
         "rating":"F",
         "category":"PRE"
      }
     
POST /testing/_doc?pretty
{
         "empID":2,
         "state":"TX",
         "Balance":19,
         "loanid":100,
         "rating":"D",
         "category":"PAID"
      }

我正在尝试创建相当于 sql 查询的 ES 查询,例如:

select * from table_name 
 where empID =1 or state = 'NY' 
       and balance >=20 or loanid in (23, 67, 89) or rating = 'D'
       and category!='FREE' or empID = 2  ;

vs (ES 查询)

GET testing/_search?pretty
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "state": {
              "query": "NY"
            }
          }
        },
        {
          "term": {
            "empID": 1
          }
        },
        {
          "bool": {
            "must": [
              {
                "range": {
                  "Balance": {
                    "gte": 20
                  }
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "loanid": [
                          23,
                          67,
                          89
                        ]
                      }
                    },
                    {
                      "match": {
                        "rating": {
                          "query": "D"
                        }
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "match": {
                              "category": {
                                "query": "FREE"
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
} 

我只得到 6 个文档,其中 sql 查询返回 7 个文档。您能否确认这是否是多个 AND & OR QUERY 在 ES 中的工作方式并帮助我解决问题。

0 个答案:

没有答案
相关问题