欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

ElasticSearch操作数据

时间:2023-06-20
一、添加数据

插入有两种方式,一种是指定id,一种是es自动分配id

# 先创建一个index1索引# 由于不建议使用多类型,所以创建只有一个类型的索引,默认类型名为_docPUT 'http://localhost:9200/index1'{ "mappings": { "properties":{ "name":{ "type":"keyword" }, "age":{ "type": "long" } } }}

1.1 指定ID

# /[index]/[type]/[id]# 默认type为_doc,指定为no1# 可以加上?op_type=create,会在重复id时报错导致插入失败,否则会更新该id的属性值PUT 'http://localhost:9200/index1/_doc/no1?op_type=create'{"name":"123","age":"123"}

1.2 自动生成id

POST 'http://localhost:9200/index1/_doc'{"name":"123","age":"123"}# 结果{ "_index" : "accounts", "_type" : "person", "_id" : "0dwdw4548", # 自动生成的id "_version" : 1, "result" : "created", "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "_seq_no": 1, "_primary_term": 1}

二、删除数据

# 通过id删除DELETe 'http://localhost:9200/index1/_doc/no1'

三、更新数据

更新文档数据可以根据id修改,而id修改支持普通修改和脚本修改

3.1 普通修改

# 通过id更新记录POST 'http://localhost:9200/index1/_doc/no1/_update'{"doc":{"name":"123","age":"321"}}

3.2 脚本修改

# 关键字”script”: 标志以脚本的方式修改文档# “lang”: 表示以何种脚本语言进行修改, “painless”表示以es内置的脚本语言进行修改、# 此外es还支持多种脚本语言, 如Python, js等等# “inline”:指定脚本内容 “ctx”代表es上下文, _source 代表文档 POST 'http://localhost:9200/index1/_doc/no1/_update'{ "script": { "lang": "painless", "inline": "ctx._source.name = params.name; ctx._source.age = params.age", "params": {"name": "312", "age": 30 } }}

四、数据查询 4.1 通过id查询数据

GET 'http://localhost:9200/index1/_doc/no1'# 结果{ "_index" : "accounts", "_type" : "person", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "user" : "123", "title" : "123", "desc" : "123" }}

4.2 查询所有记录

# 使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录。GET 'http://localhost:9200/index1/_doc/_search'{ "took":2, "timed_out":false, "_shards":{"total":5,"successful":5,"failed":0}, "hits":{ "total":2, "max_score":1.0, "hits":[ { "_index":"accounts", "_type":"person", "_id":"AV3qGfrC6jMbsbXb6k1p", "_score":1.0, "_source": { "user": "李四", "title": "工程师", "desc": "系统管理" } }, { "_index":"accounts", "_type":"person", "_id":"1", "_score":1.0, "_source": { "user" : "123", "title" : "123", "desc" : "123" } } ] }}

4.3 全文查询

# 使用 Match 查询,指定的匹配条件是desc字段里面包含"123"这个词GET 'http://localhost:9200/index1/_doc/_search'{ "query" : { "match" : { "desc" : "123" }}}# 结果{ "took":3, "timed_out":false, "_shards":{"total":5,"successful":5,"failed":0}, "hits":{ "total":1, "max_score":0.28582606, "hits":[ { "_index":"accounts", "_type":"person", "_id":"1", "_score":0.28582606, "_source": { "user" : "123", "title" : "123", "desc" : "123" } } ] }}

# Elastic 默认一次返回10条结果,可以通过size字段改变这个设置# 下面代码指定,每次只返回一条结果GET 'http://localhost:9200/index1/_doc/_search'{ "query" : { "match" : { "desc" : "123" }}, "size": 1}

# 还可以通过from字段,指定位移GET 'http://localhost:9200/index1/_doc/_search'{ "query" : { "match" : { "desc" : "管理" }}, "from": 1, "size": 1}

4.4 逻辑运算

# 如果有多个搜索关键字, Elastic 认为它们是or关系# 下面面代码搜索的是软件 or 系统GET 'http://localhost:9200/index1/_doc/_search'{ "query" : { "match" : { "desc" : "1 3" }}}

# 如果要执行多个关键词的and搜索,必须使用布尔查询GET 'http://localhost:9200/index1/_doc/_search'{ "query": { "bool": { "must": [ { "match": { "desc": "1" } }, { "match": { "desc": "3" } } ] } }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。