ES操作指南

# Creating a text file with the described Elasticsearch operations.

es_operations = """

Elasticsearch 基本操作语法:

1. 索引文档 (Index Documents):

   自动生成 ID:

   POST /index_name/_doc

   {

     "field1": "value1",

     "field2": "value2"

   }

   指定 ID:

   PUT /index_name/_doc/1

   {

     "field1": "value1",

     "field2": "value2"

   }

2. 获取文档 (Get Documents):

   按 ID 获取:

   GET /index_name/_doc/1

3. 更新文档 (Update Documents):

   部分更新文档:

   POST /index_name/_update/1

   {

     "doc": {

       "field2": "new_value"

     }

   }

4. 删除文档 (Delete Documents):

   按 ID 删除:

   DELETE /index_name/_doc/1

5. 查询文档 (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"]

  }

}

6. 范围查询 (Range Queries):

   GET /index_name/_search

   {

     "query": {

       "range": {

         "timestamp": {

           "gte": "2024-01-01",

           "lte": "2024-12-31"

         }

       }

     }

   }

7. 聚合查询 (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"

         }

       }

     }

   }

8. 排序 (Sort):

   GET /index_name/_search

   {

     "query": {

       "match_all": {}

     },

     "sort": [

       { "field1": { "order": "asc" } },

       { "field2": { "order": "desc" } }

     ]

   }

9. 分页 (Pagination):

  GET /index_name/_search

   {

     "query": {

       "match_all": {}

     },

     "from": 0,

     "size": 10

   }

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" } }

11. 滚动查询 (Scroll Query):

POST /index_name/_search?scroll=1m

    {

      "size": 100,

      "query": {

        "match_all": {}

      }

    }

    使用 _scroll_id 获取后续批次数据:

    POST /_search/scroll

    {

      "scroll": "1m",

      "scroll_id":"DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAABbZjaFl1N3dLcmJNTE1GZ1BQOGZqZ3cAAAAAAA"

    }

12. 删除索引 (Delete Index):

    DELETE /index_name

上一篇:Python Django ORM 的工作原理


下一篇:关于上传到GPU:这个地方之前一直没有弄清楚