当前位置:   article > 正文

java 连接 Elasticsearch 使用 RestHighLevelClient 进行插入、删除、修改、查询数据_resthighlevelclient delete

resthighlevelclient delete

依赖

<!-- elasticsearch-rest-client -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.13.3</version>
</dependency>
<!-- elasticsearch-rest-high-level-client -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.13.3</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

配置

@Configuration
public class ESConfig {

    @Bean
    public RestHighLevelClient getRestHighLevelClient(){
        HttpHost http1 = new HttpHost("127.0.0.1", 9200, "http");
        HttpHost http2 = new HttpHost("127.0.0.1", 9201, "http");
        RestClientBuilder restClientBuilder = RestClient.builder(http1, http2);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
        return restHighLevelClient;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
@Autowired
private RestHighLevelClient restHighLevelClient;
  • 1
  • 2

插入或更新数据

public void insertOrUpdateData() throws IOException {
    HashMap<String, String> map = new HashMap<>();
    map.put("name", "dd df ff");
map.put("age", "16");

IndexRequest indexRequest = new IndexRequest("myindex");
//设置ID
indexRequest.id("9");
//添加数据
indexRequest.source(map);

    restHighLevelClient.indexAsync(indexRequest,
            RequestOptions.DEFAULT,
            new ActionListener<IndexResponse>() {
                @Override
                public void onResponse(IndexResponse indexResponse) {
                    RestStatus status = indexResponse.status();
                    System.out.println(status.name());
                    if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
                        System.out.println("创建成功");
                    } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
      ## 标题                  System.out.println("更新成功");
                    }
                }
                @Override
                public void onFailure(Exception e) {
                    System.out.println("出异常了");
                }
            });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

删除数据

public void deleteData() throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest("myindex");
    deleteRequest.id("1");
deleteRequest.timeout(TimeValue.timeValueMinutes(2));

    DeleteResponse deleteResponse = restHighLevelClient.delete(
            deleteRequest, RequestOptions.DEFAULT);
    if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
        System.out.println("未找到需要删除的数据");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

更新数据

public void updateData() throws IOException {
    Map<String, Object> parameters = new HashMap<>();
parameters.put("count", 4);

    Script script = new Script(ScriptType.INLINE, "painless",
            "ctx._source.field += params.count", parameters);

    UpdateRequest updateRequest = new UpdateRequest("myindex", "2");
    updateRequest.script(script);
    UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
    DocWriteResponse.Result result = updateResponse.getResult();
    if (result == DocWriteResponse.Result.CREATED) {
        System.out.println("created");
    } else if (result == DocWriteResponse.Result.UPDATED) {
        System.out.println("updated");
    } else if (result == DocWriteResponse.Result.DELETED) {
        System.out.println("deleted");
    } else if (result == DocWriteResponse.Result.NOOP) {
        System.out.println("noop");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

单条查询数据

public void getOneData() throws IOException {
    String[] includes = Strings.EMPTY_ARRAY;
    String[] excludes = Strings.EMPTY_ARRAY;
    FetchSourceContext fetchSourceContext =
            new FetchSourceContext(true, includes, excludes);

    GetRequest getRequest = new GetRequest("myindex");
    getRequest.id("9");
getRequest.fetchSourceContext(fetchSourceContext);

    boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
    if (exists) {
        System.out.println("有数据");
    }
    GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
    if (response.isExists()) {
        Map<String, Object> sourceAsMap = response.getSourceAsMap();
        //获取索引名称
        String index = response.getIndex();
        //获取索引类型
        String type = response.getType();
        //获取索引id
        String id = response.getId();
        System.out.println("index:" + index);
        System.out.println("type:" + type);
        System.out.println("id:" + id);
        System.out.println(sourceAsMap);
    } else {
        System.out.println("没有数据");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

多条查询

public void multiQuery() throws IOException {
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    multiGetRequest.add("myindex", "3");
    multiGetRequest.add("myindex", "5");
    MultiGetResponse multiGetResponse = restHighLevelClient
            .mget(multiGetRequest, RequestOptions.DEFAULT);
    MultiGetItemResponse[] responses = multiGetResponse.getResponses();
    Stream.of(responses).forEach(response -> {
        String id = response.getResponse().getId();
        String source = response.getResponse().getSourceAsString();
        System.out.println("id = " + id + " --> " + source);
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/68571
推荐阅读
相关标签
  

闽ICP备14008679号