ElasticSearch進行and,or,in,not in多條件組合DSL結構化查詢

1、兩個條件and 問題:SELECT * FROM t_test_info t WHERE t.kv.p.keyword = '123' AND t.kv.b.keyword = 'p'
如果想看詳情數據設置size
GET /t_test_info/_search
{
  "size": 0, 
  "from": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "kv.p.keyword": "123"
          }
        },
        {
          "term": {
            "kv.b.keyword": "p"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "sort": [],
  "aggs": {}
}

結果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
2、同一字段 in問題:SELECT * FROM t_test_info t WHERE t.client_ip in ('123' , '1234')
GET /t_test_info/_search
{
  "size": 0, 
  "from": 0,
  "query": {
    "terms": {
      "client_ip": [
        "123","1234"
      ]
    }
  },
  "sort": [],
  "aggs": {}
}

結果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 9,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
3、兩字段 or 問題:SELECT * FROM t_test_info t WHERE t.client_ip = '123' or t.kv.b = 'l'
GET /t_test_info/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "client_ip": "123"
          }
        },
        {
          "term": {
            "kv.b": "l"
          }
        }
      ],
      "must_not": [],
      "must": []
      
    }
  },
  "from": 0,
  "size": 0,
  "sort": [],
  "aggs": {}
}

結果:

{
  "took" : 699,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 24,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
3、一個字段 not in 問題:SELECT * FROM t_test_info t WHERE t.client_ip not in ('123', '1234')
GET /t_test_info/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "client_ip": "123"
          }
        }
      ],
      "must": [],
      "should": []
    }
  },
  "from": 0,
  "size": 0,
  "sort": [],
  "aggs": {}
}
4、兩個字段 not in 問題:SELECT * FROM t_test_info t WHERE t.client_ip not in ('123') and t.@timestamp not in ("2020-05-15T15:13:47.000Z");
GET /t_test_info/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "client_ip": "123"
          }
        },
        {
          "term": {
            "@timestamp": "2020-05-15T15:13:47.000Z"
          }
        }
      ],
      "should": []     
    }
  },
  "from": 0,
  "size": 0,
  "sort": [],
  "aggs": {}
}

結果:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 343,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章