Elasticsearch 提供了一套强大的 RESTful API,允许你通过 HTTP 请求来进行各种操作,如索引(增加)、删除、更新文档,以及执行复杂的查询。以下是详细的命令介绍,包括增删改查、多条件查询、分页、聚合等。
戳底部名片,一起变现
1. 索引(Index)操作 - 增加文档
创建或更新单个文档
使用 PUT
方法向指定的索引中添加或更新一个文档:
curl -X PUT "localhost:9200/index_name/_doc/doc_id" -H 'Content-Type: application/json' -d'
{
"field1": "value1",
"field2": "value2"
}'
如果 doc_id
已存在,则会更新该文档;如果不存在,则会创建新文档。
批量索引
使用 _bulk
API 可以一次处理多个文档:
curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/x-ndjson' -d'
{ "index" : { "_index" : "index_name", "_id" : "1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "index_name", "_id" : "2" } }
{ "field2" : "value2" }
'
2. 删除操作
删除单个文档
使用 DELETE
方法根据 ID 删除指定的文档:
curl -X DELETE "localhost:9200/index_name/_doc/doc_id"
删除整个索引
删除一个完整的索引及其所有数据:
curl -X DELETE "localhost:9200/index_name"
3. 更新操作
部分更新文档
使用 _update
API 来对现有文档的部分字段进行更新:
curl -X POST "localhost:9200/index_name/_update/doc_id" -H 'Content-Type: application/json' -d'
{
"doc": {
"field1": "new_value1"
}
}'
全量更新文档
如果你想替换整个文档,可以使用 POST
方法并提供完整的文档内容:
curl -X POST "localhost:9200/index_name/_doc/doc_id" -H 'Content-Type: application/json' -d'
{
"field1": "new_value1",
"field2": "new_value2"
}'
4. 查询操作
简单匹配查询
使用 GET
方法结合 _search
API 进行简单的全文搜索:
curl -X GET "localhost:9200/index_name/_search?q=field:value"
结构化查询
构建更复杂的查询语句,例如布尔查询:
curl -X GET "localhost:9200/index_name/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "field1": "value1" } },
{ "match": { "field2": "value2" } }
]
}
}
}'
多条件查询
组合多个查询条件,比如使用 should
或 must_not
子句:
curl -X GET "localhost:9200/index_name/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "field1": "value1" } },
{ "match": { "field2": "value2" } }
],
"must_not": [
{ "term": { "status": "deleted" } }
]
}
}
}'
5. 分页查询
从头开始分页
使用 from
和 size
参数来控制结果集的起始位置和返回的数量:
curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
"from": 0,
"size": 10,
"query": {
"match_all": {}
}
}'
深度分页
对于深层分页,建议使用 scroll
API 或者 search_after
参数以提高性能:
# 使用 scroll API 获取大量数据
curl -X GET "localhost:9200/index_name/_search?scroll=1m&size=100" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'
# 使用 search_after 参数
curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 10,
"sort": [
{ "timestamp": { "order": "desc" } }
],
"search_after": [1577836800000] # 上一页最后一个文档的时间戳
}'
6. 聚合操作
基本聚合
执行简单的聚合查询,如统计某个字段的不同值数量:
curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"distinct_values": {
"terms": {
"field": "field_name.keyword"
}
}
}
}'
管道聚合
连接多个聚合步骤,例如计算平均值后再求总和:
curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"average_prices": {
"avg": {
"field": "price"
}
},
"total_sales": {
"sum_bucket": {
"buckets_path": "average_prices"
}
}
}
}'
日期直方图聚合
按时间间隔(如每天、每周)汇总数据:
curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "sale_date",
"calendar_interval": "day"
}
}
}
}'
7. 其他有用的操作
获取特定文档
直接通过 ID 获取单个文档:
curl -X GET "localhost:9200/index_name/_doc/doc_id"
检查文档是否存在
使用 HEAD
方法检查文档是否存在于索引中:
curl -X HEAD "localhost:9200/index_name/_doc/doc_id"
刷新索引
强制刷新索引以便立即可见最新更改:
curl -X POST "localhost:9200/index_name/_refresh"
优化合并段落
减少索引文件的数量,有助于提高查询性能:
curl -X POST "localhost:9200/index_name/_forcemerge?max_num_segments=1"