ElasticSearch 增删改查

目录

RESTful接口URL的格式的增删改查

通过http形式发送请求对es进行操作

image-20220310145553887

RESTful接URL的格式:

http://192.168.10.16:9200/<index>/<type>/[<id>]

案例

添加

curl -XPUT 'http://118.178.242.230:9200/store/books/1' -d '{
	"title" : "Elasticseach:The Definitive Guide",
	"name" : {
		"first" : "Zachary",
		"last" : "Tong"
	},
	"publish_date" : "2015-02-06",
	"price" : "400.0"
}'

查询

curl -XGET "http://118.178.242.230:9200/store/books/1"

浏览器访问:http://118.178.242.230:9200/store/books/1

curl -XPUT 'http://118.178.242.230:9200/store/books/4' -d '{
	"title" : "Elasticseach2:The Definitive Guide",
	"name" : {
		"first" : "Guide
		"last" : "Guide
	},
	"publish_date" : "2015-02-07",
	"price" : "50.0"
}'

_source代表返回的字段

curl -XGET "http://118.178.242.230:9200/store/books/1?_source=title"

curl -XGET "http://118.178.242.230:9200/store/books/1?_source=title,name"

修改

覆盖方式

curl -XPUT 'http://118.178.242.230:9200/store/books/3' -d '{
	"title" : "Elasticseach2:The Definitive Guide",
	"name" : {
		"first" : "Zacharyaa",
		"last" : "Tongaa"
	},
	"publish_date" : "2015-02-07",
	"price" : "999.0"
}'

通过_update API的方式单独更新你想要更新的

curl -XPOST 'http://118.178.242.230:9200/store/books/2/_update' -d '{
	"doc" : {
		"price" : 8888.0
	}
}'

删除

curl -XDELETE 'http://118.178.242.230:9200/store/books/1'

复杂查询

过滤查询(filter)

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"must" : {
				"match_all" : {}
			},
			"filter" : {
				"term" : {
					"price" : "400.0"
				}
			}
		}
	}
}'

_search : 查询

query : 查询条件

bool : 组合查询

term : 部分词

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"constant_score" : {
			"filter" : {
				"term" : {
					"price" : "400.0"
				}
			}
		}
	}
}'

查询price = “400.0”

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"filter" : {
				"term" : {
					"price" : "400.0"
				}
			}
		}
	}
}'

查询price = “400.0” 或者 “50.0”

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"filter" : {
				"terms" : {
					"price" : ["400.0","50.0"]
				}
			}
		}
	}
}'

查询时间 = “2015-02-06”

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"filter" : {
				"term": {
				 	"publish_date" : "2015-02-06"
				}
			}
		}
	}
}'

查询select * from books where (price = 50.0 or prcie = 400.0) and publish_date != “2015-02-06”

must : 条件必须满足,相当于and

should : 条件可以满足也可以不满足,相当于or

must_not : 条件不需要满足 , 相当于 not

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"should" : [
				{"term" : {"price" : 50.0}},
				{"term" : {"price" : 400.0}}
			],
			"must_not" : {
				"term" : {
					"publish_date" : "2015-02-06"
				}
			}
		}
	}
}'

select * from books where price = 50.0 or (publish_date = “2015-02-06” and price = 400.0)

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"should" : [
			{	
				"term" : {
					"price" : 50.0
				}
			},
			{
				"bool" : {
					"must" : [
						{
							"term" : {
								"publish_date" : "2015-02-06"
							}
						},
						{
							"term": {
								"price": 400.0
								}
							}
						]
					}
				}
			]
		}
	}
}'

range 范围过滤

select * friom books where price >= 10 and price < 99

  • gt : > 大于

  • lt : < 小于

  • gte : >= 大于等于

  • lte : <= 小于等于

    curl -XGET ‘http://118.178.242.230:9200/store/books/_search’ -d ‘{
    “query” : {
    “range” : {
    “price” : {
    “gte” : 10,
    “lt” : 99
    }
    }
    }
    }’

注意:如果没有定义类型对比的是字符串

name和author都必须包含Guide,并且价钱等于50.0或者400.0

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
        "bool" : {
            "must" : {
                "multi_match" : {
                    "operator" : "and",
                    "fields" : [
                        "name",
                        "author"
                    ],
                    "query" : "Guide"
                }
            },
            "filter" : {
                "terms" : {
                    "price" : [
                        50.0,
                        400.0
                    ]
                }
            }
        }
    }
}'
Elasticsearch是一个基于Lucene的全文搜索引擎,支持各种复杂查询,并提供实时索引、搜索功能。以下是关于在Elasticsearch中执行基本操作“增删改查”的简要介绍: ### **增加数据(Insert)** 在 Elasticsearch 中添加文档通常涉及创建一个新的索引文件,将数据作为 JSON 格式的文档插入到相应的索引中。 ```bash POST /my_index/_doc { "name": "John Doe", "age": 30, "occupation": "Engineer" } ``` 这将在名为 `my_index` 的索引下创建一个新的文档。 ### **删除数据(Delete)** 从 Elasticsearch 删除数据相对简单,需要指定文档的ID以及它所在的索引名称。 ```bash DELETE /my_index/_doc/1 ``` 这里的 `/1` 表示要删除索引 `my_index` 下 ID 为 `1` 的文档。 ### **修改数据(Update)** 更新文档可以覆盖整个文档,也可以仅更新部分字段。为了更新现有文档并保持其原始状态,可以使用 `_source` 参数。 ```bash PUT /my_index/_update/1?pretty { "doc" : { "age": 45 } } ``` 或者更详细地更新特定字段: ```bash PUT /my_index/_update/1?pretty { "script" : "_source.age = doc.age + 15" } ``` 这里使用了脚本语言来计算新年龄值。 ### **查询数据(Query)** 查询功能允许通过复杂的条件搜索文档,可以使用 DSL(Domain Specific Language)来进行。例如,查找所有年龄大于30的工程师: ```bash GET /my_index/_search { "query": { "bool": { "must": [ { "match": { "occupation": "Engineer" } }, { "range": { "age": { "gt": 30 } } } ] } } } ``` 这个查询返回了满足条件的所有文档。 ### 相关问题: 1. **如何优化 Elasticsearch 查询性能**? 2. **Elasticsearch 如何处理大数据量的实时搜索需求**? 3. **如何监控 Elasticsearch 集群的状态及性能指标**? 这四个问题涵盖了从基础操作到高级管理和优化的主题,深入探讨了如何更好地利用 Elasticsearch 解决实际的搜索和分析场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值