13.Elasticsearch查询语句4

本文从简单到复杂,将前面的查询关键字都用起来,并给出在mysql中的语句样式,方便对比理解。

1.简单条件查询

mysql:

SELECT
    *
FROM
    policy_file
WHERE
    LEVEL = '国家级'
AND web_source = '浙江省人民政府'
AND launch_department != '浙江省工商局'

Elasticsearch:

curl -XGET "http://10.1.1.28:9200/policy_document/policy_document/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "level": "国家级"
          }
        },
        {
          "match": {
            "web_source": "浙江省人民政府"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "launch_department": "浙江省工商局"
          }
        }
      ]
    }
  }
}'

2.简单分页

mysql:

SELECT
    *
FROM
    policy_file
WHERE
    LEVEL = '国家级'
AND web_source = '浙江省人民政府'
AND launch_department != '浙江省工商局'
LIMIT 0,10

Elasticsearch:

curl -XGET "http://10.1.1.28:9200/policy_document/policy_document/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "level": "国家级"
          }
        },
        {
          "match": {
            "web_source": "浙江省人民政府"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "launch_department": "浙江省工商局"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10
}'

3.负责分页

mysql:

SELECT title,create_time,update_time
FROM
    policy_file
WHERE
    LEVEL = '国家级'
AND web_source = '浙江省人民政府'
AND launch_department != '浙江省工商局'
AND launch_date >= '2015-01-01'
AND launch_date <= '2018-01-01'
LIMIT 0,10

Elasticsearch:

curl -XGET "http://10.1.1.28:9200/policy_document/policy_document/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "level": "国家级"
          }
        },
        {
          "match": {
            "web_source": "浙江省人民政府"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "launch_department": "浙江省工商局"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10,
  "_source": ["title","create_time","update_time"]
}'

4.复杂分页

mysql:

SELECT title,create_time,update_time,launch_date
FROM
    policy_file
WHERE 1=1
AND level = '国家级'
AND web_source = '浙江省人民政府'
AND launch_department != '浙江省工商局'
AND launch_date >= '2015-01-01'
AND launch_date <= '2018-01-01'
ORDER BY launch_date DESC
LIMIT 0,10

Elasticsearch:

curl -XGET "http://10.1.1.28:9200/policy_document/policy_document/_search" -H 'Content-Type: application/json' -d'
{
  "_source": [
    "title","create_time","update_time","launch_date"],
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "level": "国家级"
          }
        },
        {
          "match": {
            "web_source": "浙江省人民政府"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "launch_department": "浙江省工商局"
          }
        }
      ],
      "filter": {
        "range": {
          "launch_date": {
            "gte": "2005-01-01",
            "lte": "2018-01-01"
          }
        }
      }
    }
  },
  "sort": [
    {
      "launch_date": {
        "order": "desc"
      }
    }
  ], 
  "from": 0,
  "size": 10
}'

5.查询mapping

GET /policy_document/policy_document/_mapping

6.mapping中添加字段

如:添加plat_from字段,类型为integer

POST policy_document/policy_document/_mapping
{
  "properties": {
    "plat_from":{
      "type": "integer"
    }
  }
}

下一篇:14.Elasticsearch索引设置

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券