【ElasticSearch】常用的JAVA API(长期更新)
背景
主要介绍和整理ElasticSearch 常用的API
环境
SpringBoot2.1+Maven
Maven依赖
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> </dependency> //ElasticSearch高级客户端 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency>
@Configuration public class ESConfig { @Value("${yunshangxue.elasticsearch.hostlist}") private String hostlist; // 127.0.0.1:9200 @Bean // 高版本客户端 public RestHighLevelClient restHighLevelClient() { // 解析 hostlist 配置信息。假如以后有多个,则需要用 , 分开 String[] split = hostlist.split(","); // 创建 HttpHost 数组,其中存放es主机和端口的配置信息 HttpHost[] httpHostArray = new HttpHost[split.length]; for (int i = 0; i < split.length; i++) { String item = split[i]; httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http"); } // 创建RestHighLevelClient客户端 return new RestHighLevelClient(RestClient.builder(httpHostArray)); } // 项目主要使用 RestHighLevelClient,对于低级的客户端暂时不用 @Bean public RestClient restClient() { // 解析hostlist配置信息 String[] split = hostlist.split(","); // 创建HttpHost数组,其中存放es主机和端口的配置信息 HttpHost[] httpHostArray = new HttpHost[split.length]; for (int i = 0; i < split.length; i++) { String item = split[i]; httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http"); } return RestClient.builder(httpHostArray).build(); } }
索引结构
@Data @Document(indexName = "text-audit", type = "text_audit_type", shards = 1, replicas = 0, createIndex = false) public class TextModerationEntity { @Id private Long dbId; //.....其他省略 }
索引Mapper
public interface TextModerationRepository extends ElasticsearchRepository<TextModerationEntity,Long> { }
API
查询
1.分页搜索--通过SpringBootTemplate实现
@Autowired private IMTextModerationRepository imTextMdrRepository; BoolQueryBuilder builder = QueryBuilders.boolQuery(); //......省略其他查询条件 NativeSearchQuery queryBuilder = new NativeSearchQueryBuilder() .withQuery(builder) .withPageable(PageRequest.of(当前页码 - 1, 每页条数) .withSort(SortBuilders.fieldSort("xxxx").order(SortOrder.DESC)) .build(); Page<IMTextModerationEntity> pageResult = imTextMdrRepository.search(queryBuilder);
2.根据ID查询--通过SpringBootTemplate实现
Optional<IMTextModerationEntity> optional = imTextMdrRepository.findById(dbId);
3.分页查询(根据其他查询条件查询)--通过SpringBootTemplate实现
@Autowired private ElasticsearchTemplate elasticsearchTemplate; NativeSearchQuery queryBuilder = new NativeSearchQueryBuilder() .withQuery(builder) .withPageable(PageRequest.of(当前页码 - 1, 每页条数)) .build(); Page<IMTextModerationEntity> page = elasticsearchTemplate.queryForPage(queryBuilder, IMTextModerationEntity.class);
4.列表查询--通过SpringBootTemplate实现
@Autowired private ElasticsearchTemplate elasticsearchTemplate; NativeSearchQuery queryBuilder = new NativeSearchQueryBuilder() .withQuery(builder) .build();
List<ImageModerationEntity> entityList = elasticsearchTemplate.queryForList(queryBuilder, ImageModerationEntity.class);
5.列表查询--通过RestHighLevelClient实现
@Test public void testSearchAll() throws IOException, ParseException { // 搜索请求对象 SearchRequest searchRequest = new SearchRequest("ysx_course"); // 指定类型 searchRequest.types("索引类型"); // 搜索源构建对象 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 搜索方式 // matchAllQuery搜索全部 searchSourceBuilder.query(QueryBuilders.matchAllQuery()); // 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段 searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{}); // 向搜索请求对象中设置搜索源 searchRequest.source(searchSourceBuilder); // 执行搜索,向ES发起http请求 SearchResponse searchResponse = client.search(searchRequest); // 搜索结果 SearchHits hits = searchResponse.getHits(); // 匹配到的总记录数 long totalHits = hits.getTotalHits(); // 得到匹配度高的文档 SearchHit[] searchHits = hits.getHits(); // 日期格式化对象 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for(SearchHit hit:searchHits){ // 文档的主键 String id = hit.getId(); // 源文档内容 Map<String, Object> sourceAsMap = hit.getSourceAsMap(); String name = (String) sourceAsMap.get("name"); // 由于前边设置了源文档字段过虑,这时description是取不到的 String description = (String) sourceAsMap.get("description"); // 学习模式 String studymodel = (String) sourceAsMap.get("studymodel"); // 价格 Double price = (Double) sourceAsMap.get("price"); // 日期 Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp")); System.out.println(name); System.out.println(studymodel); System.out.println("描述~" + description); System.out.println(price); } }
新增
@Autowired private IMTextModerationRepository imTextMdrRepository; IMTextModerationEntity entity = new IMTextModerationEntity(); ....... imTextMdrRepository.save(entity);
更新
1.根据文档ID去更新
@Autowired private ElasticsearchTemplate elasticsearchTemplate; UpdateRequest updateRequest = new UpdateRequest(); Map<String, Object> params = Maps.newHashMap(); params.put("属性名", "属性值"); updateRequest.doc(params); UpdateQuery updateQuery = new UpdateQueryBuilder() .withId(String.valueOf(dbId)) .withClass(ImageModerationEntity.class) .withUpdateRequest(updateRequest) .build(); UpdateResponse update = elasticsearchTemplate.update(updateQuery);
2.根据其他查询条件去更新(相当于updateByQuery)
@Autowired private ElasticsearchTemplate elasticsearchTemplate; Client client = elasticsearchTemplate.getClient(); UpdateByQueryRequestBuilder builder = UpdateByQueryAction.INSTANCE.newRequestBuilder(client); builder.source("索引名/索引别名") .filter(QueryBuilders.termQuery("属性名", "属性值")) .script(new Script("ctx._source.属性名=属性值")); BulkByScrollResponse response = builder.get(); long count = response.getUpdated();
删除
//TODO
索引操作--删除索引
@Test public void testDelIndex() throws IOException { // 操作索引的对象 IndicesClient indices = client.indices(); // 删除索引的请求 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("索引名称"); // 删除索引 DeleteIndexResponse response = indices.delete(deleteIndexRequest); // 得到响应 boolean b = response.isAcknowledged(); System.out.println(b); }
索引操作--创建索引
创建索引,步骤和删除类似,需要注意的是删除的时候需要指定 ES 库分片的数量和副本的数量,并且在创建索引的时候可以将映射一起指定了。
public void testAddIndex() throws IOException { // 操作索引的对象 IndicesClient indices = client.indices(); // 创建索引的请求 CreateIndexRequest request = new CreateIndexRequest("ysx_course"); request.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0")); // 创建映射 request.mapping("doc", "{\n" + " \"properties\": {\n" + " \"description\": {\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_max_word\",\n" + " \"search_analyzer\": \"ik_smart\"\n" + " },\n" + " \"name\": {\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_max_word\",\n" + " \"search_analyzer\": \"ik_smart\"\n" + " },\n" + "\"pic\":{ \n" + "\"type\":\"text\", \n" + "\"index\":false \n" + "}, \n" + " \"price\": {\n" + " \"type\": \"float\"\n" + " },\n" + " \"studymodel\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"timestamp\": {\n" + " \"type\": \"date\",\n" + " \"format\": \"yyyy-MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis\"\n" + " }\n" + " }\n" + " }", XContentType.JSON); // 执行创建操作 CreateIndexResponse response = indices.create(request); // 得到响应 boolean b = response.isAcknowledged(); System.out.println(b);
参考链接
1.https://mp.weixin.qq.com/s/nqYEGlldmWW_SS87FRdpmg
2.https://blog.csdn.net/justlpf/article/details/121493781
分类:
ElasticSearch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
2021-08-30 【bitmap】Redis数据结构bitmap
2020-08-30 【Nacos】Springboot整合nacos配置中心(一)