ElasticSearch学习笔记
预备知识-Restful
风闻ElasticSearch API是纯Restful风格的,那他喵的什么是Restful?
起源
在没有前后端分离概念之前,一个网站的完成总是"all in one",在这个阶段,页面、数据、渲染全部在服务端完成,这样做的最大的弊端是后期维护,扩展极其痛苦,开发人员必须同时具备前后端知识。
REST
基本操作
索引
1.创建索引
PUT /索引名称
PUT /haoke
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "haoke" }
2.查看索引
GET /索引名称
GET /haoke
{ "haoke" : { "aliases" : { }, "mappings" : { }, "settings" : { "index" : { "routing" : { "allocation" : { "include" : { "_tier_preference" : "data_content" } } }, "number_of_shards" : "1", "provided_name" : "haoke", "creation_date" : "1625209127283", "number_of_replicas" : "1", "uuid" : "TRiu2s7fTv-RExr4O9zT5g", "version" : { "created" : "7130299" } } } } }
3.删除索引
DELETE /索引名称
DELETE /haoke
{
"acknowledged" : true
}
文档
1.创建文档
PUT /索引名称/类型/ID
PUT /haoke/user/1001
{
"name": "张翼德",
"sex": "男",
"age": 30,
"address": "涿郡屠夫"
}
2.查看指定文档
GET /索引名称/类型/ID
GET /haoke/user/1001
{ "_index" : "haoke", "_type" : "user", "_id" : "1001", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "name" : "张翼德", "sex" : "男", "age" : 30, "address" : "涿郡屠夫" } }
3.查看所有文档
GET /索引名称/_search
GET /haoke/user/_search
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "haoke", "_type" : "user", "_id" : "1001", "_score" : 1.0, "_source" : { "name" : "张翼德", "sex" : "男", "age" : 30, "address" : "涿郡屠夫" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1002", "_score" : 1.0, "_source" : { "name" : "关羽", "sex" : "男", "age" : 32, "address" : "河东解良" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1003", "_score" : 1.0, "_source" : { "name" : "刘备", "sex" : "男", "age" : 36, "address" : "世居巷里" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1004", "_score" : 1.0, "_source" : { "name" : "曹操", "sex" : "男", "age" : 36, "address" : "世之枭雄" } } ] } }
4.分页查询文档
GET /索引名称/_search?size=xx&from=xx
from即从多少开始插,默认0
size即一次查多少条
类似 mysql 中 limit 0, 10
GET /haoke/_search?size=2&from=1
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_score" : 1.0,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1003",
"_score" : 1.0,
"_source" : {
"name" : "刘备",
"sex" : "男",
"age" : 36,
"address" : "世居巷里"
}
}
]
}
}
5.删除指定文档
DELETE /索引名称/类型/ID
DELETE /haoke/user/1003
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1003",
"_version" : 4,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1
}
6.修改指定文档
POST /索引名称/类型/ID
POST /haoke/user/1003
{
"name": "魏延",
"sex": "男",
"age": 38,
"address": "北京西三旗"
}
响应结果:
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1003",
"_version" : 2,
"_seq_no" : 8,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "魏延",
"sex" : "男",
"age" : 38,
"address" : "北京西三旗"
}
}
查询操作
1.查询当前类型中的所有文档
GET /索引名称/索引类型/_search
GET /haoke/user/_search
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1001",
"_score" : 1.0,
"_source" : {
"name" : "张翼德",
"sex" : "男",
"age" : 30,
"address" : "涿郡屠夫"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_score" : 1.0,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_score" : 1.0,
"_source" : {
"name" : "曹操",
"sex" : "男",
"age" : 36,
"address" : "世之枭雄"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1003",
"_score" : 1.0,
"_source" : {
"name" : "魏延",
"sex" : "男",
"age" : 38,
"address" : "北京西三旗"
}
}
]
}
}
2.条件查询
如果查询年龄等于36岁的
GET /haoke/user/_search?q=age:36
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_score" : 1.0,
"_source" : {
"name" : "曹操",
"sex" : "男",
"age" : 36,
"address" : "世之枭雄"
}
}
]
}
}
3.范围查询
GET /索引名称/类型/_search?q=xxx[xx TO xx] ,注意TO大写
GET /haoke/user/_search?q=age[30 TO 36]
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1001",
"_score" : 1.0,
"_source" : {
"name" : "张翼德",
"sex" : "男",
"age" : 30,
"address" : "涿郡屠夫"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_score" : 1.0,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_score" : 1.0,
"_source" : {
"name" : "曹操",
"sex" : "男",
"age" : 36,
"address" : "世之枭雄"
}
}
]
}
}
4. 根据多个ID批量查询_mget
GET /索引名称/类型/_mget
相当于:select * from user where id in (1001, 1002)
GET /haoke/user/_mget
{
"ids": ["1001", "1002"]
}
结果:
{
"docs" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1001",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "张翼德",
"sex" : "男",
"age" : 30,
"address" : "涿郡屠夫"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
}
}
]
}
5.查询年龄小于等于36岁的憨憨
GET /索引名称/类型/_search?q=age:<=36
GET /haoke/user/_search?q=age:<=36
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1001",
"_score" : 1.0,
"_source" : {
"name" : "张翼德",
"sex" : "男",
"age" : 30,
"address" : "涿郡屠夫"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_score" : 1.0,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_score" : 1.0,
"_source" : {
"name" : "曹操",
"sex" : "男",
"age" : 36,
"address" : "世之枭雄"
}
}
]
}
}
6.查询年龄大于28岁的
GET /索引名称/类型/_search?q=age:>28
GET /haoke/user/_search?q=age:>28
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "haoke", "_type" : "user", "_id" : "1001", "_score" : 1.0, "_source" : { "name" : "张翼德", "sex" : "男", "age" : 30, "address" : "涿郡屠夫" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1002", "_score" : 1.0, "_source" : { "name" : "关羽", "sex" : "男", "age" : 32, "address" : "河东解良" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1004", "_score" : 1.0, "_source" : { "name" : "曹操", "sex" : "男", "age" : 36, "address" : "世之枭雄" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1003", "_score" : 1.0, "_source" : { "name" : "魏延", "sex" : "男", "age" : 38, "address" : "北京西三旗" } } ] } }
7.分页查询
GET /索引名称/类型/_search?size=xx&from=xx
GET /haoke/user/_search?size=2&from=1
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_score" : 1.0,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_score" : 1.0,
"_source" : {
"name" : "曹操",
"sex" : "男",
"age" : 36,
"address" : "世之枭雄"
}
}
]
}
}
8.输出指定查询结果字段_source
比如:只返回name, age字段
GET /索引名称/类型/_search?_source=name,age
GET /索引名称/类型/_search?_source=name,age
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "haoke", "_type" : "user", "_id" : "1001", "_score" : 1.0, "_source" : { "name" : "张翼德", "age" : 30 } }, { "_index" : "haoke", "_type" : "user", "_id" : "1002", "_score" : 1.0, "_source" : { "name" : "关羽", "age" : 32 } }, { "_index" : "haoke", "_type" : "user", "_id" : "1004", "_score" : 1.0, "_source" : { "name" : "曹操", "age" : 36 } }, { "_index" : "haoke", "_type" : "user", "_id" : "1003", "_score" : 1.0, "_source" : { "name" : "魏延", "age" : 38 } } ] } }
9. 对查询结果排序
GET /索引名称/类型/_search?sort=字段:desc/asc
GET /haoke/user/_search?sort=age:desc
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1003",
"_score" : null,
"_source" : {
"name" : "魏延",
"sex" : "男",
"age" : 38,
"address" : "北京西三旗"
},
"sort" : [
38
]
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_score" : null,
"_source" : {
"name" : "曹操",
"sex" : "男",
"age" : 36,
"address" : "世之枭雄"
},
"sort" : [
36
]
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_score" : null,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
},
"sort" : [
32
]
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1001",
"_score" : null,
"_source" : {
"name" : "张翼德",
"sex" : "男",
"age" : 30,
"address" : "涿郡屠夫"
},
"sort" : [
30
]
}
]
}
}
文档批量操作
1.批量获取文档数据
批量获取文档数据是通过_mget API实现的
----------------在URL中不指定index、type
GET _mget
{
"docs":
[
{
"_index": "haoke",
"_type": "user",
"_id": "1004",
"_source": ["name", "age"]
},
{
"_index": "haoke",
"_type": "user",
"_id": "1003",
"_source": ["name", "age"]
}
]
}
查询结果:
{
"docs" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "曹操",
"age" : 36
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1003",
"_version" : 2,
"_seq_no" : 8,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "魏延",
"age" : 38
}
}
]
}
------------------在URL中指定index
GET /haoke/_mget
{
"docs":
[
{
"_type": "user",
"_id": "1003",
"_source": ["name", "age"]
},
{
"_type": "user",
"_id": "1004",
"_source": ["name", "age"]
}
]
}
查询结果:
{
"docs" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1003",
"_version" : 2,
"_seq_no" : 8,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "魏延",
"age" : 38
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1004",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "曹操",
"age" : 36
}
}
]
}
------------------在URL中指定index、type
GET /haoke/user/_mget
{
"docs":
[
{
"_id": "1001"
},
{
"_id": "1002"
}
]
}
查询结果:
{
"docs" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1001",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "张翼德",
"sex" : "男",
"age" : 30,
"address" : "涿郡屠夫"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1002",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "关羽",
"sex" : "男",
"age" : 32,
"address" : "河东解良"
}
}
]
}
2.批量操作文档数据
POST _bulk
{"create": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"name": "马超", "sex": "男", "age": 34, "address": "西凉"}
{"create": {"_index": "haoke", "_type": "user", "_id": "1006"}}
{"name": "庞德", "sex": "男", "age": 33, "address": "西凉"}
{"create": {"_index": "haoke", "_type": "user", "_id": "1007"}}
{"name": "夏侯惇", "sex": "男", "age": 36, "address": "大魏"}
2.普通创建或者全量替换index
POST _bulk
{"index": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"name": "马岱", "sex": "男", "age": 32, "address": "西凉"}
{"index": {"_index": "haoke", "_type": "user", "_id": "1008"}}
{"name": "诸葛亮", "sex": "男", "age": 28, "address": "南阳"}
GET _mget
{
"docs":
[
{
"_index": "haoke",
"_type": "user",
"_id": "1005"
},
{
"_index": "haoke",
"_type": "user",
"_id": "1008"
}
]
}
{
"docs" : [
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1005",
"_version" : 3,
"_seq_no" : 13,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "马岱",
"sex" : "男",
"age" : 32,
"address" : "西凉"
}
},
{
"_index" : "haoke",
"_type" : "user",
"_id" : "1008",
"_version" : 1,
"_seq_no" : 14,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "诸葛亮",
"sex" : "男",
"age" : 28,
"address" : "南阳"
}
}
]
}
3.批量删除delete
POST _bulk
{"delete": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"delete": {"_index": "haoke", "_type": "user", "_id": "1006"}}
{"delete": {"_index": "haoke", "_type": "user", "_id": "1007"}}
POST _bulk
{"update": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"doc": {"name": "马岱"}}
{"update": {"_index": "haoke", "_type": "user", "_id": "1007"}}
{"doc": {"name": "夏侯渊"}}
DSL语言高级查询
1.无条件查询
GET /haoke/user/_search
{
"query":
{
"match_all": {}
}
}
2.有条件查询
-------------- 模糊匹配:
- query : 指定匹配的值
- operator : 匹配条件类型
- minmum_should_match : 指定最小匹配的数量
------------------ 精准匹配:
GET /haoke/user/_search
{
"query":
{
"term": {"age": 36}
}
}
terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值
示例:
GET /haoke/user/_search
{
"query":
{
"terms": {"age": [34, 36]}
}
}
range 过滤允许我们按照指定范围查找一批数据
范围操作符包含:
-
gt : 大于
-
gte:: 大于等于
-
lt : 小于
-
lte: 小于等
GET /haoke/user/_search
{
"query":
{
"range": {
"age": {
"gte": 33,
"lte": 36
}
}
}
}
exists 查询可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL 条件
GET /haoke/user/_search
{
"query":
{
"exists": {"field": "hobby"}
}
}
ids通过ID批量查询
GET /haoke/user/_search
{
"query":
{
"ids": {"values": ["1001", "1002"]}
}
}

浙公网安备 33010602011771号