Elaticsearch进阶-语法使用

325 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第7天,点击查看活动详情

前言

上一期介绍了ES的安装和简单使用,这一期说说关于ES的语法相关。

RESTfull API

1.创建索引

PUT index_test
{
    "settings": {
        "number_of_shards": 1, // 设置索引分片数量
        "number_of_replicas": 1 // 设置索引每个分片的副本数量
    },
    "mappings": {
        "properties": {
            "name":{
            "type": "text", // 文本类型字段,会被分词索引保存
            "analyzer": "ik_max_word", // 设置保存使用的分词器
            "search_analyzer": "ik_max_word" // 设置搜索时使用的分词器
        },
        "phone":{
            "type": "keyword" // 关键词类型,不会被分词索引
        },
        "age":{
            "type": "integer"
        },
        "localtion":{
            "type": "geo_point" // 地理位置类型
        },
        "createDate":{
            "type": "date", // 时间类型
            "format": "yyyy-MM-dd HH:mm:ss" // 设置数据保存格式,未设置保存为时间戳
         }
      }
   }
}

2.删除索引

DELETE index_test

3.插入数据

// 指定数据id插入数据
POST index_test/_doc/1
{
    "name":"名称",
    "phone":"18883842027",
    "age":25,
    "location":{
        "lat":30.001,
        "lon":100.001
    },
"createDate":"2021-01-19 22:40:44"
}
// 不指定id保存数据,自动生成随机ID
POST index_test/_doc
{
    "name":"名称",
    "phone":"18883842027",
    "age":25,
    "location":{
        "lat":30.001,
        "lon":100.001
    },
    "createDate":"2021-01-19 22:40:44"
}

4.删除数据

DELETE index_test/_doc/1

5.聚合

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-aggregations.html

avg :平均值

max:最大值

min:最小值

sum:求和

GET people/_search // 查询平均年龄
{
  "aggs": {
    "ageAvg": {
      "avg": {
        "field": "age"
      }
    }
  }
}
GET people/_search // 查询年龄之和
{
  "aggs": {
    "ageSum": {
      "sum": {
        "field": "age"
      }
    }
  }
}
GET people/_search // 查询最小年龄
{
  "aggs": {
    "ageMin": {
      "min": {
        "field": "age"
      }
    }
  }
}
GET people/_search // 查询最大年龄
{
  "aggs": {
    "ageMax": {
      "max": {
        "field": "age"
      }
    }
  }
}

terms词聚合

基于某个field,该 field 内的每一个【唯一词元】为一个桶,并计算每个桶内文档个数。默认返回顺序是 按照文档个数多少排序。

GET people/_search // 按照年龄分组统计数量
{
  "aggs": {
    "ageGroupCount": {
      "terms": {
        "field": "age"
      }
    }
  }
}

6.结构化查询

6.1term查询

term 主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数 据类型):

GET index_test/_search
{
  "query": {
    "term": {
      "age": {
        "value": "25" // 查询年龄等于25的数据
      }
    }
  }
}

GET index_test/_search
{
  "query": {
    "term": {
      "phone": {
        "value": "18883842027" // 查询手机号码为18883842027的数据
      }
    }
  }
}

6.2terms查询

terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需 要一起去 做匹配:

GET index_test/_search
{
  "query": {
    "terms": { // 查询年龄为23,25,27 的数据
      "age": [
        "23",
        "25",
        "27"
      ]
    }
  }
}

6.3range查询

range 过滤允许我们按照指定范围查找一批数据:

gt :: 大于

gte :: 大于等于

lt :: 小于

lte :: 小于等于

GET index_test/_search
{
  "query": {
    "range": {
      "age": { // 查询大于等于18小于等于25的数据
        "gte": 18,
        "lte": 25
      }
    }
  }
}

6.4exists查询 exists 查询可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的 IS_NULL 条件 包含这个字段就返回返回这条数据

GET index_test/_search
{
  "query": {
    "exists": { // 查询存在字段age的数据
      "field": "age"
    }
  }
}

6.5 match查询 match 查询是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它。 如果你使用 match 查询一个全文本字段,它会在真正查询之前用分析器先分析 match 一下查询字符; 如果用 match 下指定了一个确切值,在遇到数字,日期,布尔值或者 not_analyzed 的字符串时,它将 为你搜索你 给定的值:

GET index_test/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  }
}

6.6 bool查询

bool 查询可以用来合并多个条件查询结果的布尔逻辑,它包含一下操作符:

must :: 多个查询条件的完全匹配,相当于 and 。

must_not :: 多个查询条件的相反匹配,相当于 not 。

should :: 至少有一个查询条件匹配, 相当于 or 。

这些参数可以分别继承一个查询条件或者一个查询条件的数组:

GET index_test/_search // 查询年龄等于25并且电话号码为18888888888的数据
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "age": {
            "value": "25"
          }
        }},
        {
            "term": {
              "phone": {
                "value": "18888888888"
            }
          }
        }
      ]
    }
  }
}