Elasticsearch7.X entry-learning Lesson notes ---- basic CRUD operations and api

Original: Elasticsearch7.X entry-learning Lesson notes ---- basic CRUD operations and api

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_36697880/article/details/100539044

The following operations are kibana is the operation of the tool dev Tools

   

First, the index


       An index is a collection has somewhat similar features of the document. For example, you can have an index of customer data, another index catalog, there is an index order data. An index consists of a name is identified (must be all lowercase letters), and when we want to index, search for the index corresponding to this document, update, and delete, to have to use that name.


   
   
  1. #查看所有索引相关信息
  2. GET /_cat/indices?v
  3. #创建索引
  4. PUT customer
  5. #查看所有索引相关信息
  6. GET customer
  7. #删除索引
  8. DELETE customer
  9. #查看索引的文档总数
  10. GET kibana_sample_data_ecommerce/_count
  11. #查看前10条文档,了解文档格式
  12. POST kibana_sample_data_ecommerce/_search
  13. {
  14. }

Second, the document CRUD


A document is a basic unit of information that can be indexed. For example, you can have one client document, a document of a particular product, of course, one can have a document an order. Documentation JSON (Javascript Object Notation) format to represent.

  

     1 create create a document

       _create to create the specified type is not the type name of the default _doc specified ID if it already exists on the error


   
   
  1. #create document. _create 指定类型为create并不是type名称 默认是_doc 指定 ID 如果已经存在,就报错
  2. PUT users/_create/ 1
  3. {
  4.       "user" : "Jack2",
  5.     "post_date" : "2019-05-15T14:12:12",
  6.     "message" : "trying out Elasticsearch"
  7. }

   If id is the existence of a document would create an error

   

   2 index create documents

     index and create not the same place: index if the document is good, it indexes new document. If the document exists on the cover of the original document content. Version Information +1

 

3 GET query index

       According to the document id get query document content

    

4 Update modify the document

    update 修改文档 不会删除原文档 而是在 文档的基础上更新文档中的字段内容

   #  _update 才会根据文档中字段信息 在原文档上增加字段 必须带有doc
  POST users/_update/1/
   {
    "doc":{
        "post_date" : "2019-05-18T14:12:12",
        "message" : "trying out ElasticsearchOut",
        "phone" : "1806185",
        "pubtest":[1,2,3],
        "pubtest2":"[1,2,3]"
     }
 }
 

   

5 DELETE 删除文档

     ### Delete by Id
     # 删除文档
    DELETE users/_doc/1

6 查看索引的 maping 信息

   maping 相当于表的 schema 

     GET users/_mapping

     

三 文档批量操作

 bulk api 批量操作

1、bulk相当于数据库里的bash操作。

2、引入批量操作bulk,提高工作效率,你想啊,一批一批添加与一条一条添加,谁快?

3、bulk API可以帮助我们同时执行多个请求

4、bulk的格式:

action:index/create/update/delete

metadata:_index,_type,_id

request body:_source (删除操作不需要加request body)

                   { action: { metadata }}

                   { request body        }

单条操作失败不会影响其他操作

5、bulk里为什么不支持get呢?

  答:批量操作,里面放get操作,没啥用!所以,官方也不支持。

6、create 和index的区别

  如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行。

 7、bulk一次最大处理多少数据量?

  bulk会把将要处理的数据载入内存中,所以数据量是有限制的,最佳的数据量不是一个确定的数值,它取决于你的硬件,你的文档大小以及复杂性,你的索引以及搜索的负载。

  一般建议是1000-5000个文档,如果你的文档很大,可以适当减少队列,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(即$ES_HOME下的config下的elasticsearch.yml)中。


   
   
  1. ### Bulk 操作 批量操作,其中一步错误,不影响其他操作
  2. PUT _bulk
  3. { "index":{ "_index": "test", "_id": "1"}}
  4. { "name": "dukun", "post_date" : "2019-05-18T14:12:12", "age": 18, "phone" : "1806185", "sex": "男"}
  5. { "index":{ "_index": "test", "_id": "2"}}
  6. { "name": "dukun02", "post_date" : "2019-05-18T14:12:12", "age": 25, "phone" : "19888", "sex": "男"}
  7. { "create":{ "_index": "test", "_id": "3"}}
  8. { "name": "dukun03", "post_date" : "2019-05-19T14:12:12"}
  9. { "update":{ "_index": "test", "_id": "3"}}
  10. { "doc" :{ "name": "dukun04", "phone" : "1806185", "age": 28, "sex": "男"}}
  11. { "delete":{ "_index": "test", "_id": "4"}}

  mget 批量读取

    批量操作可以减少网络连接所带来的开销,提供性能。

        


   
   
  1. ##批量查询mget
  2. GET /_mget
  3. {
  4. "docs":[
  5. {
  6. "_index" : "test",
  7. "_id" : "1"
  8. },
  9. {
  10. "_index" : "test",
  11. "_id" : "3"
  12. }
  13. ]
  14. }
  15. ##批量查询mget url中指定索引 可以简化如下
  16. GET test/_mget
  17. {
  18. "ids":[1,2,3]
  19. }
  20. #批量查询mget 中_source过滤默认_source字段会返回所有的内容,
  21. 你也可以通过_source进行过滤。
  22. 比如使用_source,_source_include 包含字段,_source_exclude 查询排除字段. "_source" : false 不显示字段
  23. GET test/_mget
  24. {
  25. "docs":[
  26. { "_id": "1",
  27. "_source" : false
  28. },
  29. { "_id": "1",
  30. "_source" : true
  31. },
  32. { "_id": "2",
  33. "_source" : [ "name", "age"]
  34. },
  35. {
  36. "_id": "3",
  37. "_source":{
  38. "include":[ "name", "age", "sex"]
  39. }
  40. },
  41. {
  42. "_id": "3",
  43. "_source":{
  44. "exclude":[ "name", "age", "sex"]
  45. }
  46. }
  47. ]
  48. }

   批量查询  _msearch

      

          使用match_all进行查询,并且只返回第一个文档。如果没有指定size的值,则默认返回前10个文档
也可以指定返回从哪个文档开始,返回多少文档.

        

took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
timed_out —— 指明这个搜索是否超时
_shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
hits —— 搜索结果
hits.total —— 能够匹配我们查询标准的文档的总数目
hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11612413.html