1.Elasticsearch和elasticsearch-headr跨域问题( 二 )


测试所需要的方法,例如 crud,批处理等
@SpringBootTestclass YueEsApiApplicationTests {@Autowired@Qualifier("restHighLevelClient")private RestHighLevelClient client;@Testvoid contextLoads() {}//测试索引的创建 Request PUT kuang_index@Testvoid testCreateIndex() throws IOException {//1. 创建索引请求CreateIndexRequest request = new CreateIndexRequest("yue_index");//2. 客户端执行创建请求 IndicesClient 请求后获得响应CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);System.out.println(createIndexResponse);}//测试获取索引 判断其是否存在@Testvoid testExists() throws IOException {GetIndexRequest request = new GetIndexRequest("yue_index");boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists);}//测试删除索引@Testvoid testDeleteIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("yue_index");//删除AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);System.out.println(delete.isAcknowledged());}//测试添加文档@Testvoid testAddDocument() throws IOException {//创建对象User user = new User("aa", 3);//创建请求IndexRequest request = new IndexRequest("yue_index");//规则 put /kuang_index/_doc/1request.id("1");request.timeout(TimeValue.timeValueSeconds(1));request.timeout("1s");//将我们的数据放入请求 jsonrequest.source(JSON.toJSONString(user), XContentType.JSON);//客户端发送请求,获取响应的结果IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);System.out.println(indexResponse.toString());//对应我们命令返回的状态 CREATEDSystem.out.println(indexResponse.status());}//获取文档,判断是否存在@Testvoid testIsExists() throws IOException {GetRequest request = new GetRequest("yue_index", "1");//不获取返回的 _source 的上下文了request.fetchSourceContext(new FetchSourceContext(false));request.storedFields("_none_");boolean exists = client.exists(request, RequestOptions.DEFAULT);System.out.println(exists);}//获得文档的信息@Testvoid testGetDocument() throws IOException {GetRequest request = new GetRequest("yue_index", "1");GetResponse response = client.get(request, RequestOptions.DEFAULT);//打印文档的内容System.out.println(response.getSourceAsString());//返回的全部内容和命令是一样的System.out.println(response);}//更新文档的信息@Testvoid testUpdateDocument() throws IOException {UpdateRequest request = new UpdateRequest("yue_index", "1");request.timeout("1s");User user = new User("狂神说Java", 18);request.doc(JSON.toJSONString(user), XContentType.JSON);UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);System.out.println(updateResponse.status());}//删除文档的记录@Testvoid testdeleteDocument() throws IOException {DeleteRequest request = new DeleteRequest("yue_index", "1");request.timeout("1s");DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);System.out.println(deleteResponse.status());}//特殊的,真实项目一般都会批量插入数据@Testvoid testBulkRequest() throws IOException {BulkRequest bulkRequest = new BulkRequest();bulkRequest.timeout("10s");List list = new ArrayList<>();list.add(new User("k1", 3));list.add(new User("k2", 3));list.add(new User("k3", 3));list.add(new User("g1", 3));list.add(new User("g2", 3));list.add(new User("g3", 3));//批处理请求for (int i = 0; i < list.size(); i++) {// 批量更新和批量删除,就在这里修改对应请求就可以了bulkRequest.add(new IndexRequest("yue_index").id("" + (i + 1)).source(JSON.toJSONString(list.get(i)), XContentType.JSON));}BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);//是否失败,返回 false 表示成功System.out.println(bulkResponse.hasFailures());}//查询//SearchRequest 搜索请求//SearchSourceBuilder 条件构造//HighlightBuilder 构建高亮//TermQueryBuilder 精确查询//MatchAllQueryBuilder//xxx QueryBuilder 对应我们刚才看到的命令@Testvoid testSearch() throws IOException {SearchRequest searchRequest = new SearchRequest("yue_index");//构建搜索的条件SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//查询条件,我们可以使用QueryBuilders工具类来实现//QueryBuilders.termQuery 精确//QueryBuilders.matchAllQuery() 匹配所有TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "q1");//MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();sourceBuilder.query(termQueryBuilder);sourceBuilder.from();sourceBuilder.size();sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));searchRequest.source(sourceBuilder);SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);System.out.println(JSON.toJSONString(searchResponse.getHits()));System.out.println("===================");for (SearchHit searchHit : searchResponse.getHits().getHits()) {System.out.println(searchHit.getSourceAsMap());}}}