【知识积累】-ES的DSL查询

96 阅读2分钟

本文采用的ES版本为7.1.0

1. 创建索引库

# 创建索引文档,并制定mapping
PUT demo_index
{
  "mappings": {
      "properties":{
        "title":{
          "type":"text"
        },
        "idCard":{
          "type": "keyword"
        },
        "age":{
          "type": "integer"
        },
        "price":{
          "type": "double"
        },
        "salePrice":{
          "type": "long"
        },
        "createTime":{
          "type": "date",
          "format": ["yyyy-MM-dd HH:mm:ss"]
        },
        "isDeleted":{
          "type": "boolean"
        },
        "children":{
          "type": "object",
          "properties": {
            "aaa":{
              "type":"text"
            }
          }
        },
        "list" : {
          "type": "nested", 
          "properties" : {
            "age" : {
              "type" : "long"
            },
            "name" : {
              "type" : "text"
            }
          }
        }
      }
    }
}
字段名数据类型说明
titletext会被分词器分词,如本文中的ES没有用分词器,插入该字段的值为“测试”,被分词为“测”、“试”两个倒排索引
idCardkeyword不会被分词器分词
ageinteger整型
pricedouble双精度浮点型
salePricelong长整型
createTimedate日期
isDeletedboolean布尔
childrenobject嵌套对象
listnested数组对象

2. 查看索引库

# 查看索引文档的mapping
GET /demo_index/_mapping?pretty=true

3. 删除索引库

# 删除索引
DELETE demo_index

4. 向索引中添加document记录

PUT demo_index/_doc/1
{
  "title":"测试1",
  "idCard":"111",
  "age":28,
  "price":22.30,
  "salePrice":2230,
  "createTime":"2020-09-16 11:35:35",
  "isDeleted":false,
  "children":{"aaa":"aaa"},
  "list":[
    {
      "name":"zhangsan",
      "age":28
    },
    {
      "name":"lisi",
      "age":29
    }
    ]
}

PUT demo_index/_doc/2
{
  "title":"测试2",
  "idCard":"测试2",
  "age":28,
  "price":22.30,
  "salePrice":2230,
  "createTime":"2020-09-16 11:35:35",
  "isDeleted":false,
  "children":{"aaa":"aaa"},
  "list":[
    {
      "name":"zhangsan",
      "age":28
    },
    {
      "name":"lisi",
      "age":29
    }
    ]
}

PUT demo_index/_doc/3
{
  "title":"测试",
  "idCard":"测试",
  "age":28,
  "price":22.30,
  "salePrice":2230,
  "createTime":"2020-09-16 11:35:35",
  "isDeleted":false,
  "children":{"aaa":"aaa"},
  "list":[
    {
      "name":"zhangsan",
      "age":28
    },
    {
      "name":"lisi",
      "age":29
    }
    ]
}

PUT demo_index/_doc/4
{
  "title":"测",
  "idCard":"测",
  "age":28,
  "price":22.30,
  "salePrice":2230,
  "createTime":"2020-09-16 11:35:35",
  "isDeleted":false,
  "children":{"aaa":"aaa"},
  "list":[
    {
      "name":"zhangsan",
      "age":28
    },
    {
      "name":"lisi",
      "age":29
    }
    ]
}

5. 删除索引库中的记录

# 删除索引中id为1的document记录
DELETE demo_index/_doc/1

6. dsl查询

1. 查询索引中所有记录

# 查询索引文档中的所有document记录
GET demo_index/_search
{
  "query": {
   "match_all": {}
  }
}

2. bool查询

GET demo_index/_search
{
  "query": {
   "bool": {
     "must": [
       {"match": {
         "title": "测试1"
       }},
       {"match": {
         "idCard": "测试2"
       }},
       {"range": {
         "age": {
           "gte": 28,
           "lte": 28
         }
       }}
     ]
   }
  }
}

3. term查询

单值查询,相当于sql的=操作,精确匹配,参数不会被分词

GET demo_index/_search
{
  "query": {
    "term": {
      "idCard": {
        "value": "测试"
      }
    }
  }
}

4. terms查询

多值查询,相当于sql的in操作,精确匹配,参数不会被分词

GET demo_index/_search
{
  "query": {
    "terms": {
      "idCard": [
        "测试",
        "测试2",
        "测"
      ]
    }
  }
}
查询方式参数是否被分词说明
match该文档使用的ES未配置分词,参数传入“测试”,被分词成“测”、“试”两个词汇,匹配其中之一就会被检索出来,适用场景:模糊匹配
term/terms参数不会被分词,适用场景:精确匹配