5.ElasticSearch系列之文档的基本操作
阅读原文时间:2023年07月10日阅读:1
1. 文档写入
# create document. 自动生成 _id
POST users/_doc
{
  "user" : "shenjian",
  "post_date" : "2022-08-07T14:12:12",
  "message" : "trying out Kibana"
}
# 创建指定ID文档,存在则替换
POST users/_doc/1
{
    "user" : "Domi",
    "post_date" : "2022-08-07T14:12:12",
    "message" : "trying out Elasticsearch"
}
2. 文档查询
# 查询id为1的文档
GET users/_doc/1
# 查询字段order_id值为584677的文档
GET kibana_sample_data_ecommerce/_search?q=order_id:584677
# 查询所有文档
GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match_all": {}
  }
}
# _source指定返回字段 sort排序 form size分页 term查询指定字段
POST kibana_sample_data_ecommerce/_search
{
  "_source": ["currency", "order_date"],
  "sort": [
    {
      "order_date": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "currency": {
              "value": "EUR"
            }
          }
        }
      ]
    }
  }
}
# 嵌套对象查询
GET community/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "address_component",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "address_component.city": "佛山"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
3. 文档更新
# 在原文档上增加字段
POST users/_update/1/
{
    "doc":{
        "post_date" : "2019-05-15T14:12:12",
        "message" : "trying out Elasticsearch"
    }
}
# 批量查询后更新
POST kibana_sample_data_ecommerce/_update_by_query
{
  "script": "ctx._source.customer_first_name='domi'",
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "customer_first_name.keyword": {
              "value": "Eddie"
            }
          }
        }
      ]
    }
  }
}
4. 文档删除
# 删除文档
DELETE users/_doc/1
5. 批量操作[不常用,了解,后续通过python或java客户端批量操作]
# 批量操作
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
# 批量查询
GET /_mget
{
    "docs" : [
        {"_index" : "test","_id" : "1"},
        {"_index" : "test","_id" : "2"}
    ]
}

# URI中指定index
GET /test/_mget
{
    "docs" : [
        {"_id" : "1"},{"_id" : "2"}
    ]
}
# 查询指定索引指定字段
GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_id" : "1",
            "_source" : false
        },
        {
            "_index" : "test",
            "_id" : "2",
            "_source" : ["field3", "field4"]
        },
        {
            "_index" : "test",
            "_id" : "3",
            "_source" : {
                "include": ["user"],
                "exclude": ["user.location"]
            }
        }
    ]
}
### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}


# 删除索引,清除数据
DELETE users
DELETE test
DELETE test2

欢迎关注公众号算法小生沈健的技术博客