在index中还有一个mapping,mapping管理了整个index的各个字段的属性,也就是定义了整个index中document的结构。

查看集群健康状态
GET /_cluster/health

索引

创建索引(index):创建索引等同于创建数据库
put /shopping
shopping是索引名称

  1. {
  2. "acknowledged" : true, //响应结果
  3. "shards_acknowledged" : true,
  4. "index" : "shopping" // 索引名称
  5. }

获取某个索引的信息:GET /shopping

  1. {
  2. "shopping" : {
  3. "aliases" : { },
  4. "mappings" : { },
  5. "settings" : {
  6. "index" : {
  7. "routing" : {
  8. "allocation" : {
  9. "include" : {
  10. "_tier_preference" : "data_content"
  11. }
  12. }
  13. },
  14. "number_of_shards" : "1",
  15. "provided_name" : "shopping",
  16. "creation_date" : "1643999970956",
  17. "number_of_replicas" : "1",
  18. "uuid" : "xDoZ9ubEQ3GS-734315icA",
  19. "version" : {
  20. "created" : "7140199"
  21. }
  22. }
  23. }
  24. }
  25. }

查看所有的索引信息: GET /_cat/indices?v

  1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  2. green open .geoip_databases 3ggNwehpTtmFYhF4Y9kshg 1 0 42 62 64.9mb 64.9mb
  3. yellow 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
}

  1. {
  2. "_index" : "shopping",
  3. "_type" : "_doc",
  4. "_id" : "_X0Zxn4BHie2_QkDb_yf", // 这条数据的id,自动分配的
  5. "_version" : 1,
  6. "result" : "created", //创建好
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "_seq_no" : 0,
  13. "_primary_term" : 1
  14. }

POST /shopping/_doc/1001 //自定义id
{
“title”:”小米手机”,
“category”:”小米”,
“images”:”www.baidu.com”,
“price”:4999.00
}

主键查询:GET /shopping/_doc/1001

  1. {
  2. "_index" : "shopping",
  3. "_type" : "_doc",
  4. "_id" : "1001",
  5. "_version" : 1,
  6. "_seq_no" : 1,
  7. "_primary_term" : 1,
  8. "found" : true, // 是否找到, 未找到 false
  9. "_source" : {
  10. "title" : "小米手机",
  11. "category" : "小米",
  12. "images" : "www.baidu.com",
  13. "price" : 4999.0
  14. }
  15. }

全部查询: GET /shopping/_search

  1. {
  2. "took" : 1, // 耗时 ms
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [ // 命中结果
  17. {
  18. "_index" : "shopping",
  19. "_type" : "_doc",
  20. "_id" : "_X0Zxn4BHie2_QkDb_yf",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "title" : "小米手机",
  24. "category" : "小米",
  25. "images" : "www.baidu.com",
  26. "price" : 3999.0
  27. }
  28. }
  29. ]
  30. }
  31. }

全量数据修改:PUT /shopping/_doc/1001
{
“title”:”小米手机2”,
“category”:”小米2”,
“images”:”www.baidu.com”,
“price”:5999.00
}
PUT操作幂等的,全量修改(也叫覆盖),使用的不多

  1. {
  2. "_index" : "shopping",
  3. "_type" : "_doc",
  4. "_id" : "1001",
  5. "_version" : 2, // 版本号+1
  6. "result" : "updated", // 修改结果
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "_seq_no" : 3,
  13. "_primary_term" : 1
  14. }

局部数据修改: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” // 升序
}
}
]
}

多条件查询与范围过滤

  1. GET /shopping/_search
  2. {
  3. "query": { //查询
  4. "bool": { //条件
  5. "must": [ // 表示同时成立的条件 must并且,shold 或者
  6. {
  7. "match": { //匹配条件1
  8. "category": "红米"
  9. }
  10. },
  11. {
  12. "match": { //匹配条件2
  13. "price": 3999
  14. }
  15. }
  16. ],
  17. "filter": [ // 过滤条件,范围条件
  18. {
  19. "range": { //范围
  20. "price": {
  21. "gte": 3000, // >=
  22. "lte": 6000 // <=
  23. }
  24. }
  25. }
  26. ]
  27. }
  28. }
  29. }

注意:一个match 里面只有一个字段属性,不支持多个字段,多条件应该写多个match

全文检索和完全匹配:

match 为全文检索,已经进行了分词,
match_phrase: 为完全匹配

聚合查询:aggs

分组操作

  1. GET /shopping/_search
  2. {
  3. "aggs": { // 聚合查询
  4. "price_group": { // 查询后的名字
  5. "terms": { // 聚合操作之分组
  6. "field": "price" // 对某个字段进行分组
  7. }
  8. }
  9. },
  10. "size": 0 // 返回的元数据为0
  11. }
  12. {
  13. "aggregations" : {
  14. "price_group" : {
  15. "doc_count_error_upper_bound" : 0,
  16. "sum_other_doc_count" : 0,
  17. "buckets" : [ // 分组结果
  18. {
  19. "key" : 3999.0,
  20. "doc_count" : 16
  21. },
  22. {
  23. "key" : 4999.0,
  24. "doc_count" : 1
  25. },
  26. {
  27. "key" : 5999.0,
  28. "doc_count" : 1
  29. }
  30. ]
  31. }
  32. }
  33. }

平均值:

  1. GET /shopping/_search
  2. {
  3. "aggs": {
  4. "price_avg": {
  5. "avg": { // 聚合操作之平均值
  6. "field": "price" // 对某个字段求平均值
  7. }
  8. }
  9. },
  10. "size": 0
  11. }
  12. {
  13. "aggregations" : {
  14. "price_avg" : {
  15. "value" : 4165.666666666667
  16. }
  17. }}

创建映射关系

  1. PUT /user/_mapping
  2. {
  3. "properties":{ // 属性
  4. "name":{ // 字段
  5. "type":"text", // 类型,文本,可分词
  6. "index":true // 索引,该字段可以被索引查询的
  7. },
  8. "sex":{
  9. "type":"keyword", // 类型,不能够分词,必须完全匹配
  10. "index":true
  11. },
  12. "tel":{
  13. "type":"keyword",
  14. "index":false // 不能够被索引,不支持查询
  15. }
  16. }
  17. }
  18. 返回:
  19. {
  20. "user" : {
  21. "aliases" : { },
  22. "mappings" : {
  23. "properties" : {
  24. "name" : {
  25. "type" : "text"
  26. },
  27. "sex" : {
  28. "type" : "keyword"
  29. },
  30. "tel" : {
  31. "type" : "keyword",
  32. "index" : false
  33. }
  34. }
  35. },
  36. "settings" : {
  37. "index" : {
  38. "routing" : {
  39. "allocation" : {
  40. "include" : {
  41. "_tier_preference" : "data_content"
  42. }
  43. }
  44. },
  45. "number_of_shards" : "1",
  46. "provided_name" : "user",
  47. "creation_date" : "1644008590748",
  48. "number_of_replicas" : "1",
  49. "uuid" : "im8zan-vS-Ku0q-m6HXrHg",
  50. "version" : {
  51. "created" : "7140199"
  52. }
  53. }
  54. }
  55. }
  56. }