经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
ElasticSearch 实现分词全文检索 - Restful基本操作
来源:cnblogs  作者:VipSoft  时间:2023/3/6 9:13:54  对本文有异议

Restful 语法

GET 请求:

  1. http://ip:port/index: 查询索引信息
  2. http://ip;port/index/type/doc_id: 查询指定的文档信息

POST 请求:

  1. http://ip;port/index/type/_search: 查询文档,可以在请求体中添加json字符串来代表查询条件
  2. http://ip;port/index/type/doc_id/_update: 修改文档,在请求体中指定ison字符串代表修改的具体信息

PUT 请求:

  1. http://ip;port/index: 创建一个索引,需要在请求体中指定索引的信息,类型,结构
  2. http://ip:port/index/type/_mappings: 代表创建索引时,指定索引文档存储的属性的信息

DELETE 请求:

  1. http://ip;port/index: 删除跑路
  2. http://ip;port/index/type/doc_id: 删除指定的文档

操作

创建一个索引

Kibana 操作
创建 person 索引

  1. # 创建一个索引
  2. PUT /person
  3. {
  4. "settings": {
  5. "number_of_shards": 5, #分片数
  6. "number_of_replicas": 1 #备份数
  7. }
  8. }

返回值

  1. {
  2. "acknowledged" : true,
  3. "shards_acknowledged" : true,
  4. "index" : "person"
  5. }

Kibana 中查看

Stack Management -> 索引管理

image
image
单机版,分片无法存放,所以 yellow 黄色

查看索引信息

  1. # 查看索引信息
  2. GET /person

image

删除索引信息

  1. # 删除索引
  2. DELETE /person

Kibana 操作
image

Field datatypes

  1. String:
  2. text: 一般用于全文检索。将当前的Field进行分词
  3. keyword: 当前 Field 不会被分词
  4. 数值类型:
  5. long integer short byte double float
  6. half_float:精度比float小一半
  7. scaled_float:根据一个longscaled来表达一个浮点型, long=345,scaled=100 => 3.45
  8. 时间类型:
  9. date:针对时间类型指定具体格式
  10. 布尔类型:
  11. boolean:表达truefalse
  12. 二进制类型:
  13. binary:暂时支持Base64 encode string
  14. 范围类型(Range datatypes):
  15. long_range: 赋值时,无序指定具体的内容,只需要存储一个范围即可,指定gt,此,gtelte
  16. integer_range:同上
  17. double_range:同上
  18. float_range: 同上
  19. date_range:同上
  20. ip_range: 同上。
  21. 经纬度类型:
  22. geo_point: 用来存储经纬度,结合定位的经纬度,来计算出距离
  23. IP类型
  24. ip 可以存付IPV4IPV6

其它类型参考官网:

创建索引并指定结构

  1. ## 创建索引并指定结构
  2. PUT /book
  3. {
  4. "settings": {
  5. "number_of_shards": 1,
  6. "number_of_replicas": 1
  7. },
  8. "mappings": {
  9. // 文档存储的Field
  10. "properties":{
  11. "name":{
  12. // 类型
  13. "type":"text",
  14. // 指定分词器
  15. "analyzer":"ik_max_word",
  16. // 指定当前Field可以被作为查询的条件
  17. "index":true,
  18. // 是否需要额外存储
  19. "store":false
  20. },
  21. "author":{
  22. "type":"keyword"
  23. },
  24. "count":{
  25. "type":"long"
  26. },
  27. "on-sale":{
  28. "type":"date",
  29. // 时间类型时格式化方式
  30. "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  31. },
  32. "descr":{
  33. "type":"text",
  34. "analyzer":"ik_max_word"
  35. }
  36. }
  37. }
  38. }

image

文档操作

文档在ES服务中的唯一标识,_index, _type, _id 三个内容为组合,锁定一个文档进行操作

新建文档

自动生成 _id

ID不方便使用

  1. # 添加文档,自动生成 id
  2. POST /book/_doc
  3. {
  4. "name":"太极",
  5. "author":"伏羲",
  6. "count":888,
  7. "on-sale":"2023-02-23",
  8. "descr":"太极生两仪,两仪生四象,四象生八卦"
  9. }

返回

  1. {
  2. "_index" : "book",
  3. "_type" : "_doc", //创建时没指定,默认的
  4. "_id" : "YJJLfYYBGlLaT58LDoV5", // 自动生成的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. }

手动创建ID

  1. POST /book/_doc/1
  2. {
  3. "name":"太极拳",
  4. "author":"陈王廷",
  5. "count":666,
  6. "on-sale":"2023-02-23",
  7. "descr":"掤、捋、挤、按、采、列、肘、靠"
  8. }

返回值

  1. {
  2. "_index" : "book",
  3. "_type" : "_doc",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "result" : "created",
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "_seq_no" : 1,
  13. "_primary_term" : 1
  14. }

修改文档

覆盖式修改

  1. PUT /book/_doc/1
  2. {
  3. "name":"太极拳",
  4. "author":"陈王廷",
  5. "count":666, //如果不赋值,原来的值将被更新成 0
  6. "on-sale":"2023-02-28",
  7. "descr":"掤、捋、挤、按、采、列、肘、靠"
  8. }

doc 修改方式

Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.

  1. POST /book/_doc/1/_update
  2. {
  3. "doc":{
  4. // 指定Field单独修改
  5. "count":6666
  6. }
  7. }

返回值

  1. #! Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
  2. {
  3. "_index" : "book",
  4. "_type" : "_doc",
  5. "_id" : "1",
  6. "_version" : 2,
  7. "result" : "updated",
  8. "_shards" : {
  9. "total" : 2,
  10. "successful" : 1,
  11. "failed" : 0
  12. },
  13. "_seq_no" : 2,
  14. "_primary_term" : 1
  15. }

删除文档

  1. DELETE /book/_doc/YJJLfYYBGlLaT58LDoV5

查看数据

image
image
image
image
image
image

原文链接:https://www.cnblogs.com/vipsoft/p/17147797.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号