创建一个工具类ElasticsearchConfig.java
package com.example.demo.baseConfig.elasticsearch;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticsearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return client;
}
}
使用MainController .java进行主要功能测试
package com.example.demo.controller;
import com.alibaba.fastjson.JSON;
import com.example.demo.baseConfig.elasticsearch.ElasticsearchConfig;
import com.example.demo.bean.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainController {
static ElasticsearchConfig elasticsearchConfig=new ElasticsearchConfig();
static RestHighLevelClient restHighLevelClient=elasticsearchConfig.restHighLevelClient(); //获取连接地址
public static void main(String[] args) throws IOException {
testSearch();
}
/**
* 判断索引是否存在
* @throws IOException
*/
static void testExisIndex() throws IOException{
GetIndexRequest getIndexRequest=new GetIndexRequest("xiaozhan_index");
boolean exists=
restHighLevelClient.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
System.out.println("判断索引是否存在:"+exists);
}
/**
* 创建索引
* @throws IOException
*/
static void createIndex() throws IOException {
//1、创建索引请求
CreateIndexRequest createIndexRequest=new CreateIndexRequest("xiaozhan_index");
//2、客户端执行请求
CreateIndexResponse createIndexResponse=
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
}
/**
* 测试判断是否删除成功
* @throws IOException
*/
static void deleteIndex() throws IOException {
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("xiaozhan_index");
AcknowledgedResponse exists=restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
System.out.println(exists);
}
/**
* 添加文档
* @throws IOException
*/
static void testAddDocument() throws IOException {
User user=new User("占新清","25","男","1105936733","湖北省黄冈市蕲春县狮子镇","华为荣耀青春版");
IndexRequest indexRequest=new IndexRequest("xiaozhan_index");
//规则 put /xiaozhan_index/_doc/1
indexRequest.id("2");
indexRequest.timeout(TimeValue.timeValueSeconds(1));
indexRequest.timeout("1s");
//将我们的数据放入请求,json
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse indexResponse=restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
System.out.println("indexResponse:"+indexResponse.toString());
System.out.println("indexResponse的statue:"+indexResponse.status()); //返回我们命令对应的状态
}
/**
* 获取文档,判断是否存在get /index/doc/1
* @throws IOException
*/
static void testIsExist() throws IOException {
GetRequest getRequest=new GetRequest("xiaozhan_index","1");
boolean exist=restHighLevelClient.exists(getRequest,RequestOptions.DEFAULT);
//不获取返回的 _source的上下文了
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists=restHighLevelClient.exists(getRequest,RequestOptions.DEFAULT);
System.out.println("获取exists状态"+exist);
}
/**
* 获取文档的信息
* @throws IOException
*/
static void testGetDocument() throws IOException {
GetRequest getRequest=new GetRequest("xiaozhan_index","1");
GetResponse getResponse=restHighLevelClient.get(getRequest,RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString()); //打印文档的内容
}
/**
* 修改文档
* @throws IOException
*/
static void testUpdateDocument() throws IOException {
UpdateRequest updateRequest=new UpdateRequest("xiaozhan_index","1");
updateRequest.timeout("1s");
User user=new User("刘松","12","男","1105936733","湖北省民政厅","oppo A11");
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse updateResponse=restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
System.out.println("updateResponse的status"+updateResponse.status());
}
/**
* 删除文档记录
* @throws IOException
*/
static void testDeleteRequest() throws IOException {
DeleteRequest deleteIndexRequest=new DeleteRequest("xiaozhan_index","1");
deleteIndexRequest.timeout("1s");
DeleteResponse deleteResponse=restHighLevelClient.delete(deleteIndexRequest,RequestOptions.DEFAULT);
System.out.println("DeleteResponse的状态"+deleteResponse.status());
}
/**
* 批量处理请求
* @throws IOException
*/
static void testBulkRequest() throws IOException {
BulkRequest bulkRequest=new BulkRequest();
bulkRequest.timeout("10s");
List<User>list=new ArrayList<User>();
User user1=new User("占新清","25","男","1105936733","湖北省黄冈市蕲春县狮子镇","华为荣耀青春版");
User user2=new User("刘松","26","男","1105936733","湖北省黄冈市蕲春县狮子镇","oppo A11");
User user3=new User("尼克松","28","男","468403390","湖北省黄冈市蕲春县狮子镇","酷睿");
list.add(user1);
list.add(user2);
list.add(user3);
for (int i=0;i<list.size();i++){
bulkRequest.add(
new IndexRequest("xiaozhan_index")
.id(""+(i+1))
.source(JSON.toJSONString(list.get(i)),XContentType.JSON));
}
BulkResponse bulkItemResponses=restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
System.out.println("bulkItemResponses的状态"+bulkItemResponses.hasFailures());
}
/**
* 查询
* SearchRequest 搜索请求
* SearchSourceBuilder 条件构建
* highlighter 构建高亮
* TermQueryBuilder 精确查找
* Match
* @throws IOException
*/
static void testSearch() throws IOException {
SearchRequest searchRequest=new SearchRequest("xiaozhan_index");
//构建搜索条件
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
searchSourceBuilder.highlighter();
//查询条件,我们可以使用queryBuilders工具来实现
//QueryBuilders.termQuery精确
//QueryBuilders.matchAllQuery匹配所有
TermQueryBuilder termQueryBuilder= QueryBuilders.termQuery("name","刘松");
// TermQueryBuilder termQueryBuilder= QueryBuilders.matchAllQuery("name","刘松");
searchSourceBuilder.query(termQueryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits())); //获取里面的数据
for (SearchHit documentFields : searchResponse.getHits().getHits()){
System.out.println(documentFields.getSourceAsMap());
}
}
}