在index中还有一个mapping,mapping管理了整个index的各个字段的属性,也就是定义了整个index中document的结构。
查看集群健康状态
GET /_cluster/health
索引
创建索引(index):创建索引等同于创建数据库
put /shopping
shopping是索引名称
{"acknowledged" : true, //响应结果"shards_acknowledged" : true,"index" : "shopping" // 索引名称}
获取某个索引的信息:GET /shopping
{"shopping" : {"aliases" : { },"mappings" : { },"settings" : {"index" : {"routing" : {"allocation" : {"include" : {"_tier_preference" : "data_content"}}},"number_of_shards" : "1","provided_name" : "shopping","creation_date" : "1643999970956","number_of_replicas" : "1","uuid" : "xDoZ9ubEQ3GS-734315icA","version" : {"created" : "7140199"}}}}}
查看所有的索引信息: GET /_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.sizegreen open .geoip_databases 3ggNwehpTtmFYhF4Y9kshg 1 0 42 62 64.9mb 64.9mbyellow open shopping xDoZ9ubEQ3GS-734315icA 1 1 0 0 208b 208b
删除某一个索引:DELETE /shoppin
文档
添加数据:索引创建完后开始添加数据(也就是文档json格式数据)使用post方法, json 格式的数据
POST /shopping/_doc //系统分配id
{
“title”:”小米手机”,
“category”:”小米”,
“images”:”www.baidu.com”,
“price”:3999.00
}
{"_index" : "shopping","_type" : "_doc","_id" : "_X0Zxn4BHie2_QkDb_yf", // 这条数据的id,自动分配的"_version" : 1,"result" : "created", //创建好"_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1}
POST /shopping/_doc/1001 //自定义id
{
“title”:”小米手机”,
“category”:”小米”,
“images”:”www.baidu.com”,
“price”:4999.00
}
主键查询:GET /shopping/_doc/1001
{"_index" : "shopping","_type" : "_doc","_id" : "1001","_version" : 1,"_seq_no" : 1,"_primary_term" : 1,"found" : true, // 是否找到, 未找到 false"_source" : {"title" : "小米手机","category" : "小米","images" : "www.baidu.com","price" : 4999.0}}
全部查询: GET /shopping/_search
{"took" : 1, // 耗时 ms"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 1.0,"hits" : [ // 命中结果{"_index" : "shopping","_type" : "_doc","_id" : "_X0Zxn4BHie2_QkDb_yf","_score" : 1.0,"_source" : {"title" : "小米手机","category" : "小米","images" : "www.baidu.com","price" : 3999.0}}]}}
全量数据修改:PUT /shopping/_doc/1001
{
“title”:”小米手机2”,
“category”:”小米2”,
“images”:”www.baidu.com”,
“price”:5999.00
}
PUT操作幂等的,全量修改(也叫覆盖),使用的不多
{"_index" : "shopping","_type" : "_doc","_id" : "1001","_version" : 2, // 版本号+1"result" : "updated", // 修改结果"_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 3,"_primary_term" : 1}
局部数据修改:post /shopping/_update/1001
{
“doc”:{
“title”:”苹果” // 待修改的某个字段或者多个字段
}
}
注意:必须使用_update 才可以局部更新字段,若仍使用_doc则会将该条数据原有的信息被更新后的数据进行覆盖,
POST /shopping/_doc/1001
{
“doc”: {
“title”:”苹果1”
}
}
结果:同 PUT /shopping/_doc/1001 这个操作
{
“_index” : “shopping”,
“_type” : “_doc”,
“_id” : “1001”,
“_version” : 14,
“_seq_no” : 15,
“_primary_term” : 1,
“found” : true,
“_source” : {
“doc” : {
“title” : “苹果1”
}
}
}
删除数据:DELETE /shopping/_doc/1001
条件查询
GET /shopping/_search
{
“query”: { // 查询语句
“match”: { // 匹配条件
“category”: “小米” // 相关字段的值
}
}
}
分页查询&排序:
GET /shopping/_search
{
“query”: {
“match_all”: {
}
},
“from”: 1, // 游标 (页码-1)* size
“size”: 3, // 每页大小
“_source”: [“title”], // 可以选择返回来的指定字段
“sort”: [ // 排序
{
“price”: { // 字段
“order”: “desc” // 升序
}
}
]
}
多条件查询与范围过滤
GET /shopping/_search{"query": { //查询"bool": { //条件"must": [ // 表示同时成立的条件 must并且,shold 或者{"match": { //匹配条件1"category": "红米"}},{"match": { //匹配条件2"price": 3999}}],"filter": [ // 过滤条件,范围条件{"range": { //范围"price": {"gte": 3000, // >="lte": 6000 // <=}}}]}}}
注意:一个match 里面只有一个字段属性,不支持多个字段,多条件应该写多个match
全文检索和完全匹配:
match 为全文检索,已经进行了分词,
match_phrase: 为完全匹配
聚合查询:aggs
分组操作
GET /shopping/_search{"aggs": { // 聚合查询"price_group": { // 查询后的名字"terms": { // 聚合操作之分组"field": "price" // 对某个字段进行分组}}},"size": 0 // 返回的元数据为0个}{"aggregations" : {"price_group" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [ // 分组结果{"key" : 3999.0,"doc_count" : 16},{"key" : 4999.0,"doc_count" : 1},{"key" : 5999.0,"doc_count" : 1}]}}}
平均值:
GET /shopping/_search{"aggs": {"price_avg": {"avg": { // 聚合操作之平均值"field": "price" // 对某个字段求平均值}}},"size": 0}{"aggregations" : {"price_avg" : {"value" : 4165.666666666667}}}
创建映射关系
PUT /user/_mapping{"properties":{ // 属性"name":{ // 字段"type":"text", // 类型,文本,可分词"index":true // 索引,该字段可以被索引查询的},"sex":{"type":"keyword", // 类型,不能够分词,必须完全匹配"index":true},"tel":{"type":"keyword","index":false // 不能够被索引,不支持查询}}}返回:{"user" : {"aliases" : { },"mappings" : {"properties" : {"name" : {"type" : "text"},"sex" : {"type" : "keyword"},"tel" : {"type" : "keyword","index" : false}}},"settings" : {"index" : {"routing" : {"allocation" : {"include" : {"_tier_preference" : "data_content"}}},"number_of_shards" : "1","provided_name" : "user","creation_date" : "1644008590748","number_of_replicas" : "1","uuid" : "im8zan-vS-Ku0q-m6HXrHg","version" : {"created" : "7140199"}}}}}
