elasticsearch6.4 官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/6.4/getting-started.html
基本概念
索引(index):
类似于 mysql 中的数据库
自定义类型(type):
类似于 mysql 中的数据表
文档(document):
类似于 mysql 中的行
文档ID:
类似于 mysql 中的主键ID
常用API
API请求路径格式
/索引(index)/自定义类型(type)/文档id
索引相关
创建索引(同时创建
自定义类型
):PUT /my-index
{ "mappings": { "my-type": { "properties": { "name": { "type": "keyword" }, "age": { "type": "integer" }, "birthday": { "type": "date" }, "desc": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } } } }
删除索引:
DELETE /my-index
获取索引列表:
GET /_cat/indices?format=json
文档操作相关(增删改)
创建文档:
PUT /my-index/my-type/11
{ "name": "011", "age": 25, "birthday": "2000-01-01", "desc": "我的名字叫011,我今年23岁了。我出生于2000年1月1日。谢谢大家!" }
批量创建文档:
POST /my-index/my-type/_bulk
{"index":{"_id":"1"}} {"name":"001","age":33,"birthday":"1990-01-01","desc":"我的名字叫001,我今年33岁了。我出生于1990年1月1日。谢谢大家!"} {"index":{"_id":"2"}} {"name":"002","age":32,"birthday":"1991-01-01","desc":"我的名字叫002,我今年32岁了。我出生于1991年1月1日。谢谢大家!"} {"index":{"_id":"3"}} {"name":"003","age":31,"birthday":"1992-01-01","desc":"我的名字叫003,我今年31岁了。我出生于1992年1月1日。谢谢大家!"} {"index":{"_id":"4"}} {"name":"004","age":30,"birthday":"1993-01-01","desc":"我的名字叫004,我今年30岁了。我出生于1993年1月1日。谢谢大家!"} {"index":{"_id":"5"}} {"name":"005","age":29,"birthday":"1994-01-01","desc":"我的名字叫005,我今年29岁了。我出生于1994年1月1日。谢谢大家!"} {"index":{"_id":"6"}} {"name":"006","age":28,"birthday":"1995-01-01","desc":"我的名字叫006,我今年28岁了。我出生于1995年1月1日。谢谢大家!"} {"index":{"_id":"7"}} {"name":"007","age":27,"birthday":"1996-01-01","desc":"我的名字叫007,我今年27岁了。我出生于1996年1月1日。谢谢大家!"} {"index":{"_id":"8"}} {"name":"008","age":26,"birthday":"1997-01-01","desc":"我的名字叫008,我今年26岁了。我出生于1997年1月1日。谢谢大家!"} {"index":{"_id":"9"}} {"name":"009","age":25,"birthday":"1998-01-01","desc":"我的名字叫009,我今年25岁了。我出生于1998年1月1日。谢谢大家!"} {"index":{"_id":"10"}} {"name":"010","age":24,"birthday":"1999-01-01","desc":"我的名字叫010,我今年24岁了。我出生于1999年1月1日。谢谢大家!"}
根据ID删除文档:
DELETE /my-index/my-type/1
根据条件删除文档:
POST /my-index/my-type/_delete_by_query
{ "query": { "match": { "age": 23 } } }
根据ID更新文档:
PUT /my-index/my-type/1
{ "name": "1", "age": 0, "birthday": "1990-01-01", "desc": "这是一条测试数据" }
根据条件更新文档:
POST /my-index/my-type/_update_by_query
{ "query": { "match": { "age": 24 } }, "script": "ctx._source.age=2;ctx._source.desc+=\"增加描述\"" }
根据ID更新文档中的字段:
POST /my-index/my-type/1/_update
{ "doc": { "age": 1 } }
文档查询相关(支持查询指定字段、分页、排序)
获取所有文档:
GET /my-index/_search
{ "query": { "match_all": {} }, "_source": [ "name", "age" ], "sort": { "age": "asc" }, "from": 0, "size": 2 }
根据ID获取文档:
GET /my-index/my-type/1/_source
范围查询:
GET /my-index/_search
{ "query": { "range": { "age": { "gte": 25, "lt": 30 } } }, "_source": [ "name", "age" ], "sort": { "age": "asc" }, "from": 0, "size": 2 }
单个关键词匹配查询:
GET /my-index/_search
{ "query": { "term": { "age": 25 } }, "_source": [ "name", "age" ], "sort": { "age": "asc" }, "from": 0, "size": 2 }
多个关键词匹配查询(类似于
IN
):GET /my-index/_search
{ "query": { "terms": { "name": [ "001", "002", "003" ] } }, "_source": [ "name", "age" ], "sort": { "age": "asc" }, "from": 0, "size": 2 }
AND
条件查询:GET /my-index/_search
{ "query": { "bool": { "must": [ { "match": { "desc": "谢谢" } }, { "match": { "desc": "你" } } ] } }, "_source": [ "name", "age" ], "sort": { "age": "asc" }, "from": 0, "size": 2 }
OR
条件查询:GET /my-index/_search
{ "query": { "bool": { "should": [ { "match": { "desc": "谢谢" } }, { "match": { "desc": "你" } } ] } }, "_source": [ "name", "age" ], "sort": { "age": "asc" }, "from": 0, "size": 2 }
查询包含指定字段的文档:
GET /my-index/_search
{ "query": { "exists": { "field": "name" } }, "_source": [ "name", "age" ], "sort": { "age": "asc" }, "from": 0, "size": 2 }
获取满足条件的文档总数量:
GET /my-index/_count
{ "query": { "term": { "age": 25 } } }
其他
获取节点列表:
GET /_cat/nodes?format=json
根据指定语句获取分词结果:
GET /_analyze
{ "text": [ "你好,我叫小李子。英文名叫:lee" ], "analyzer": "ik_max_word" }