Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringBoot+Elasticsearch

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Preface

1.1. Integration mode

There are 4 ways to integrate Elasticsearch into Spring Boot:

This article uses the latter two ways to connect and operate Elasticsearch respectively.

1.2. Environment and configuration

Server: elasticsearch-6.3.2 1

Client: elasticsearch 6.4.1

Server profile: elasticsearch.yml

/ etc/security/limits.conf

/ etc/sysctl.conf

1.3. Version

The default elasticsearch version of Spring Boot 2.0.5 is very low, so here we use the latest version 6.4.1

If it appears during startup

It indicates that the version of the jar package that elasticsearch depends on is inconsistent, so you can change it to 6.4.1.

In addition, Spring Boot 2.0.5 depends on spring-data-elasticsearch version 3.0.1, which needs to be upgraded to 3.1.0

Dependence

4.0.0

Com.cjs.example

Cjs-elasticsearch-example

0.0.1-SNAPSHOT

Jar

Cjs-elasticsearch-example

Org.springframework.boot

Spring-boot-starter-parent

2.0.5.RELEASE

UTF-8

UTF-8

1.8

6.4.1

3.1.0.RELEASE

Org.elasticsearch

Elasticsearch

${elasticsearch.version}

Org.elasticsearch.client

Transport

${elasticsearch.version}

Org.elasticsearch.client

Elasticsearch-rest-client

${elasticsearch.version}

Org.elasticsearch.plugin

Transport-netty4-client

${elasticsearch.version}

Org.springframework.data

Spring-data-elasticsearch

${spring.data.elasticsearch.version}

Org.springframework.boot

Spring-boot-starter-data-elasticsearch

Org.springframework.boot

Spring-boot-starter-web

Org.projectlombok

Lombok

True

Org.springframework.boot

Spring-boot-starter-test

Test

Org.springframework.boot

Spring-boot-maven-plugin

Application.properties

Perhaps, you will wonder, the port clearly written in the configuration file is 9200, why is the port written when connecting in the configuration file is 9300?

Because configuration 9200 is the port connected through HTTP, and 9300 is the port connected by TCP

Operation

4.1. Use Spring Data Elasticsearch Repositories to operate Elasticsearch

First, define an entity class

Package com.cjs.example.entity

Import lombok.Data

Import org.springframework.data.annotation.Id

Import org.springframework.data.elasticsearch.annotations.Document

Import java.io.Serializable;@Databr/ > @ Data

Public class Commodity implements Serializable {@ Idbr/ > @ Id

Private String name

Private String category

Private Integer price

Private String brand

Private Integer stock

Here, a Commodity instance is defined to represent a commodity. In Elasticsearch 6.x, type is not recommended, and type will be completely discarded in 7.x, so I only specify indexName here, not type. Here, a Commodity represents an item and an index record.

When analogous to a relational database, Index is equivalent to table and Document is equivalent to record

Then, you need to define an interface yourself and inherit ElasticsearchRepository

Package com.cjs.example.dao

Import com.cjs.example.entity.Commodity

Import org.springframework.data.elasticsearch.repository.ElasticsearchRepository

Import org.springframework.stereotype.Repository;@Repositorybr/ > @ Repository

Repository here is equivalent to DAO, and the operation of mysql or elasticsearch is the same.

Next, define the service interface

Package com.cjs.example.service

Import com.cjs.example.entity.Commodity

Import org.springframework.data.domain.Page

Import java.util.List

Public interface CommodityService {

Long count ()

Commodity save (Commodity commodity)

Void delete (Commodity commodity)

Iterable getAll ()

List getByName (String name)

Page pageQuery (Integer pageNo, Integer pageSize, String kw)

Implementation class

Package com.cjs.example.service.impl

Import com.cjs.example.entity.Commodity

Import com.cjs.example.dao.CommodityRepository

Import com.cjs.example.service.CommodityService

Import org.elasticsearch.index.query.MatchQueryBuilder

Import org.elasticsearch.index.query.QueryBuilders

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.data.domain.Page

Import org.springframework.data.domain.PageRequest

Import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder

Import org.springframework.data.elasticsearch.core.query.SearchQuery

Import org.springframework.stereotype.Service

Import java.util.ArrayList

Import java.util.List;@Servicebr/ > @ Servicebr/ > @ Autowiredbr/ > @ Override

Return commodityRepository.count (); @ Overridebr/ > @ Override

Return commodityRepository.save (commodity); @ Overridebr/ > @ Override

CommodityRepository.delete (commodity)

/ / commodityRepository.deleteById (commodity.getSkuId ()); @ Overridebr/ > @ Override

Return commodityRepository.findAll (); @ Overridebr/ > @ Override

List list = new ArrayList ()

MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder ("name", name)

Iterable iterable = commodityRepository.search (matchQueryBuilder)

Iterable.forEach (e-> list.add (e))

Return list;@Overridebr/ > @ Override

SearchQuery searchQuery = new NativeSearchQueryBuilder ()

.withQuery (QueryBuilders.matchPhraseQuery ("name", kw))

.withPageable (PageRequest.of (pageNo, pageSize))

.build ()

Return commodityRepository.search (searchQuery)

Add, delete, query and modify operations are demonstrated in this Service, as well as paged queries.

Finally, write a test class to test the methods in it

Package com.cjs.example

Import com.cjs.example.entity.Commodity

Import com.cjs.example.service.CommodityService

Import org.junit.Test

Import org.junit.runner.RunWith

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.boot.test.context.SpringBootTest

Import org.springframework.data.domain.Page

Import org.springframework.test.context.junit4.SpringRunner

Import java.util.List;@RunWith (SpringRunner.class) br/ > @ RunWith (SpringRunner.class)

Public class CjsElasticsearchExampleApplicationTests {@ Autowiredbr/ > @ Autowiredbr/ > @ Test

@ Testbr/ > System.out.println (commodityService.count ())

@ Test

Commodity commodity = new Commodity ()

Commodity.setSkuId ("1501009001")

Commodity.setName ("original sliced bread (10 slices)")

Commodity.setCategory ("101")

Commodity.setPrice (880)

Commodity.setBrand ("good goods shop")

CommodityService.save (commodity)

Commodity = new Commodity ()

Commodity.setSkuId ("1501009002")

Commodity.setName ("original sliced bread (6 slices)")

Commodity.setCategory ("101")

Commodity.setPrice (680)

Commodity.setBrand ("good goods shop")

CommodityService.save (commodity)

Commodity = new Commodity (); commodity.setSkuId ("1501009004")

Commodity.setName ("Yuanqi Tuji 850g")

Commodity.setCategory ("101")

Commodity.setPrice (120)

Commodity.setBrand ("hundred herbs")

CommodityService.save (commodity)

@ Testbr/ > commodity.setSkuId ("1501009004")

Commodity.setName ("Yuanqi Tuji 850g")

Commodity.setCategory ("101")

Commodity.setPrice (120)

Commodity.setBrand ("hundred herbs")

CommodityService.save (commodity)

@ Test

Commodity commodity = new Commodity (); commodity.setSkuId ("1501009002")

CommodityService.delete (commodity)

@ Testbr/ > commodity.setSkuId ("1501009002")

CommodityService.delete (commodity)

@ Test

Iterable iterable = commodityService.getAll (); iterable.forEach (e-> System.out.println (e.toString ()

@ Testbr/ > iterable.forEach (e-> System.out.println (e.toString ()

@ Test

List list = commodityService.getByName ("bread"); System.out.println (list)

@ Testbr/ > System.out.println (list)

@ Test

Page page = commodityService.pageQuery (0,10, "slice")

System.out.println (page.getTotalPages ())

System.out.println (page.getNumber ())

System.out.println (page.getContent ())

The above is the way to use Elasticsearch Repositories

4.2. Use ElasticsearchTemplate to operate Elasticsearch

Package com.cjs.example

Import com.cjs.example.entity.Commodity

Import org.elasticsearch.index.query.QueryBuilders

Import org.junit.Test

Import org.junit.runner.RunWith

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.boot.test.context.SpringBootTest

Import org.springframework.data.elasticsearch.core.ElasticsearchTemplate

Import org.springframework.data.elasticsearch.core.query.*

Import org.springframework.test.context.junit4.SpringRunner

Import java.util.List;@RunWith (SpringRunner.class) br/ > @ RunWith (SpringRunner.class)

Public class ElasticsearchTemplateTest {@ Autowiredbr/ > @ Autowiredbr/ > @ Test

Commodity commodity = new Commodity ()

Commodity.setSkuId ("1501009005")

Commodity.setName ("Grape Toast (10 pieces)")

Commodity.setCategory ("101")

Commodity.setPrice (160)

Commodity.setBrand ("good goods shop")

IndexQuery indexQuery = new IndexQueryBuilder (). WithObject (commodity). Build (); elasticsearchTemplate.index (indexQuery)

@ Testbr/ > elasticsearchTemplate.index (indexQuery)

@ Test

SearchQuery searchQuery = new NativeSearchQueryBuilder ()

.withQuery (QueryBuilders.matchQuery ("name", "toast"))

.build ()

List list = elasticsearchTemplate.queryForList (searchQuery, Commodity.class)

System.out.println (list)

ElasticsearchTemplate is configured automatically

Demo

Engineering structure

Referenc

Https://docs.spring.io/spring-data/elasticsearch/docs/3.1.0.RELEASE/reference/html/#repositories.query-methods.details

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report