由于elasticSearch版本更新频繁,此笔记适用ES版本为 7.10.2
此笔记摘录自《Elasticsearch搜索引擎构建入门与实战》第一版
文中涉及代码适用于kibana开发工具,其他如es-head则语法会不太相同
elasticSearch学习笔记01-基础语法
1.创建索引
字段名在es中也叫映射(mapping)
PUT /索引名
{
"settings": { // 可省略,不写就给默认值
"number_of_shards": 主分片数,
"number_of_replicas": 副分片数
},
"mappings": {
"properties": {
"字段名": {
"type": "字段类型"
},
"字段名": {
"type": "字段类型"
},
"字段名": {
"type": "字段类型"
}
}
}
}
// 例子:
PUT /fzy_test_hotel
{
"mappings": {
"properties": {
"title": {
"type": "text" // 默认分词
},
"city": {
"type": "keyword" // 默认不分词
},
"price": {
"type": "double" // 数字类型
}
}
}
}
2.写入文档
注意id 如果不指定id es会自动给你生成一个
POST /索引名/_doc/插入的id
{
"title": "插入的值",
"city": "插入的值",
"price": 插入的值
}
// 例子:
POST /fzy_test_hotel/_doc/001
{
"title": "哈哈哈",
"city": "呵呵呵呵",
"price": 25.2
}
批量写入,语法比较难记
POST /_bulk
{"index":{"_index":"fzy_test_hotel"}}
{"title":"hahahahaah","city":"北京","price":555}
{"index":{"_index":"fzy_test_hotel"}}
{"title":"wwwwwww","city":"上海","price":2.2}
{"index":{"_index":"fzy_test_hotel"}}
{"title":"wwwwwww","city":"广州","price":1.1}
3.检索文档(根据id)
GET /索引名/_doc/文档id
//例子:
GET /fzy_test_hotel/_doc/001
//返回值
{
"_index" : "fzy_test_hotel",
"_id" : "001",
"_version" : 2,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "哈哈哈",
"city" : "呵呵呵呵",
"price" : 25.2
}
}
4.检索文档(条件查询)
// 默认全查
GET /索引名/_search
// 带条件查询则需要请求体
GET /fzy_test_hotel/_search
{
"query": {
"term": {
"字段": {
"value": "字段值"
}
}
}
}
//例子:
GET /fzy_test_hotel/_search
{
"query": {
"term": {
"price": {
"value": "25.2"
}
}
}
}
5.删除索引
DELETE /索引名
//例子:
DELETE /fzy_test_hotel
6.关闭索引和打开索引
索引关闭后不可 增删改查
POST /索引名/_close
POST /索引名/_open
7.索引别名
有些时候数据量特大或者有特殊需求需要“分库分表的”情况,新增数据的时候需要分别在不同的索引中新增,但查询的时候想全部一次性查出,则需要使用es的别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "当前索引名1",
"alias": "别名"
}
},
{
"add": {
"index": "当前索引名2",
"alias": "别名"
}
}
]
}
// 例子
POST /_aliases
{
"actions": [
{
"add": {
"index": "fzy_test_hotel",
"alias": "all_hotel"
}
},
{
"add": {
"index": "fzy_test_hotel2",
"alias": "all_hotel"
}
}
]
}
// 索引别名指向哪些索引 试了两种语法均可
GET /_alias/索引名
GET /索引名/_alias/*
8.查看索引映射
索引映射其实就是在传统关系型数据库中的表结构 es中叫映射(mapping)
GET /索引名/_mapping
//例子
GET /fzy_test_hotel/_mapping
//返回json:
{
"fzy_test_hotel" : {
"mappings" : {
"properties" : {
"city" : {
"type" : "keyword"
},
"price" : {
"type" : "double"
},
"title" : {
"type" : "text"
}
}
}
}
}
9.拓展映射
es是不可以更改索引映射结构的,只能新增字段或者给object对象类型增加属性字段
POST /fzy_test_hotel/_mapping
{
"properties": {
"新映射名": {
"type": "keyword"
}
}
}
10.多字段类型
一个字段可以有多个类型 例如一个字段可以即时text也是keyword
代码中新增了一个hotel_order映射, 注意user_name是存在两个类型
代码中user_name.user_name_keyword ,来指定某个多字段
// 创建映射
PUT /hotel_order
{
"mappings": {
"properties": {
"order_id": {
"type": "keyword"
},
"user_id": {
"type": "keyword"
},
"user_name": {
"type": "text",
"fields": {
"user_name_keyword": {
"type": "keyword"
}
}
},
"hotel_id":{
"type": "keyword"
}
}
}
}
// 查询映射
GET /hotel_order/_search
{
"query": {
"match": {
"user_name": "hahaha"
}
},
"sort": [
{
"user_name.user_name_keyword": {
"order": "desc"
}
}
]
}
11.更新文档
更新后文档的verson号会改变+1
注意 upsert关键字 如果文档存在则更新doc内的内容,如果文档不存在则插入upsert内的内容
单条更新
// 单条更新
POST /fzy_test_hotel/_update/001
{
"doc": {
"title": "hahaha",
"city": "冰岛",
"price": 2.2
}
}
// upsert关键字
POST /fzy_test_hotel/_update/001
{
"doc": {
"title": "存在修改这条",
"city": "存在修改这条",
"price": 2.2
},
"upsert": {
"title": "如果不存在插入此条",
"city": "如果不存在插入此条",
"price": 2.3
}
}
批量更新 语法同批量新增 语法有些古怪
// 批量更新
POST /_bulk
{"update":{"_index":"fzy_test_hotel","_id":"001"}}
{"doc":{"title":"hahahahaah","city":"北京","price":555}}
{"update":{"_index":"fzy_test_hotel","_id":"002"}}
{"doc":{"title":"hahahahaah","city":"北京","price":555}}
{"update":{"_index":"fzy_test_hotel","_id":"003"}}
{"doc":{"title":"hahahahaah","city":"北京","price":555}}
{"update":{"_index":"fzy_test_hotel","_id":"004"}}
{"doc":{"title":"hahahahaah","city":"北京","price":555}}
条件更新
根据条件来更新语句,类似数据库中的updete…where
script指定更新的内容
执行后先搜索出query里的内容,然后修改为script的脚本
注意,如果不写query的部分则是全部修改
POST /fzy_test_hotel/_update_by_query
{
"query": { // 如果不写query的则是全部修改
"term": {
"city": {
"value": "检索值"
}
}
},
"script": {
"source": "ctx._source['city']='上海'",
"lang": "painless"
}
}
12.删除文档
单条删除 批量删除 条件删除
// 删除指定id的文档
DELETE /fzy_test_hotel/_doc/001
// 批量删除 语法同修改和新增
POST /_bulk
{"delete":{"_index":"fzy_test_hotel","_id":"001"}}
{"delete":{"_index":"fzy_test_hotel","_id":"002"}}
{"delete":{"_index":"fzy_test_hotel","_id":"003"}}
// 条件删除
POST /fzy_test_hotel/_delete_by_query
{
"query": {
"term": {
"city": {
"value": "检索值"
}
}
}
}
// 全部删除 也是特殊的条件删除,查询全部即可
POST /fzy_test_hotel/_delete_by_query
{
"query": {
"match_all": {
}
}
}