Elasticsearch 学习笔记 索引创建、插入、查询、修改、删除

1、索引创建

方式一、在Elasticsearch - head的索引界面中新建


方式二、使用POSTMAN工具发送PUT请求创建



  1. {  
  2.     "settings":{  
  3.         "number_of_shards": 3,  
  4.         "number_of_replicas": 1  
  5.     },  
  6.     "mappings":{  
  7.         "man":{  
  8.             "properties":{  
  9.                 "word_count":{  
  10.                     "type""integer"  
  11.                 },  
  12.                 "author":{  
  13.                      "type""keyword"  
  14.                 },  
  15.                 "title":{  
  16.                     "type""text"  
  17.                 },  
  18.                 "publish_date":{  
  19.                     "type""date",  
  20.                     "format""yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"  
  21.                 }  
  22.             }  
  23.         },  
  24.         "woman":{  
  25.               
  26.         }  
  27.     }  
  28. }  


观察返回结果已经成功。


2、插入

2.1 指定ID值的插入

PUT 127.0.0.1:9200/people/man/1


  1. {  
  2.     "name": "chenjie",  
  3.     "country": "China",  
  4.     "age": 30,  
  5.     "date": "1994-09-27"  
  6. }  




2.2 不指定ID的插入



3、修改

方式一、POST127.0.0.1:9200/people/man/1/_update


  1. {  
  2.     "doc":{  
  3.         "name":"chenjie_update"  
  4.     }  
  5. }  




方式二:通过内置脚本修改 POST127.0.0.1:9200/people/man/1/_update

[plain] view plain copy
  1. {  
  2.     "script":{  
  3.         "lang":"painless",  
  4.         "inline":"ctx._source.age += 10"  
  5.     }  
  6. }  




方式3:在内置脚本中使用参数


  1. {  
  2.     "script":{  
  3.         "lang":"painless",  
  4.         "inline":"ctx._source.age = params.age",  
  5.         "params":{  
  6.             "age":100  
  7.         }  
  8.     }  
  9. }  




4、删除

DELETE127.0.0.1:9200/people/man/1



5、查询

以一个新的索引为例:

使用PUT127.0.0.1:9200/book 创建BOOK索引,并随便插入几条数据


  1. {  
  2.     "settings":{  
  3.         "number_of_shards": 3,  
  4.         "number_of_replicas": 1  
  5.     },  
  6.     "mappings":{  
  7.         "novel":{  
  8.             "properties":{  
  9.                 "word_count":{  
  10.                     "type": "integer"  
  11.                 },  
  12.                 "author":{  
  13.                      "type": "keyword"  
  14.                 },  
  15.                 "title":{  
  16.                     "type": "text"  
  17.                 },  
  18.                 "publish_date":{  
  19.                     "type": "date",  
  20.                     "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"  
  21.                 }  
  22.             }  
  23.         },  
  24.         "woman":{  
  25.               
  26.         }  
  27.     }  
  28. }  

POST 127.0.0.1:9200/book/


  1. {  
  2.     "word_count": "1000",  
  3.     "author": "chenjie",  
  4.     "title": "Java从入门到放弃",  
  5.     "publish_date": "1995-08-19"  
  6. }  


5.1 查询指定ID

GET127.0.0.1:9200/book/novel/1



5.2 查询所有的

POST127.0.0.1:9200/book/_search


  1. 127.0.0.1:9200/book/_search  




5.3 查询所有的,指定from和size

POST127.0.0.1:9200/book/_search

[html] view plain copy
  1. {  
  2.     "query":{  
  3.         "match_all":{}  
  4.     },  
  5.     "from":1,  
  6.     "size":1  
  7. }  



5.4 指定条件查询


  1. {  
  2. <span style="white-space:pre;"> </span>"query":{  
  3. <span style="white-space:pre;">     </span>"match":{  
  4. <span style="white-space:pre;">         </span>"title":"活着"  
  5. <span style="white-space:pre;">     </span>}  
  6. <span style="white-space:pre;"> </span>},  
  7. <span style="white-space:pre;"> </span>"size":1  
  8. }  

猜你喜欢

转载自blog.csdn.net/qq_36663951/article/details/80668905