Java 操作ElasticSearch
1、连接ElasticSearch服务器
@Configuration
public class ElasticSearchClientConfig {
private static final String CLUSTER_NAME = "*******************************";
private static final String APP_KEY = "******************************";
private static final String ACCESS_KEY = "********************************";
@Bean
public RestHighLevelClient restHighLevelClient() {
return PorosHighLevelClientBuilder.builder()
.clusterName(CLUSTER_NAME)
.appKey(APP_KEY)
.accessKey(ACCESS_KEY)
.build();
}
}
2、准备工作
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String username;
private Integer age;
}
3、增删改查(index)
3.1 注入RestHighLevelClient
@Autowired
RestHighLevelClient restHighLevelClient;
3.2 创建index
@Test
public void testCreateIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("lcc_index");
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
log.info(createIndexResponse.toString());
}
3.3 判断是否存在index
@Test
public void testGetIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("lcc_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
log.info(exists + "");
}
3.4 删除index
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("lcc_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
log.info(delete.toString());
}
4、增删改查document
4.1创建document
@Test
public void testAddDocument() throws IOException {
User user = new User("lcc", 3);
IndexRequest request = new IndexRequest("lcc_index");
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
Gson gson = new Gson();
request.source(gson.toJson(user), XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
log.info(indexResponse.toString());
}
4.2判断document是否存在
@Test
public void testExistDocument() throws IOException {
GetRequest request = new GetRequest("lcc_index", "1");
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
log.info(exists + "");
}
4.3获得document
@Test
public void testGetDocument() throws IOException {
GetRequest request = new GetRequest("lcc_index", "1");
GetResponse documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT);
log.info(documentFields.getSource().toString());
}
4.4更新document
@Test
public void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("lcc_index", "1");
User user = new User();
user.setAge(5);
Gson gson = new Gson();
request.doc(gson.toJson(user), XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT);
log.info(update.toString());
}
4.5删除document
@Test
public void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("lcc_index", "1");
DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
log.info(delete.status() + "");
}
5、批量操作(批量发送请求)
@Test
public void testBulkRequest() throws IOException {
BulkRequest request = new BulkRequest("lcc_index");
request.timeout("10s");
List<User> list = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
list.add(new User("lcc" + i, i));
}
Gson gson = new Gson();
for (int i = 0; i < 10; i++) {
request.add(new IndexRequest()
.id((i+1) + "")
.source(gson.toJson(list.get(i)), XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
log.info(bulk.hasFailures() + "");
}
6、条件查询(高级)
@Test
public void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest("lcc_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "lcc1");
searchSourceBuilder.query(termQueryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
log.info(search.getHits() + "");
for (SearchHit hit : search.getHits().getHits()) {
log.info(hit.getSourceAsMap() + "");
}
}