se使用postmain进行数据的增删改查

2021/4/24 18:55:24

本文主要是介绍se使用postmain进行数据的增删改查,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

es的基本安装

安装遇到的问题

 java本地环境和es环境冲突
 ​
 https://www.cnblogs.com/q1359720840/p/14077049.html
 ​
 ​
 ,看要使用jdk11,本机安装了jdk8,修改默认jdk配置吧,好家伙es自带jdk15,就用它了,
 ​
 配置SE_JAVA_HOME(类似java_home),
 ​
  接下来返回到elasticsearch,在bin目录下找到elasticsearch-env文件,在39-40行处。将JAVA_HOME改成ES_JAVA_HOME
 ​
 我将javahome都改成ES_JAVA_HOME起来了
 ​
 if "%ES_JAVA_HOME%" == "" (
   set JAVA="%ES_HOME%\jdk\bin\java.exe"
   set ES_JAVA_HOME="%ES_HOME%\jdk"
   set JAVA_TYPE=bundled jdk
 ) else (
   set JAVA="%ES_JAVA_HOME%\bin\java.exe"
   set JAVA_TYPE=ES_JAVA_HOME
 ) 
 另一种方式:直接使用本地java环境的11
 ​
 在bin目录下找到elasticsearch-env文件,set JAVA_HOME=jdk11的路径,即可
 ​
 

es基本操作

es所支持的风格

 get:获取 
 post:修改
 put:添加
 delete:删除

 

创建索引

在postman向es发送put请求 http:://127.0.0.1:9200/shopping

image-20210419065330365

当你索引创建成功后-再想通过put创建相同的索引会报错,put幂等

 {
     "error": {
         "root_cause": [
             {
                 "type": "resource_already_exists_exception",
                 "reason": "index [shoppingg/G5fil2wXS6On0_rJa1fYtw] already exists",
                 "index_uuid": "G5fil2wXS6On0_rJa1fYtw",
                 "index": "shoppingg"
             }
         ],
         "type": "resource_already_exists_exception",
         "reason": "index [shoppingg/G5fil2wXS6On0_rJa1fYtw] already exists",
         "index_uuid": "G5fil2wXS6On0_rJa1fYtw",
         "index": "shoppingg"
     },
     "status": 400
 }

那如果使用post呢,post不是幂等的,不允许这样操作

 {
     "error": "Incorrect HTTP method for uri [/shoppingg] and method [POST], allowed: [HEAD, DELETE, GET, PUT]",
     "status": 405
 }

获取索引

get请求:http:://127.0.0.1:9200/shopping

 {
     "shoppingg": {
         "aliases": {},
         "mappings": {},
         "settings": {
             "index": {
                 "creation_date": "1618786330597",
                 "number_of_shards": "1",
                 "number_of_replicas": "1",
                 "uuid": "G5fil2wXS6On0_rJa1fYtw",
                 "version": {
                     "created": "7080099"
                 },
                 "provided_name": "shoppingg"
             }
         }
     }
 }

删除索引

delete : http:://127.0.0.1:9200/shopping

获取全部索引

get请求:http:127.0.0.1:9200/_cat/indinces?v

 health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
 yellow open   shoppingg G5fil2wXS6On0_rJa1fYtw   1   1          0            0       208b           208b
 ​

添加文档数据

post请求: http:127.0.0.1:9200/shopping/_doc

数据为body中,请求方式为json:

{

"title":"小米手机","category":"小米","images":"http://www.gulixueyuan.com/xm.jpg","price":3900.00

}

添加成功后会返回数据集:

 {
     "_index": "shopping",
     "_type": "_doc",
     "_id": "YKtY53gBLVBklMqda4mp",
     "_version": 1,
     "result": "created",
     "_shards": {
         "total": 2,
         "successful": 1,
         "failed": 0
     },
     "_seq_no": 0,
     "_primary_term": 1
 }

post请求的id新增数据是随机生成的,多次相同的请求会生效,因为id不一样

我们一般都是通过id查询数据的,那么如何使用自己的id呢?

 

新增文档数据-自定义id

post请求: http:127.0.0.1:9200/shopping/_doc/1001

如果id存在,那么直接将数据全部update替换

数据为body中,请求方式为json:

{

"title":"小米手机","category":"小米","images":"http://www.gulixueyuan.com/xm.jpg","price":3900.00

}

对于post请求/_doc就是新增 / _update就是修改

修改数据

post : localhost:9200/shopping/_update/1002

BODY:

 {
  "doc": {
      "title": "小米手机12",
      "category": "小米12",
      "images": "http://www.gulixueyuan.com/xm.jpg",
      "price": 3900.00
  }
 }
 {
  "_index": "shopping",
  "_type": "_doc",
  "_id": "1002",
  "_version": 4,
  "result": "updated",
  "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
  },
  "_seq_no": 10,
  "_primary_term": 1
 }

 

查询数据

get 请求:http:127.0.0.1:9200/shopping/_doc/1001

 

 {
     "_index": "shopping",
     "_type": "_doc",
     "_id": "1001",
     "_version": 1,
     "_seq_no": 3,
     "_primary_term": 1,
     "found": true,
     "_source": {
         "title": "小米手机",
         "category": "小米",
         "images": "http://www.gulixueyuan.com/xm.jpg",
         "price": 3900.00
     }
 }

修改文档

put :http:127.0.0.1:9200/shopping/_doc/1003

 {
 "title":"小米手机222"
 }

 

添加文档数据-id不能重复

put :http:127.0.0.1:9200/shopping/_create/1003

id重复会出现异常

 {
     "error": {
         "root_cause": [
             {
                 "type": "version_conflict_engine_exception",
                 "reason": "[1003]: version conflict, document already exists (current version [1])",
                 "index_uuid": "oQ0XOQPvSQmtXijIHbb-4w",
                 "shard": "0",
                 "index": "shopping"
             }
         ],
         "type": "version_conflict_engine_exception",
         "reason": "[1003]: version conflict, document already exists (current version [1])",
         "index_uuid": "oQ0XOQPvSQmtXijIHbb-4w",
         "shard": "0",
         "index": "shopping"
     },
     "status": 409
 }

如果不传id会报错

 {
  "error": "Incorrect HTTP method for uri [/shopping/_create] and method [PUT], allowed: [POST]",
  "status": 405
 }

查询某个索引下的所有数据

get: localhost:9200/shopping/_search

 {
     "took": 33,
     "timed_out": false,
     "_shards": {
         "total": 1,
         "successful": 1,
         "skipped": 0,
         "failed": 0
     },
     "hits": {
         "total": {
             "value": 6,
             "relation": "eq"
         },
         "max_score": 1.0,
         "hits": [
             {
                 "_index": "shopping",
                 "_type": "_doc",
                 "_id": "YKtY53gBLVBklMqda4mp",
                 "_score": 1.0,
                 "_source": {
                     "title": "小米手机",
                     "category": "小米",
                     "images": "http://www.gulixueyuan.com/xm.jpg",
                     "price": 3900.00
                 }
             },
             {
                 "_index": "shopping",
                 "_type": "_doc",
                 "_id": "1001",
                 "_score": 1.0,
                 "_source": {
                     "title": "小米手机",
                     "category": "小米",
                     "images": "http://www.gulixueyuan.com/xm.jpg",
                     "price": 3900.00
                 }
             },
             {
                 "_index": "shopping",
                 "_type": "_doc",
                 "_id": "Yatg53gBLVBklMqdd4ku",
                 "_score": 1.0,
                 "_source": {
                     "title": "小米手机",
                     "category": "小米",
                     "images": "http://www.gulixueyuan.com/xm.jpg",
                     "price": 3900.00
                 }
             },
             {
                 "_index": "shopping",
                 "_type": "_doc",
                 "_id": "1002",
                 "_score": 1.0,
                 "_source": {
                     "title": "小米手机"
                 }
             },
             {
                 "_index": "shopping",
                 "_type": "_doc",
                 "_id": "1003",
                 "_score": 1.0,
                 "_source": {
                     "title": "小米手机"
                 }
             },
             {
                 "_index": "shopping",
                 "_type": "_doc",
                 "_id": "serach",
                 "_score": 1.0,
                 "_source": {
                     "title": "小米手机2"
                 }
             }
         ]
     }
 }

删除文档

delete:http://127.0.0.1:9200/shopping/_doc/1001

 

复杂查询-条件查询-会中文乱码

get :localhost:9200/shopping/_search?q=title:小米

这种搜索直接在参数拼接,容易出现中文乱码

复杂查询-条件查询

get :localhost:9200/_search

 {"query":{
 ​
 "match":{"title":"小米"}
 ​
 }}

复杂查询-查询全部

 

 

复杂查询-条件查询+分页

get: localhost:9200/_search

 {"query":{
  "match_all":{}
 ​
 },
 "from":0,
  "size":2
 ​
 }

 

复杂查询-条件查询+分页 +显示某列内容

get : localhost:9200/_search

备注:分页然后只查询title

 请求的 body:
 {"query":{
  "match_all":{}
 ​
 },
 "from":0,
  "size":2,
 "_source":["title"]
 }

复杂查询-条件查询+对内容结果排序

get : localhost:9200/_search

备注:分页然后只查询title

 请求的 body:
 {"query":{
 "match_all":{}
 },
 "sort":{
 "price":{
     "order":"asc"
 }
 }
 ​
 }

多条件查询-查数据同时存在

localhost:9200/_search

query:代表查询

多个条件: bool

多个条件同时成立,必须的意思,里面是数组: must

或者的意思 should:

匹配的条件: match

 

 get : localhost:9200/_search
 ​
 body:
 ​
 {"query":{
     "bool":{
 "must":[   
     {
     "match":{
     "category":"小米"
 }}
 ,
 {
     "match":{
         "price":"3900"
     }
 }
 ]
     }
 }
 ​
 }

 

多条件查询-查数据或者存在

 get : localhost:9200/_search
 ​
 {"query":{
     "bool":{
         "should":[
 {"match":{
     "title":"小米"
 }},{"match":{
     "title":"华为"
 }}
         ]
     }
 }}

多条件查询-查数据范围查找

 get : localhost:9200/_search
 ​
 {"query":{
     "bool":{
        "filter":{
        "range":{"price":{
            "gt":4000
        }}
        }
     }
 }}

需要注意,即使查询一个词也会查询出相关的数据,倒排索引的关系,全文检索

完全匹配操作

localhost:9200/_search

如果match_phrase 这个查询是完全匹配,如果match ,是倒排索引匹配,match_phrase 输入的不全可以匹配到带有相关名字的,但是不进行拆分,而match 是进行拆分匹配的。

 ​
 ​
 {"query":{
 "match_phrase":{
 "title":"小米"
 }
 }
 }

高亮显示

localhost:9200/_search

 {"query":{
 "match":{
 "title":"小华"
 }
 }
 ,"highlight":{
     "fields":{
         "title":{}
     }
 }
 }

聚合查询 aggs 分组terms

 ​
 ​
 {
 "aggs":{
 "price_group":{//名称随意起
 "terms":{
 "field":"price"//分组字段
 }
 }
 }
 }

结果:是带着原始数据的,如果不要原始数据,可以增加 size:0

image-20210424173956198

 {
 "aggs":{
 "price_group":{
 "terms":{
 "field":"price"
 }
 }
 },"size":0
 }

求平均值 avg

 {"aggs":{
 "price_avg":{
 "avg":{
 "field":"price"
 }
 }
 },"size":0
 ​
 }
 ​

查看es映射关系

1.创建索引

put http://localhost:9200/user

2.创建结构信息

put http://127.0.0.1:9200/user/_mapping



这篇关于se使用postmain进行数据的增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程