ElasticSearch 文档的增删改查

简介

Elasticsearch 是一个分布式的免费开源搜索和分析引擎。适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据。

ES核心概念

关系型数据库与ES对比


f1d69e27dd534fd1bbf8b93a094fcd59.png

新增文档

指定文档 id 创建文档

20c49ab261db4653aa8aaae7f8602840.png
PUT /pt_emp_effect_info/_doc/888666
{
“emp_code”: “42014861”,
“emp_name”: “张三”
}

入库结果


fbdd5d9c76da4de5aa83d2b14f35169a.png

自动生成id

POST /pt_emp_effect_info/_doc
{
“emp_code”: “42014889”,
“emp_name”: “李四”
}

入库结果


813d27ca9347450a87c999b86af727a7.png

修改文档

指定id

POST /pt_emp_effect_info/_doc/42014889?refresh=true
{
“emp_code”: “42014889”,
“emp_name”: “李四”,
“emp_desc”: “描述”
}

删除文档

根据指定id删除

DELETE /pt_emp_effect_info/_doc/888666

根据查询条件删除

POST /pt_emp_effect_info/_delete_by_query?refresh=true
{
“query”: {
“term” : {
“emp_code” : “42014889”
}
}
}

查询文档

查询所有数据

GET /pt_emp_effect_info/_search

根据指定id查询

GET /pt_emp_effect_info/_doc/42014889

词条匹配(term)

term 查询被用于精确值 匹配

POST /pt_emp_effect_info/_search
{
“query”: {
“**term**” : {
“emp_code” : “42014889”
}
}
}

多词条精确匹配(terms)

POST /pt_emp_effect_info/_search
{
“query”: {
“**terms**” : {
“emp_code” : [“42014889”, “42011002”]
}
}
}

范围查询 (gte lte gt lt)

POST /pt_emp_effect_info/_search
{
“query”: {
“**range**”: {
“create_time”: {
“gt”: “2023-07-18 00:00:00”
}
}
}
}

通配符

POST /pt_emp_effect_info/_search
{
“query”:{
“**wildcard**”:{
“emp_name”:“*张*”
}
}
}

指定输出结果字段

POST /pt_emp_effect_info/_search
{
** “_source”: [“emp_code”],**
“query”: {
“term”: {
“emp_code”: “42014889”
}
}
}

组合查询bool(must、must_not、should、filter)

{
“_source”: [“waybill_no”, “deliver_tm”, “deliver_dept_code”],
“query”: {
“bool”: {
“must”: [
{
“exists”: {
“field”: “signed_back_is_pick”
}
}
],
“filter”: [
{
“range”: {
“deliver_tm”: {
“lte”: “2023-07-18 10:00:00”
}
}
},
{
“term”: {
“signed_back_is_need”: true
}
}
]
}
}
}

{
“bool”: {
“should”: [
{
“terms”: {
“a”: [
“a”
],
“boost”: 1
}
},
{
“terms”: {
“b”: [
“b”
],
“boost”: 1
}
}
],
“adjust_pure_negative”: true,
“minimum_should_match”: “1”,
“boost”: 1
}
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容