SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码) (3)

测试代码如下

@Test public void testCreate() { System.out.println(elasticsearchTemplate.createIndex(StoreDocument.class)); System.out.println(elasticsearchTemplate.putMapping(StoreDocument.class)); } 删除索引

ElasticsearchTemplate提供了2个deleteIndex()方法来删除索引

@Override public <T> boolean deleteIndex(Class<T> clazz) { return deleteIndex(getPersistentEntityFor(clazz).getIndexName()); } @Override public boolean deleteIndex(String indexName) { Assert.notNull(indexName, "No index defined for delete operation"); if (indexExists(indexName)) { return client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet().isAcknowledged(); } return false; } 新增&修改文档

在Elasticsearch中文档是不可改变的,不能修改它们。相反,如果想要更新现有的文档,需要重建索引或者进行替换。

所以可以使用和新增同样的接口来对文档进行修改操作。区分的依据就是id。

下面提供新增&修改文档的其中两种方法,一种是通过ElasticsearchTemplate提供的index()方法:

@Override public String index(IndexQuery query) { String documentId = prepareIndex(query).execute().actionGet().getId(); // We should call this because we are not going through a mapper. if (query.getObject() != null) { setPersistentEntityId(query.getObject(), documentId); } return documentId; }

示例代码如下:

/** * 更新索引 * @param indexName 索引名称 * @param type 索引类型 * @param id ID * @param jsonDoc JSON格式的文档 * @param refresh 是否刷新索引 * @return ID */ public String index(String indexName, String type, String id, JsonNode jsonDoc, boolean refresh) throws JsonProcessingException { log.info("AbstractDocumentIndexService更新索引.indexName:{},type:{},id:{},jsonDoc:{}", indexName, type, id, jsonDoc); IndexQuery indexQuery = new IndexQueryBuilder() .withIndexName(indexName) .withType(type) .withId(id) .withSource(objectMapper.writeValueAsString(jsonDoc)) .build(); try { if (elasticsearchTemplate.indexExists(indexName)) { String index = elasticsearchTemplate.index(indexQuery); if (refresh) { elasticsearchTemplate.refresh(indexName); } return index; } } catch (Exception e) { log.error("更新索引失败,刷新ES重试", e); elasticsearchTemplate.refresh(indexName); return elasticsearchTemplate.index(indexQuery); } throw BaseException.INDEX_NOT_EXISTS_EXCEPTION.build(); }

另一种则是通过Repository接口。Spring提供的ES的Repository接口为ElasticsearchCrudRepository,所以我们就可以直接定义额新的接口,然后实现ElasticsearchCrudRepository即可:

package com.taoche.docindex.repo; import com.taoche.document.StoreDocument; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /** * 门店Repository * @author 李锋镝 * @date Create at 09:30 2019/8/23 */ public interface StoreRepository extends ElasticsearchRepository<StoreDocument, String> { }

示例代码如下:

@Test public void testSave() { StoreDocument storeDocument = new StoreDocument(); storeDocument.setId("1"); StoreBaseInfo baseInfo = new StoreBaseInfo(); baseInfo.setStoreId("1"); baseInfo.setCreatedTime(DateTime.now()); storeDocument.setBaseInfo(baseInfo); storeRepository.save(storeDocument); } 查询

ES的主要功能就是查询,ElasticsearchRepository也提供了基本的查询接口,比如findById()、findAll()、findAllById()、search()等方法;当然也可以使用Spring Data提供的另外一个功能:Spring Data JPA——通过方法名创建查询,当然需要遵循一定的规则,比如你的方法名叫做findByTitle(),那么它就知道你是根据title查询,然后自动帮你完成,这里就不仔细说了。

上边说的基本能满足一般的查询,复杂一点的查询就无能为力了,这就需要用到自定义查询,这里可以查看我的另一篇博客SpringBoot使用注解的方式构建Elasticsearch查询语句,实现多条件的复杂查询,这里边有详细的说明。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgjdp.html