分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

ElasticSearch记录(2)curl操作

发布时间:2023-09-06 02:32责任编辑:蔡小小关键词:url

新建和删除 Index

新建 Index,可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫weather的 Index。

curl -XPUT http://192.168.239.101:9200/weather/

服务器返回一个 JSON 对象,里面的acknowledged字段表示操作成功。

{ ?"acknowledged":true, ?"shards_acknowledged":true}

然后,我们发出 DELETE 请求,删除这个 Index。

$ curl -X DELETE http://192.168.239.101:9200/weather

数据操作

5.1 新增记录

向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录。

curl -X PUT http://192.168.239.101:9200/accounts/person -d ‘{ ?"user": "张三", ?"title": "工程师", ?"desc": "数据库管理"}‘ 

服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息。

{ ?"_index":"accounts", ?"_type":"person", ?"_id":"1", ?"_version":1, ?"result":"created", ?"_shards":{"total":2,"successful":1,"failed":0}, ?"created":true}

如果你仔细看,会发现请求路径是/accounts/person/1,最后的1是该条记录的 Id。它不一定是数字,任意字符串(比如abc)都可以。

新增记录的时候,也可以不指定 Id,这时要改成 POST 请求。

curl -X POST http://192.168.239.101:9200/accounts/person -d ‘{ ?"user": "李四", ?"title": "工程师", ?"desc": "系统管理"}‘

上面代码中,向/accounts/person发出一个 POST 请求,添加一个记录。这时,服务器返回的 JSON 对象里面,_id字段就是一个随机字符串。

{ ?"_index":"accounts", ?"_type":"person", ?"_id":"AV3qGfrC6jMbsbXb6k1p", ?"_version":1, ?"result":"created", ?"_shards":{"total":2,"successful":1,"failed":0}, ?"created":true}

注意,如果没有先创建 Index(这个例子是accounts),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,打字的时候要小心,不要写错 Index 的名称。

5.2 查看记录

/Index/Type/Id发出 GET 请求,就可以查看这条记录。

curl -XGET http://192.168.239.101:9200/accounts/person/1?pretty

上面代码请求查看/accounts/person/1这条记录,URL 的参数pretty=true表示以易读的格式返回。

返回的数据中,found字段表示查询成功,_source字段返回原始记录。

{ ?"_index" : "accounts", ?"_type" : "person", ?"_id" : "1", ?"_version" : 1, ?"found" : true, ?"_source" : { ???"user" : "张三", ???"title" : "工程师", ???"desc" : "数据库管理" ?}}

如果 Id 不正确,就查不到数据,found字段就是false

curl -XGET http://192.168.239.101:9200/accounts/person/1?pretty{ ?"_index" : "accounts", ?"_type" : "person", ?"_id" : "1", ?"found" : false}

5.3 删除记录

删除记录就是发出 DELETE 请求。

curl -X DELETE ‘http://192.168.239.101:9200/accounts/person/AWjP8cuP2r1J3ImKYMiA‘

5.4 更新记录

更新记录就是使用 PUT 请求,重新发送一次数据。

curl -X PUT http://192.168.239.101:9200/accounts/person/1 -d ‘{ ?"user": "李四", ?"title": "工程师", ?"desc": "数据库管理"}‘ 
curl -X PUT http://192.168.239.101:9200/accounts/person/1 -d ‘> {> ??"user": "李四",> ??"title": "工程师",> ??"desc": "数据库管理"> }‘ {"_index":"accounts","_type":"person","_id":"1","_version":2,"_shards":{"total":2,"successful":2,"failed":0},"created":false}

上面代码中,我们将原始数据从"数据库管理"改成"数据库管理,软件开发"。 返回结果里面,有几个字段发生了变化。

"_version" : 2,"result" : "updated","created" : false

可以看到,记录的 Id 没变,但是版本(version)从1变成2,操作类型(result)从created变成updatedcreated字段变成false,因为这次不是新建记录。

数据查询

返回所有记录

使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录。

curl http://192.168.239.101:9200/accounts/person/_search?pretty{ ?"took" : 5, ?"timed_out" : false, ?"_shards" : { ???"total" : 5, ???"successful" : 5, ???"failed" : 0 ?}, ?"hits" : { ???"total" : 2, ???"max_score" : 1.0, ???"hits" : [ { ?????"_index" : "accounts", ?????"_type" : "person", ?????"_id" : "2", ?????"_score" : 1.0, ?????"_source" : { ???????"user" : "十大的", ???????"title" : "工程师", ???????"desc" : "数据库管理" ?????} ???}, { ?????"_index" : "accounts", ?????"_type" : "person", ?????"_id" : "1", ?????"_score" : 1.0, ?????"_source" : { ???????"user" : "李四", ???????"title" : "工程师", ???????"desc" : "数据库管理" ?????} ???} ] ?}}

上面代码中,返回结果的 took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录,里面子字段的含义如下。

  • total:返回记录数,本例是2条。
  • max_score:最高的匹配程度,本例是1.0
  • hits:返回的记录组成的数组。

返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。

全文搜索

Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。

curl ‘http://192.168.239.101:9200/accounts/person/_search‘ ?-d ‘{ ???"query" : { "match" : { "desc" : "系统" }}}‘

上面代码使用 Match 查询,指定的匹配条件是desc字段里面包含"软件"这个词。返回结果如下。

{"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.2169777,"hits":[{"_index":"accounts","_type":"person","_id":"2","_score":0.2169777,"_source":{ ?"user": "十大的", ?"title": "工程师", ?"desc": "系统管理"}}]}}

Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。

curl ‘http://192.168.239.101:9200/accounts/person/_search‘ ?-d ‘{ ?"query" : { "match" : { "desc" : "系统" }}, ?"size": 1}‘

上面代码指定,每次只返回一条结果。

还可以通过from字段,指定位移。

curl ‘http://192.168.239.101:9200/accounts/person/_search‘ ?-d ‘{ ?"query" : { "match" : { "desc" : "系统" }}, ?"from": 1, ?"size": 1}‘

上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。

逻辑运算

如果有多个搜索关键字, Elastic 认为它们是or关系。

curl ‘http://192.168.239.101:9200/accounts/person/_search?pretty‘ ?-d ‘> {> ??"query" : { "match" : { "desc" : "数据 系统" }}> }‘{ ?"took" : 9, ?"timed_out" : false, ?"_shards" : { ???"total" : 5, ???"successful" : 5, ???"failed" : 0 ?}, ?"hits" : { ???"total" : 2, ???"max_score" : 0.03182549, ???"hits" : [ { ?????"_index" : "accounts", ?????"_type" : "person", ?????"_id" : "2", ?????"_score" : 0.03182549, ?????"_source" : { ???????"user" : "十大的", ???????"title" : "工程师", ???????"desc" : "系统管理" ?????} ???}, { ?????"_index" : "accounts", ?????"_type" : "person", ?????"_id" : "1", ?????"_score" : 0.027847305, ?????"_source" : { ???????"user" : "李四", ???????"title" : "工程师", ???????"desc" : "数据库管理" ?????} ???} ] ?}}

上面代码搜索的是软件 or 系统

对多个field发起查询:multi_match

curl -XGET http://192.168.239.101:9200/bjsxt/employee/_search?pretty -d ‘{ "query": ?{"multi_match": ??{ ???"query":"bin", ???"fields":["last_name","first_name"], ???"operator":"and" ??} ?}}‘

如果要执行多个关键词的and搜索,必须使用布尔查询。

curl ‘http://192.168.239.101:9200/accounts/person/_search?pretty‘ ?-d ‘{ ?"query": { ???"bool": { ?????"must": [ ???????{ "match": { "desc": "软件" } }, ???????{ "match": { "desc": "系统" } } ?????] ???} ?}}‘

ElasticSearch记录(2)curl操作

原文地址:https://www.cnblogs.com/kpsmile/p/10356875.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved