Creating a text file with the described Elasticsearch operations.
es_operations = """
Elasticsearch 基本操作语法:
- 索引文档 (Index Documents):
自动生成 ID:
POST /index_name/_doc
{
"field1": "value1",
"field2": "value2"
}
指定 ID:
PUT /index_name/_doc/1
{
"field1": "value1",
"field2": "value2"
}
- 获取文档 (Get Documents):
按 ID 获取:
GET /index_name/_doc/1
- 更新文档 (Update Documents):
部分更新文档:
POST /index_name/_update/1
{
"doc": {
"field2": "new_value"
}
}
- 删除文档 (Delete Documents):
按 ID 删除:
DELETE /index_name/_doc/1
- 查询文档 (Search Documents):
Match 查询:
GET /index_name/_search
{
"query": {
"match": {
"field1": "value"
}
}
}
Term 查询:
GET /index_name/_search
{
"query": {
"term": {
"field1": "exact_value"
}
}
}
Bool 查询:
GET /index_name/_search
{
"query": {
"bool": {
"must": [
{ "match": { "field1": "value" } },
{ "term": { "field2": "exact_value" } }
],
"must_not": [
{ "term": { "field3": "value_to_exclude" } }
],
"should": [
{ "match": { "field4": "optional_value" } }
]
}
}
}
{
"multi_match": {
"query": "search text",
"fields": ["field1", "field2", "field3"]
}
}
- 范围查询 (Range Queries):
GET /index_name/_search
{
"query": {
"range": {
"timestamp": {
"gte": "2024-01-01",
"lte": "2024-12-31"
}
}
}
}
- 聚合查询 (Aggregations):
按字段分组 (Terms Aggregation):
GET /index_name/_search
{
"size": 0,
"aggs": {
"group_by_field1": {
"terms": {
"field": "field1.keyword"
}
}
}
}
统计 (Stats Aggregation):
GET /index_name/_search
{
"size": 0,
"aggs": {
"field1_stats": {
"stats": {
"field": "field1"
}
}
}
}
- 排序 (Sort):
GET /index_name/_search
{
"query": {
"match_all": {}
},
"sort": [
{ "field1": { "order": "asc" } },
{ "field2": { "order": "desc" } }
]
}
- 分页 (Pagination):
GET /index_name/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10
}
- 批量操作 (Bulk Operations):
POST /_bulk
{ "index": { "_index": "index_name", "_id": "1" } }
{ "field1": "value1" }
{ "delete": { "_index": "index_name", "_id": "2" } }
{ "update": { "_index": "index_name", "_id": "3" } }
{ "doc": { "field2": "new_value" } }
- 滚动查询 (Scroll Query):
POST /index_name/_search?scroll=1m
{
"size": 100,
"query": {
"match_all": {}
}
}
使用 _scroll_id 获取后续批次数据:
POST /_search/scroll
{
"scroll": "1m",
"scroll_id":"DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAABbZjaFl1N3dLcmJNTE1GZ1BQOGZqZ3cAAAAAAA"
}
- 删除索引 (Delete Index):
DELETE /index_name