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" : [ ]
  }
}

4、一个字段 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": {}
}

5、两个字段 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" : [ ]
  }
}

ElasticSearch的简单API操作可以查看我另外的一篇文档:

ElasticSearch RESTFUL API的简单操作

你可能感兴趣的:(ElasticSearch)