使用elasticsearch的Java High Level REST Client

在elasticsearch 7版本中原先的常用的TransportClient 已经不推荐使用,并且会在8版本中彻底移除掉,这里使用了的是Java High Level REST Client

环境

elasticsearch 7.9

java 1.8

准备测试数据

# 查询数据
post : localhost:9200/index_blog1/index_blog1/docid_00021
{"name":"abc1","age":"121" ,"content":"this is a doc  abc1"}

# 查询测试
http://localhost:9200/index_blog1/index_blog1/_search?pretty

maven 依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.9.3</version>
</dependency>



<repositories>
    <!-- add the elasticsearch repo -->
    <repository>
        <id>elasticsearch-releases</id>
        <url>https://artifacts.elastic.co/maven</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

查询代码


import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


public class TermQueryTest {


    /**
     * post : localhost:9200/index_blog1/index_blog1/docid_00121
     * {"name":"abc1","age":31 ,"content":"this is a doc  abc1"}
     *
     * post : localhost:9200/index_blog1/index_blog1/docid_0001
     * {"name":"abc","age":22 ,"content":"this is a doc  abc"}
     *
     */


    private RestHighLevelClient client ;

    @Before
    public void setUp() throws Exception {
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));

    }

    @After
    public void tearDown() throws Exception {
        client.close();
    }

    /**
     * name = "abc1"
     * @throws IOException
     */
    @Test
    public void queryTerm() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery("name", "abc1"));

        sourceBuilder.from(0);
        sourceBuilder.size(5);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        searchRequest.indices("index_blog1");
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit.getSourceAsString());
        }


    }

}




报错

执行查询代码,发现如下错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/common/xcontent/DeprecationHandler
    at org.elasticsearch.client.RestHighLevelClient.<clinit>(RestHighLevelClient.java:1906)
    at org.example.elasticsearch.EsQueryDemo.queryEs(EsQueryDemo.java:29)
    at org.example.elasticsearch.EsQueryDemo.main(EsQueryDemo.java:52)
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.xcontent.DeprecationHandler
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

经过排查为依赖中的包冲突,修改maven依赖如下 ,排除elasticsearch-rest-high-level-client 中的elasticsearch包,另外再引入对应的elasticsearch

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.9.3</version>
    <exclusions>
        <exclusion>
            <artifactId>elasticsearch</artifactId>
            <groupId>org.elasticsearch</groupId>
        </exclusion>
    </exclusions>
</dependency>


<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.9.3</version>
</dependency>

再次执行查询,结果如下,响应正常

14:28:22.824 [main] DEBUG org.elasticsearch.client.RestClient - request [POST http://localhost:9200/index_blog1/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true] returned [HTTP/1.1 200 OK]
{"name":"abc1","age":"121","content":"this is a doc  abc1"}

其他基本查询

  • 大于
  • 小于
  • 范围
  • 多条件查询
  • 分页

样例代码见github

参考链接

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.9/java-rest-high.html

posted @ 2020-11-08 19:30  海拉鲁小霸王  阅读(952)  评论(0编辑  收藏  举报