elasticsearch7.2的增删改查语法

本篇内容主要讲解“elasticsearch7.2的增删改查语法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“elasticsearch7.2的增删改查语法”吧!

创新互联建站-专业网站定制、快速模板网站建设、高性价比容城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式容城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖容城地区。费用合理售后完善,10年实体公司更值得信赖。

首先在pom.xml里面导入es相关依赖:
  
 
      org.elasticsearch
      elasticsearch
      7.2.0
    





    
      org.elasticsearch.client
      elasticsearch-rest-high-level-client
      7.2.0
    
    
      org.elasticsearch.client
      elasticsearch-rest-client
      7.2.0
    
    

代码如下:

import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpHost; //import org.elasticsearch.action.get.GetRequest; //import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; //import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.*;

public class Demo1 {     private static RestHighLevelClient client;     private static final String ip="170.60.140.120";     private static final int port=9200;

public static void main(String[] args) throws Exception{
    client=new RestHighLevelClient(RestClient.builder(
            new HttpHost(ip, port, "http")));
            
//      indexincrement();
        indexsearch();
//      indexdelete();
//      indexupdate();

        client.close();
    }

创建索引

 private static void indexincrement() throws Exception{
        HashMap jsonmap = new HashMap<>();
        jsonmap.put("name","王五");
        jsonmap.put("age",32);
        IndexRequest indexRequest = new IndexRequest("post").source(jsonmap);
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println("indexresponse = "+indexResponse);
        System.out.println("indexResponse.status() = "+indexResponse.status());

}

删除索引

 private static void indexdelete() throws Exception{
         //第一种,按索引id删除某个文档。
/*      DeleteRequest deleteRequest = new DeleteRequest("post", "m56EDXIBK5FuWnazQHHa");
        client.delete(deleteRequest,RequestOptions.DEFAULT);*/

       //第二种,一次性删除整个索引
/*       DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("post");
         client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);  */

        //第三种,先按条件查询出某范围内所有Id,再一个一个删除
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(boolQueryBuilder);
        sourceBuilder.size(100);
        SearchRequest searchRequest = new SearchRequest().indices("post");
        searchRequest.source(sourceBuilder);
        SearchResponse response1 = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = response1.getHits().getHits();
        for(SearchHit hit:hits){
            String id = hit.getId();
            DeleteRequest deleteRequest = new DeleteRequest("post", id);
            client.delete(deleteRequest,RequestOptions.DEFAULT);
        }

更新文档,(以map方式为例)

private static void indexupdate() throws Exception{
    UpdateRequest updateRequest = new UpdateRequest("post","kCfJHHIBCUDx2GUCB0Dk");
    HashMap jsonmap = new HashMap<>();
    jsonmap.put("name","赵六");
    jsonmap.put("age",16);
    updateRequest.doc(jsonmap);
    client.update(updateRequest,RequestOptions.DEFAULT);
}

查询索引

  private static void indexsearch(Connection conn) throws Exception{
/*      第一种,按某个id号查询方式
        GetRequest getRequest = new GetRequest("risklog", "rpBmNnIB76i_tBrAwvlC");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);*/
        
/*
    第二种,按条件查询整个索引表方式
*/
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(boolQueryBuilder);
 //这里设置查询7万条数据
    sourceBuilder.size(70000);

//        SearchRequest searchRequest=new SearchRequest();
        SearchRequest searchRequest = new SearchRequest().indices("middleware_apache_server_access");
        searchRequest.source(sourceBuilder);
        SearchResponse response1 = client.search(searchRequest, RequestOptions.DEFAULT);
        List list = new ArrayList<>();
        response1.getHits().forEach(item ->            list.add(JSON.parseObject(item.getSourceAsString())));
        System.out.println("list.size() = "+list.size());
//        Iterator iterator = list.iterator();
        for(int i=0;i

}

到此,相信大家对“elasticsearch7.2的增删改查语法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


本文标题:elasticsearch7.2的增删改查语法
文章路径:http://azwzsj.com/article/gghcpg.html