package com.es.elasticsearch;
import com.alibaba.fastjson.JSON;
import com.es.elasticsearch.pojo.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.swing.text.Highlighter;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
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.XPackClient;
import org.elasticsearch.client.indices.CreateDataStreamRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.core.TimeValue;
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 org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ElasticSearchApplicationTests {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient restHighLevelClient;
// 索引的创建
@Test
void testCreateIndex() throws IOException {
String index = "text_index";
// 1. 创建索引请求 等价于 PUT /gorit_index
CreateIndexRequest request = new CreateIndexRequest(index);
// 2. 执行创建请求 IndicesClient, 请求后获得响应
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
// 判断索引是否存在 如果存在则删除
@Test
void testExistIndex() throws IOException {
String index = "text_index";
GetIndexRequest getIndexRequest = new GetIndexRequest(index);
boolean exists = restHighLevelClient.indices()
.exists(getIndexRequest, RequestOptions.DEFAULT);
if(exists){
// 如果存在则删除
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
AcknowledgedResponse delete = restHighLevelClient.indices()
.delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
}
// 添加文档
@Test
void testAddDoctoIndex() throws IOException {
String index = "text_index";
IndexRequest indexRequest = new IndexRequest(index);
// 规则 PUT /gorit_index/_doc/1
// 可以不指定ID
indexRequest.id("1");
indexRequest.timeout(TimeValue.timeValueSeconds(1));
indexRequest.timeout("1s");
// 将数据放入请求json
indexRequest.source(JSON.toJSON(new User("李四",19)), XContentType.JSON);
//客户端发送请求
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
// 获取文档 ,判断是否存在 get /index/doc/1
@Test
void testIsExists() throws IOException {
String index = "text_index";
GetRequest getRequest = new GetRequest(index,"1");
// 不获取返回的 _source 的上下文了
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
// 获取文档信息
@Test
void testGetDocument() throws IOException {
String index = "text_index";
GetRequest getRequest = new GetRequest(index,"1");
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse);
System.out.println(getResponse.getSourceAsString());
}
// 更新文档信息
@Test
void testUpdateDocument() throws IOException {
String index = "text_index";
UpdateRequest updateRequest = new UpdateRequest(index,"1");
User user = new User("李四", 19);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}
// 删除文档信息
@Test
void testDeleteDocument() throws IOException {
String index = "text_index";
DeleteRequest deleteRequest = new DeleteRequest(index,"1");
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
}
// 批量插入数据
@Test
void testBulkDocument() throws IOException {
String index = "text_index";
BulkRequest BulkRequest = new BulkRequest();
BulkRequest.timeout("30s");
ArrayList<User> users = new ArrayList<>();
users.add(new User("李四", 19));
users.add(new User("李四", 19));
users.add(new User("李四", 19));
users.add(new User("李四", 19));
users.add(new User("李四", 19));
users.add(new User("李四", 19));
users.add(new User("李四", 19));
for(User user:users){
BulkRequest.add(new IndexRequest(index).source(JSON.toJSONString(user),XContentType.JSON));
}
BulkResponse bulkResponse = restHighLevelClient.bulk(BulkRequest, RequestOptions.DEFAULT);
// 是否失败
System.out.println(bulkResponse.hasFailures());
}
// 查询
// SearchRequest 搜索请求
// SearchSourceBuilder条件构造
// HighlightBuilder 构建高亮
// TermQueryBuilder 精确查询
// MatchAllQueryBuilder
// xxx QueryBuilder
@Test
void testSearch() throws IOException {
String index = "text_index";
SearchRequest searchRequest = new SearchRequest(index);
// 构建搜索的条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
/**
* 查询条件 使用 QueryBuilders 工具类来实现
* QueryBuilders.termQuery 精确
* QueryBuilders.matchAllQueryBuilder() 匹配所有
*/
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "四");//精确查询
// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
sourceBuilder.query(termQueryBuilder);
// 分页
sourceBuilder.from(0);
sourceBuilder.size(5);
sourceBuilder.highlighter(); // 设置高亮
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 构建搜索
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
System.out.println("==========================================");
for (SearchHit documentFields: searchResponse.getHits().getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Elasticsearch 应用
共159个文件
xml:127个
class:9个
java:9个
0 下载量 7 浏览量
2023-11-17
18:20:27
上传
评论
收藏 188KB ZIP 举报
温馨提示
Elasticsearch 应用
资源推荐
资源详情
资源评论
收起资源包目录
Elasticsearch 应用 (159个子文件)
ElasticSearchApplicationTests.class 11KB
ContextService.class 9KB
Context.class 2KB
HtmlParseUtil.class 2KB
User.class 2KB
ContextController.class 2KB
ElasticSearchClientConfig.class 1KB
ElasticSearchApplication.class 768B
IndexController.class 641B
mvnw.cmd 7KB
.gitignore 395B
.gitignore 184B
index.html 777B
index.html 714B
elasticSearch.iml 14KB
ElasticSearchApplicationTests.java 8KB
ContextService.java 6KB
ContextController.java 1KB
HtmlParseUtil.java 1KB
ElasticSearchClientConfig.java 560B
IndexController.java 430B
ElasticSearchApplication.java 332B
Context.java 280B
User.java 246B
vue.min.js 91KB
vue.min.js 91KB
axios.min.js 29KB
axios.min.js 29KB
HELP.md 1KB
mvnw 10KB
application.properties 64B
application.properties 64B
uiDesigner.xml 9KB
workspace.xml 8KB
pom.xml 3KB
jarRepositories.xml 871B
compiler.xml 830B
Maven__org_springframework_boot_spring_boot_starter_data_elasticsearch_2_7_17.xml 776B
Maven__org_elasticsearch_client_elasticsearch_rest_high_level_client_7_14_0.xml 762B
Maven__org_springframework_boot_spring_boot_configuration_processor_2_7_17.xml 755B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_13_5.xml 736B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_7_17.xml 720B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_smile_2_13_5.xml 710B
Maven__org_elasticsearch_elasticsearch_plugin_classloader_7_14_0.xml 706B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_cbor_2_13_5.xml 703B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_yaml_2_13_5.xml 703B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 700B
Maven__org_springframework_boot_spring_boot_starter_logging_2_7_17.xml 699B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_13_5.xml 695B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_7_17.xml 692B
Maven__org_elasticsearch_client_elasticsearch_rest_client_7_14_0.xml 685B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_7_17.xml 685B
Maven__org_springframework_data_spring_data_elasticsearch_4_4_17.xml 685B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_13_5.xml 681B
Maven__org_springframework_boot_spring_boot_starter_test_2_7_17.xml 678B
Maven__org_elasticsearch_plugin_aggs_matrix_stats_client_7_14_0.xml 678B
Maven__org_springframework_boot_spring_boot_starter_json_2_7_17.xml 678B
Maven__org_springframework_boot_spring_boot_starter_web_2_7_17.xml 671B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_82.xml 660B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_13_5.xml 651B
Maven__org_springframework_boot_spring_boot_devtools_2_7_17.xml 650B
Maven__org_elasticsearch_plugin_lang_mustache_client_7_14_0.xml 650B
Maven__org_elasticsearch_plugin_mapper_extras_client_7_14_0.xml 650B
Maven__org_elasticsearch_elasticsearch_x_content_7_14_0.xml 643B
Maven__org_springframework_boot_spring_boot_starter_2_7_17.xml 643B
Maven__org_springframework_data_spring_data_commons_2_7_17.xml 643B
Maven__org_elasticsearch_elasticsearch_secure_sm_7_14_0.xml 643B
Maven__org_apache_lucene_lucene_analyzers_common_8_9_0.xml 636B
Maven__org_elasticsearch_plugin_parent_join_client_7_14_0.xml 636B
Maven__org_junit_platform_junit_platform_commons_1_8_2.xml 633B
Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml 633B
Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml 633B
Maven__com_alibaba_fastjson2_fastjson2_extension_2_0_20.xml 631B
Maven__com_fasterxml_jackson_core_jackson_databind_2_13_5.xml 630B
Maven__org_apache_lucene_lucene_backward_codecs_8_9_0.xml 629B
Maven__org_junit_platform_junit_platform_engine_1_8_2.xml 626B
Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_82.xml 625B
Maven__org_elasticsearch_plugin_rank_eval_client_7_14_0.xml 622B
Maven__org_apache_lucene_lucene_spatial_extras_8_9_0.xml 622B
Maven__org_springframework_boot_spring_boot_test_2_7_17.xml 622B
Maven__co_elastic_clients_elasticsearch_java_7_17_14.xml 619B
Maven__org_junit_jupiter_junit_jupiter_params_5_8_2.xml 615B
Maven__org_junit_jupiter_junit_jupiter_engine_5_8_2.xml 615B
Maven__org_apache_httpcomponents_httpasyncclient_4_1_5.xml 612B
Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml 611B
Maven__org_apache_tomcat_embed_tomcat_embed_el_9_0_82.xml 611B
Maven__org_springframework_spring_expression_5_3_30.xml 609B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_17_2.xml 608B
Maven__org_elasticsearch_elasticsearch_core_7_14_0.xml 608B
Maven__com_fasterxml_jackson_core_jackson_core_2_13_5.xml 602B
Maven__org_elasticsearch_elasticsearch_cli_7_14_0.xml 601B
Maven__org_elasticsearch_elasticsearch_geo_7_14_0.xml 601B
Maven__org_apache_lucene_lucene_highlighter_8_9_0.xml 601B
Maven__org_apache_lucene_lucene_queryparser_8_9_0.xml 601B
Maven__org_apache_httpcomponents_httpcore_nio_4_4_16.xml 598B
Maven__org_mockito_mockito_junit_jupiter_4_5_1.xml 598B
Maven__com_github_spullara_mustache_java_compiler_0_9_6.xml 595B
Maven__org_junit_jupiter_junit_jupiter_api_5_8_2.xml 594B
Maven__org_springframework_spring_context_5_3_30.xml 588B
Maven__org_apache_lucene_lucene_spatial3d_8_9_0.xml 587B
共 159 条
- 1
- 2
资源评论
是个吃不饱的人
- 粉丝: 282
- 资源: 36
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功