ESDocumentAPI

一、插入文档

  1. PUT /<target>/_doc/<_id>

    在插入文档的时候,就指定文档的Id。如果插入的文档id已经存在,则会更新文档,并且_version字段的值会加一

    @Test
    public void addDocumentTest() {
        String baseUrl = "http://192.168.1.51:9200";
        Person person = new Person();
        String id = UUID.randomUUID().toString();
        person.setId(id);
        person.setName("xiaoxiao");
        person.setAge(18);
        person.setSalary(1.23);
      	// http://192.168.1.51:9200/test-index/_doc/abcd
        String url = baseUrl + "/" + index + "/_doc/" + id;
        String insertResult = RestClient.put(url, JSON.toJSONString(person), headerMap);
        System.out.println(insertResult);
        String queryResult = EsUtilsWithAuth.queryAll(index);
        System.out.println(queryResult);
    }
    
  2. POST /<target>/_doc/

    在插入文档的时候,不指定文档Id,由ES生成文档的Id

    @Test
    public void addDocumentTest() {
        String baseUrl = "http://192.168.1.51:9200";
        Person person = new Person();
        String id = UUID.randomUUID().toString();
        person.setId(id);
        person.setName("xiaoxiao");
        person.setAge(18);
        person.setSalary(1.23);
      	// http://192.168.1.51:9200/test-index/_doc
        String url = baseUrl + "/" + index + "/_doc";
        String insertResult = RestClient.post(url, JSON.toJSONString(person), headerMap);
        System.out.println(insertResult);
        String queryResult = EsUtilsWithAuth.queryAll(index);
        System.out.println(queryResult);
    }
    
  3. PUT /<target>/_create/<_id>

    如果待插入的文档Id已经存在,则会出现version冲突

    @Test
    public void addDocumentTest() {
        String baseUrl = "http://192.168.1.51:9200";
        Person person = new Person();
        String id = UUID.randomUUID().toString();
        person.setId(id);
        person.setName("xiaoxiao");
        person.setAge(18);
        person.setSalary(1.23);
        // http://192.168.1.51:9200/test-index/_create/abcd
        String url = baseUrl + "/" + index + "/_create/" + id;
        String insertResult = RestClient.put(url, JSON.toJSONString(person), headerMap);
        System.out.println(insertResult);
        String queryResult = EsUtilsWithAuth.queryAll(index);
        System.out.println(queryResult);
    }
    
  4. POST /<target>/_create/<_id>

    如果待插入的文档Id已经存在,则会出现version冲突

    @Test
    public void addDocumentTest() {
        String baseUrl = "http://192.168.1.51:9200";
        Person person = new Person();
        String id = UUID.randomUUID().toString();
        person.setId(id);
        person.setName("xiaoxiao");
        person.setAge(18);
        person.setSalary(1.23);
        // http://192.168.1.51:9200/test-index/_create/abcd
        String url = baseUrl + "/" + index + "/_create/" + id;
        String insertResult = RestClient.post(url, JSON.toJSONString(person), headerMap);
        System.out.println(insertResult);
        String queryResult = EsUtilsWithAuth.queryAll(index);
        System.out.println(queryResult);
    }
    

二、简单查询文档

官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html

  1. GET <index>/_doc/<_id>

    根据id查询文档信息

    GET /test-index-02/_doc/1
    
    {
      "_index" : "test-index-02",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 25,
      "_primary_term" : 5,
      "found" : true,
      "_source" : {
        "id" : 3,
        "name" : "xiao3",
        "age" : 18
      }
    }
    
  2. HEAD <index>/_doc/<_id>

    判断文档是否存在

    HEAD /test-index-02/_doc/1
    
  3. GET <index>/_source/<_id>

    只查询具体数据

    GET /test-index-02/_source/1
    
    {
      "id" : 3,
      "name" : "xiao3",
      "age" : 18
    }
    
  4. HEAD <index>/_source/<_id>

    判断文档是否存在

    HEAD /test-index-02/_source/1
    
  5. _mget

    官方文档 https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html

    在多个索引下查询数据

    GET /_mget
    {
      "docs": [
        {
          "_index": "test-index-01",
          "_id": 1
        },
        {
          "_index": "test-index-02",
          "_id": 2
        }
      ]
    }
    

    在一个索引下查询数据

    GET /test-index-02/_mget
    {
      "docs": [
        {
          "_id": 1
        },
        {
          "_id": 2
        }
      ]
    }
    

三、修改文档

  1. 更新执行的字段

    POST /test-index-02/_update/560405c8-a4c9-4059-8424-e5ba05cff44f
    {
      "doc": {
        "name":"xiao1"
      }
    }
    
    @Test
    public void updateDocumentTest() {
        String id = "560405c8-a4c9-4059-8424-e5ba05cff44f";
        String url = baseUrl + "/" + index + "/_update/" + id;
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("name", "xiao2");
        Map<String, Object> docMap = new HashMap<>();
        docMap.put("doc", paramMap);
    
        String updateResult = RestClient.post(url, JSON.toJSONString(docMap), headerMap);
        System.out.println(updateResult);
    }
    
  2. upsert更新

    • 如果需要更新的文档存在:

      1. 如果doc中的字段存在,则更新该字段
      2. 如果doc中的字段不存在,但是开启了动态mapping,则新增该字段;
    • 如果文档不存在,则插入upsert中的字段

    POST /test-index-02/_update/1
    {
      "doc":{
        "address": "chengdu"
      },
      "upsert": {
        "sex":"woman"
      }
    }
    

    使用upsert更新数据

    update test-index-02._doc set address = 'beijing' where address = 'chengdu'
    
    POST /test-index-02/_update_by_query
    {
      "query": {
        "term": {
          "address":"chengdu" 
        }
      },
      "script": {
        "source": "ctx._source.address = 'beijing'",
        "lang": "painless"
      }
    }
    

四、删除文档

  1. 通过id删除文档

    DELETE /test-index-02/_doc/3
    
  2. delete_by_query

    POST /test-index-02/_delete_by_query
    {
      "query":{
        "term":{
          "address": "beijing"
        }
      }
    }
    

五、批量操作

POST _bulk
{"create": {"_index": "test", "_id":5}}
{"field5": "value5"}
{"update": {"_index": "test", "_id": 5}}
{"doc": {"field5": "value6"}}
{"index": {"_index": "test", "_id": 5}}
{"field5": "value7"}
{"delete": {"_index": "test", "_id": 5}}

六、使用sql查询ES数据

format可以选择json或者txt

POST /_sql?format=txt
{
  "query": """
  SELECT speaker, count(1) as speaker_count FROM "shakespeare" group by speaker order by speaker_count desc
  """
}
@Test
public void sqlTest() {
    String url = baseUrl + "/_sql?format=txt";
    Map<String, String> queryBody = new HashMap<>();
    queryBody.put("query", "SELECT speaker, count(1) as speaker_count FROM shakespeare group by speaker order by"
        + " speaker_count desc");
    String result = RestClient.post(url, JSON.toJSONString(queryBody), headerMap);
    System.out.println(result);
}

sql转换成DSL

POST /_sql/translate
{
  "query": """
  SELECT speaker, count(1) as speaker_count FROM "shakespeare" group by speaker order by speaker_count desc
  """
}
{
  "size" : 0,
  "_source" : false,
  "aggregations" : {
    "groupby" : {
      "composite" : {
        "size" : 1000,
        "sources" : [
          {
            "7cd992d7" : {
              "terms" : {
                "field" : "speaker.keyword",
                "missing_bucket" : true,
                "order" : "asc"
              }
            }
          }
        ]
      }
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值