Java 配置 Elasticsearch RestHighLevelClient (基于 X-Pack 认证)

TODO

Java 配置 Elasticsearch RestHighLevelClient (带认证)

application.yml

spring:
 elasticsearch:
   rest:
     uris:
       - http://localhost:9200
     username: elastic
     password: password

pom.xml


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

RestHighLevelClient 基本使用

/**
 * The type Elasticsearch test.
 *
 * @author WYG
 */
@Slf4j
public class ElasticsearchTest extends Tester {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    /**
     * Test.
     */
    @Test
    public void testEsRestClient() {

        SearchRequest searchRequest = new SearchRequest("megacorp");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchPhraseQuery("about", "build cabinets"));
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);
        try {
            SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            Arrays.stream(response.getHits().getHits())
                    .forEach(i -> {
                        log.info(i.getIndex());
                        log.info(i.getSourceAsString());
                        log.info(i.getType());
                    });
            System.out.println(response.getHits().totalHits);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java 配置 Elasticsearch RestHighLevelClient (基于 X-Pack 认证))