ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-04-24 18:35:26  阅读:234  来源: 互联网

标签:index shopping 9200 title postmain 改查 match http se


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

标签:index,shopping,9200,title,postmain,改查,match,http,se
来源: https://www.cnblogs.com/q1359720840/p/14697611.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有