依赖
<!-- 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>
配置
@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;
}
}
@Autowired
private RestHighLevelClient restHighLevelClient;
模糊查询
public void fuzzy() throws IOException {
SearchRequest searchRequest = new SearchRequest("myindex");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders
.fuzzyQuery("name", "bbc")
.fuzziness(Fuzziness.AUTO));
searchRequest.source(searchSourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
Stream.of(hits).forEach(System.out::println);
}
|