至此 , 客户端配置完毕 , 项目启动的时候 , 会自动注入到 Spring 的 ioc 容器里面 。
2.4、索引管理es 中最重要的就是索引库 , 客户端如何创建呢?请看下文!
- 创建索引
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit {@Autowiredprivate RestHighLevelClient client;/*** 创建索引(简单模式)* @throws IOException*/@Testpublic void createIndex() throws IOException {CreateIndexRequest request = new CreateIndexRequest("cs_index");CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());}/*** 创建索引(复杂模式)* 可以直接把对应的文档结构也一并初始化* @throws IOException*/@Testpublic void createIndexComplete() throws IOException {CreateIndexRequest request = new CreateIndexRequest();//索引名称request.index("cs_index");//索引配置Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build();request.settings(settings);//映射结构字段Map<String, Object> properties = new HashMap();properties.put("id", ImmutableBiMap.of("type", "text"));properties.put("name", ImmutableBiMap.of("type", "text"));properties.put("sex", ImmutableBiMap.of("type", "text"));properties.put("age", ImmutableBiMap.of("type", "long"));properties.put("city", ImmutableBiMap.of("type", "text"));properties.put("createTime", ImmutableBiMap.of("type", "long"));Map<String, Object> mapping = new HashMap<>();mapping.put("properties", properties);//添加一个默认类型System.out.println(JSON.toJSONString(request));request.mapping("_doc",mapping);CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());}}
- 删除索引
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit {@Autowiredprivate RestHighLevelClient client;/*** 删除索引* @throws IOException*/@Testpublic void deleteIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("cs_index1");AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());}}
- 查询索引
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit {@Autowiredprivate RestHighLevelClient client;/*** 查询索引* @throws IOException*/@Testpublic void getIndex() throws IOException {// 创建请求GetIndexRequest request = new GetIndexRequest();request.indices("cs_index");// 执行请求,获取响应GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);System.out.println(response.toString());}}
- 查询索引是否存在
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit {@Autowiredprivate RestHighLevelClient client;/*** 检查索引是否存在* @throws IOException*/@Testpublic void exists() throws IOException {// 创建请求GetIndexRequest request = new GetIndexRequest();request.indices("cs_index");// 执行请求,获取响应boolean response = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(response);}}
- 查询所有的索引名称
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit {@Autowiredprivate RestHighLevelClient client;/*** 查询所有的索引名称* @throws IOException*/@Testpublic void getAllIndices() throws IOException {GetAliasesRequest request = new GetAliasesRequest();GetAliasesResponse response =client.indices().getAlias(request,RequestOptions.DEFAULT);Map<String, Set<AliasMetaData>> map = response.getAliases();Set<String> indices = map.keySet();for (String key : indices) {System.out.println(key);}}}
- 查询索引映射字段
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit {@Autowiredprivate RestHighLevelClient client;/*** 查询索引映射字段* @throws IOException*/@Testpublic void getMapping() throws IOException {GetMappingsRequest request = new GetMappingsRequest();request.indices("cs_index");request.types("_doc");GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT);System.out.println(response.toString());}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- SpringBoot接口 - 如何生成接口文档之Swagger技术栈?
- SpringBoot 各种 Web 容器服开启 AccessLog 日志
- 初识SpringBoot Starter
- SpringBoot整合JWT实现登录认证
- SpringBoot定时跑批
- RocketMQ与SpringBoot整合进行生产级二次封装
- SpringBoot统一封装返回前端结果集
- 吉利|整合“长城、比亚迪”!吉利全新RADAR品牌将发:主打新能源皮卡
- SpringBoot 实现 Office 各种格式在线预览
- springboot集成elasticsearch