ES使用命令说明

索引映射相关

# 查询索引
GET goods_index

# 添加索引
PUT person

# 添加映射
PUT person/_mapping
{
  "properties":
  {
    "name":
    {
      "type":"keyword"
    },
    "age":
    {
      "type":"integer"
    }
  }
}

# 查询映射
GET person/_mapping

# 创建并添加映射
PUT person
{
  "properties":
  {
    "name":
    {
      "type":"keyword"
    },
    "age":
    {
      "type":"integer"
    }
  }
}

# 映射添加字段
PUT person/_mapping
{
  "properties":
  {
    "address":{
      "type":"text"
    }
  }
}

文档相关

# 添加文档,指定id
PUT person/_doc/1
{
  "name":"abzz",
  "age":10,
  "abbress":"成都市锦江区"
}

# 添加文档,不指定id
POST person/_doc
{
  "name":"abzz",
  "age":10,
  "abbress":"成都市锦江区"
}

# 修改文档
PUT person/_doc/1
{
  "name":"abzz_1",
  "age":10,
  "abbress":"成都市锦江区"
}

# 根据id查询文档
GET person/_doc/1

# 查询所有文档
GET person/_doc/_search

# 根据id删除文档
DELETE person/_doc/1HTcj34BJqQfCGFUhRkz

分词器相关

# 分词器使用
GET _analyze
{
  "analyzer": "standard",
  "text": [
    "i have a dream"
    ]
}

term查询和match查询

# 分词器使用
GET _analyze
{
  "analyzer": "ik_max_word",
  "text": [
    "青岛市市南区"
    ]
}

# 删除索引
DELETE person

# 新建一个带有指定分词器的索引
PUT person

PUT person/_mapping
{
  "properties":{
    "name":{
      "type":"keyword"
    },
    "age":{
      "type":"integer"
    },
    "address":{
      "type":"text",
      "analyzer":"ik_max_word"
    }
  }
}

# 添加文档
PUT person/_doc/1
{
  "name":"abzz_1",
  "age":10,
  "address":"成都市锦江区"
}

PUT person/_doc/2
{
  "name":"abzz_2",
  "age":11,
  "address":"成都市高新区"
}


PUT person/_doc/3
{
  "name":"abzz_3",
  "age":11,
  "address":"青岛市市南区"
}

GET person/_search

# term查询,需要词条完全匹配,不对查询条件进行分词
GET person/_search
{
  "query": {
    "term": {
      "address": {
        "value": "都市"
      }
    }
  }
}

# match查询,首先会对查询条件进行分词,然后对各个分词的搜索条件进行求并集
GET person/_search
{
  "query": {
    "match": {
      "address": "青岛是一个好地方"
    }
  }
}

批量操作

# 1.删除id为1的记录
# 2.创建id为6的张国荣记录
# 3.修改id为3的记录为黎明
POST _bulk 
{"delete":{"_index":"person", "_id":"1"}}
{"create":{"_index":"person", "_id":"6"}}
{"name":"张国荣","age":55,"address":"香港"}
{"update":{"_index":"person", "_id":"3"}}
{"doc":{"name":"黎明"}}

matchAll示例

# matchAll演示,默认查询前十条,from和size控制分页
GET goods/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2
}

模糊查询

wildcard查询

# wildcard查询 ?代表一个字符 *
GET goods/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "魅族"
      }
    }
  }
}

正则查询

# 正则表达式查询
GET goods/_search
{
  "query": {
    "regexp": {
      "title": "魅族111*"
    }
  }
}

前缀查询

# 前缀查询
GET goods/_search
{
  "query": {
    "prefix": {
      "title": {
        "value": "魅"
      }
    }
  }
}

范围查询

# range查询
GET goods/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 10,
        "lte": 20000
      }
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

query_string和simple_query_string

# queryString和simpleQueryString
# queryString中的query条件支持and或者or关键字,并且关键字需要大写,而simpleQueryString不支持
GET goods/_search
{
  "query": {
    "query_string": {
      "fields": ["title", "categoryName", "brandName"], 
      "query": "小米 AND 手机"
    }
  }
}

bool查询

说明:

  1. must 返回的文档必须满足must子句的条件,并且参与计算分值
  2. filter 返回的文档必须满足filter子句的条件。不计算分值
  3. should 返回的文档可能满足should子句的条件。在一个Bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回。minimum_should_match参数定义了至少满足几个子句
  4. must_not 返回的文档必须不满足must_not定义的条件
# bool查询
GET goods/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "brandName": {
            "value": "小米"
          }
        }},
        {"match": {
          "title": "手机笔记本"
        }}
      ]
    }
  }
}

聚合查询

  1. 指标聚合(聚合函数)
  2. 桶聚合(分组)
# 聚合
# 指标聚合-聚合函数
GET goods/_search
{
  "query": {
    "match": {
      "title": "手机"
    }
  },
  "aggs": {
    "min_price": {
      "min": {
        "field": "price"
      }
    }
  }
}

# 桶聚合-分组聚合
GET goods/_search
{
  "query": {
    "term": {
      "brandName": {
        "value": "魅族"
      }
    }
  },
  "aggs": {
    "brand_name": {
      "terms": {
        "field": "brandName",
        "size": 10
      }
    }
  }
}

高亮查询

# 高亮查询
GET goods/_search
{
  "query": {
    "match": {
      "title": "手机"
    }
  },
  "highlight": {
    "pre_tags": "<abzz>",
    "post_tags": "</abzz>",
    "fields": {
      "title": {}
    }
  }
}

重建索引

# 重建索引
# 初始索引
# 1.添加索引
PUT test_index_v1
# 2.设置映射
PUT test_index_v1/_mapping
{
  "properties":{
    "name":{
      "type":"keyword"
    },
    "age":{
      "type":"integer"
    }
  }
}
GET test_index_v1/_mapping
# 3.添加数据
PUT test_index_v1/_doc/1
{
  "name":"abzz",
  "age":13
}
GET test_index_v1/_search
# 业务需求变了,需要修改映射结构,将name的类型修改为text
PUT test_index_v2
PUT test_index_v2/_mapping
{
  "properties":{
    "name":{
      "type":"text"
    },
    "age":{
      "type":"integer"
    }
  }
}
# 复制数据
POST _reindex
{
  "source": {"index": "test_index_v1"},
  "dest": {"index": "test_index_v2"}
}
GET test_index_v2/_search

说明:由于索引映射结构变化需要走上述流程,但是代码里面的索引还是用的原始的,有以下解决方案

  1. 修改代码中的索引库名称(不推荐,懂得都懂)
  2. 将新建的索引赋值别名(推荐)
# 先删除之前的索引
DELETE test_index_v1
# 给新的索引赋值别名
POST test_index_v2/_alias/test_index_v1