How to integrate elasticsearch in springboot
1, introduce dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
2, write entity mapping class
@Data @Document(indexName = "index", createIndex = true) public class Index { @Id private String id; @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart") private String content; }
3, write access interface (if you need to automatically create an index, this interface must be written, otherwise it will not work when the project starts Automatically detect and create indexes)
@Repository public interface IndexRepository extends ElasticsearchRepository<Index, String> { Page<Index> findByContent(String content, Pageable page); }
4, test, use template, and repository to test
@SpringBootTest public class EsTest { @Autowired ElasticsearchRestTemplate esTemplate; @Autowired IndexRepository indexRepository; @BeforeEach public void init() { System.out.println("init"); indexRepository.deleteAll(); indexRepository.saveAll(ListUtil.of( new Index("1","美国留给伊拉克的是个烂摊子吗"), new Index("2","公安部:各地校车将享最高路权"), new Index("3","中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"), new Index("4","中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"), new Index("5","中国天眼向全球正式开放下月世界大赛将比拼FAST脉冲星搜索") )); } @Test void testRepositoryQuery() { Page<Index> pageList = indexRepository.findByContent("中国", PageRequest.of(0, 10)); pageList.getContent().forEach(e -> { System.out.println("repositoryQuery => "+e); }); } @Test void testTemplateQuery() { BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.simpleQueryStringQuery("中国").field("content")); NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(queryBuilder) .withPageable(PageRequest.of(0, 10)) .build(); SearchHits<Index> search = esTemplate.search(query, Index.class); if(search.hasSearchHits()) { search.getSearchHits().forEach(e -> { System.out.println("templateQuery => "+e.getContent()); }); } } }
init data templateQuery => Index(id=3, content=中韩渔警冲突调查:韩警平均每天扣1艘中国渔船) templateQuery => Index(id=4, content=中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首) templateQuery => Index(id=5, content=中国天眼向全球正式开放下月世界大赛将比拼FAST脉冲星搜索) init data repositoryQuery => Index(id=3, content=中韩渔警冲突调查:韩警平均每天扣1艘中国渔船) repositoryQuery => Index(id=4, content=中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首) repositoryQuery => Index(id=5, content=中国天眼向全球正式开放下月世界大赛将比拼FAST脉冲星搜索)
5, you can start a scheduled task and ping regularly to prevent Connection time out
@Scheduled(fixedRate = 15000) public void ping() { esTemplate.execute(client -> client.ping(RequestOptions.DEFAULT)); }
The above is the detailed content of How to integrate elasticsearch in springboot. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

This article will write a detailed example to talk about the actual development of dubbo+nacos+Spring Boot. This article will not cover too much theoretical knowledge, but will write the simplest example to illustrate how dubbo can be integrated with nacos to quickly build a development environment.

What is the difference between SpringBoot and SpringMVC? SpringBoot and SpringMVC are two very popular Java development frameworks for building web applications. Although they are often used separately, the differences between them are obvious. First of all, SpringBoot can be regarded as an extension or enhanced version of the Spring framework. It is designed to simplify the initialization and configuration process of Spring applications to help developers

How to use PHP and Elasticsearch to achieve highlighted search results Introduction: In the modern Internet world, search engines have become the main way for people to obtain information. In order to improve the readability and user experience of search results, highlighting search keywords has become a common requirement. This article will introduce how to use PHP and Elasticsearch to achieve highlighted search results. 1. Preparation Before starting, we need to ensure that PHP and Elasticsearch have been installed and configured correctly.

PHPElasticsearch: How to use dynamic mapping to achieve flexible search capabilities? Introduction: Search functionality is an integral part of developing modern applications. Elasticsearch is a powerful search and analysis engine that provides rich functionality and flexible data modeling. In this article, we will focus on how to use dynamic mapping to achieve flexible search capabilities. 1. Introduction to dynamic mapping In Elasticsearch, mapping (mapp

In-depth study of Elasticsearch query syntax and practical introduction: Elasticsearch is an open source search engine based on Lucene. It is mainly used for distributed search and analysis. It is widely used in full-text search of large-scale data, log analysis, recommendation systems and other scenarios. When using Elasticsearch for data query, flexible use of query syntax is the key to improving query efficiency. This article will delve into the Elasticsearch query syntax and give it based on actual cases.

Summary of log analysis and exception monitoring based on Elasticsearch in PHP: This article will introduce how to use the Elasticsearch database for log analysis and exception monitoring. Through concise PHP code examples, it shows how to connect to the Elasticsearch database, write log data to the database, and use Elasticsearch's powerful query function to analyze and monitor anomalies in the logs. Introduction: Log analysis and exception monitoring are

Introduction to the Practical Guide for the Integration of PHPElasticsearch and Relational Databases: With the advent of the Internet and big data era, data storage and processing methods are also constantly evolving. Traditional relational databases have gradually shown some shortcomings when faced with scenarios such as massive data, high concurrent reading and writing, and full-text search. As a real-time distributed search and analysis engine, Elasticsearch has gradually attracted the attention and use of the industry through its high-performance full-text search, real-time analysis and data visualization functions. Ran
