内容简介:同上本文同步发表在公众号,欢迎大家关注!:grin: 后续笔记欢迎关注获取第一时间更新!
<!-- Spring Boot Elasticsearch 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> spring: data: elasticsearch: repositories: enabled: true cluster-name: docker-cluster cluster-nodes: lujiahao.ml:9300 复制代码
1.2 创建Repository
@Repository public interface PersonEsRepository extends ElasticsearchRepository<Person,Long> { List<Person> findPersonByName(String name); } 复制代码
1.3 文档实体类
@Data @Document(indexName = "person", type = "chinese") public class Person implements Serializable{ private static final long serialVersionUID = -6804453833406105286L; @Id private Long id; private String name; private Integer age; private String address; } 复制代码
1.4 增删改查
@Service public class EsStarterService { @Autowired private PersonEsRepository repository; /** * 增 */ public Person save(Person person) { return repository.save(person); } /** * 删 */ public void delete(Person person) { repository.delete(person); } /** * 改 */ public Person update(Person person) { return repository.save(person); } /** * 查 */ public Iterable<Person> findAll() { return repository.findAll(); } } 复制代码
1.5 单元测试
@RunWith(SpringRunner.class) @SpringBootTest public class EsStarterServiceTest { @Autowired private EsStarterService esStarterService; @Test public void save() { Person person = new Person(); person.setId(new Random().nextLong()); person.setName("lujiahao"); esStarterService.save(person); } @Test public void delete() { Person person = new Person(); person.setId(-5264182431891613084L); person.setName("lujiahao123456"); esStarterService.delete(person); } @Test public void update() { Person person = new Person(); person.setId(542136934419565287L); person.setName("lujiahao123456"); esStarterService.update(person); } @Test public void findAll() { Iterable<Person> all = esStarterService.findAll(); all.forEach(System.out::println); } } 复制代码
2. ElasticsearchTemplate
2.1 pom.xml和application.ym
<!--elasticsearch--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> </dependency> spring: data: elasticsearch: repositories: enabled: true cluster-name: docker-cluster cluster-nodes: lujiahao.ml:9300 复制代码
2.2 文档实体类
同上
2.3 增删改查
@Service public class ElasticsearchTemplateService { @Autowired public ElasticsearchTemplate elasticsearchTemplate; private static final String INDEX_NAME = "person"; private static final String TYPE_NAME = "chinese"; /** * 增 */ public String save(Person person) { IndexQuery indexQuery = new IndexQueryBuilder() .withIndexName(INDEX_NAME) .withType(TYPE_NAME) .withId(String.valueOf(person.getId())) .withObject(person) .build(); String index = elasticsearchTemplate.index(indexQuery); System.out.println("xxxxxxxxxxxx " + index); return index; } /** * 删 */ public void deleteByName(String name) { DeleteQuery deleteQuery = new DeleteQuery(); deleteQuery.setQuery(QueryBuilders.matchQuery("name", name)); deleteQuery.setIndex(INDEX_NAME); deleteQuery.setType(TYPE_NAME); elasticsearchTemplate.delete(deleteQuery); } /** * 改 */ public UpdateResponse update(Person person) { try { UpdateRequest updateRequest = new UpdateRequest() .index(INDEX_NAME) .type(TYPE_NAME) .id(String.valueOf(person.getId())) .doc(XContentFactory.jsonBuilder() .startObject() .field("name", person.getName()) .endObject()); UpdateQuery updateQuery = new UpdateQueryBuilder() .withIndexName(INDEX_NAME) .withType(TYPE_NAME) .withId(String.valueOf(person.getId())) .withClass(Person.class) .withUpdateRequest(updateRequest) .build(); UpdateResponse update = elasticsearchTemplate.update(updateQuery); return update; } catch (Exception e) { return null; } } /** * 查 */ public List<Person> getAll() { SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.matchAllQuery()) .build(); return elasticsearchTemplate.queryForList(searchQuery, Person.class); } } 复制代码
2.4 单元测试
@RunWith(SpringRunner.class) @SpringBootTest public class ElasticsearchTemplateServiceTest { @Autowired private ElasticsearchTemplateService elasticsearchTemplateService; @Test public void save() { Person person = new Person(); person.setId(new Random().nextLong()); person.setName("haha"); String save = elasticsearchTemplateService.save(person); System.out.println(save); } @Test public void deleteByName() { elasticsearchTemplateService.deleteByName("lujiahao"); } @Test public void update() { Person person = new Person(); person.setId(-5264182431891613084L); person.setName("hahaaaaaaaaa"); UpdateResponse update = elasticsearchTemplateService.update(person); System.out.println(update); } @Test public void getAll() { List<Person> all = elasticsearchTemplateService.getAll(); System.out.println(all); } } 复制代码
3. 代码示例
https://github.com/lujiahao0708/LearnSeries/tree/master/LearnElasticSerach 复制代码
Tips
本文同步发表在公众号,欢迎大家关注!:grin: 后续笔记欢迎关注获取第一时间更新!
以上所述就是小编给大家介绍的《《Elasticsearch技术解析与实战》Chapter 1.4 Spring Boot整合Elasticsearch》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- ItemDecoration深入解析与实战(一)
- 新书上市 -《Elasticsearch 源码解析与优化实战》
- ItemDecoration深入解析与实战(二)—— 实际运用
- ItemDecoration深入解析与实战(一)——源码分析
- Spring Boot 外部化配置实战解析
- Spring Boot外部化配置实战解析
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
代码大全(第2版)
[美] 史蒂夫·迈克康奈尔 / 金戈、汤凌、陈硕、张菲 译、裘宗燕 审校 / 电子工业出版社 / 2006-3 / 128.00元
第2版的《代码大全》是著名IT畅销书作者史蒂夫·迈克康奈尔11年前的经典著作的全新演绎:第2版不是第一版的简单修订增补,而是完全进行了重写;增加了很多与时俱进的内容。这也是一本完整的软件构建手册,涵盖了软件构建过程中的所有细节。它从软件质量和编程思想等方面论述了软件构建的各个问题,并详细论述了紧跟潮流的新技术、高屋建瓴的观点、通用的概念,还含有丰富而典型的程序示例。这本书中所论述的技术不仅填补了初......一起来看看 《代码大全(第2版)》 这本书的介绍吧!