SpringBoot——Elasticsearch(二十八)

安装ES

依赖

1
2
3
4
5
<!-- 添加 spring-data-elasticsearch的依赖 -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-data-elasticsearch </artifactId>
l</dependency>

配置

1
2
3
4
5
6
7
spring:
application:
name: search
data:
elasticsearch:
cluster-name: my-cluster
cluster-nodes: localhost:9300

也许,大家会疑惑,配置文件中明明写的端口是9200,为何这里配置文件中连接的时候写的端口是9300呢?

因为,配置9200是通过HTTP连接的端口,9300是TCP连接的端口

spring.data.elasticsearch.cluster-name:集群名称

spring.data.elasticsearch.cluster-nodes:集群节点地址列表,多个节点用英文逗号(,)分隔

创建ES文档和映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
@Data
@Document(indexName = "commodity")
public class Commodity implements Serializable {
@Id
private String skuId;
private String name;
private String category;
private Integer price;
private String brand;
private Integer stock;
}

这里定义了Commodity实例,表示商品。在Elasticsearch 6.X 版本中,不建议使用type,而且在7.X版本中将会彻底废弃type,所以此处我只指定了indexName,没有指定type。这里,一个Commodity代表一个商品,同时代表一条索引记录。

ElasticsearchRepository

Spring提供的ES的Repository接口为ElasticsearchCrudRepository,所以我们就可以直接定义额新的接口,然后实现ElasticsearchCrudRepository即可,ElasticsearchRepository也提供了基本的增删改查接口。

1
2
3
4
5
6
7
package com.cjs.example.dao;
import com.cjs.example.entity.Commodity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CommodityRepository extends ElasticsearchRepository<Commodity, String> {
}

接下来,定义service接口

1
2
3
4
5
6
7
8
9
10
11
12
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<Commodity> getAll();
List<Commodity> getByName(String name);
Page<Commodity> pageQuery(Integer pageNo, Integer pageSize, String kw);
}

实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
@Service
public class CommodityServiceImpl implements CommodityService {
@Autowired
private CommodityRepository commodityRepository;
@Override
public long count() {
return commodityRepository.count();
}
@Override
public Commodity save(Commodity commodity) {
return commodityRepository.save(commodity);
}
@Override
public void delete(Commodity commodity) {
commodityRepository.delete(commodity);
// commodityRepository.deleteById(commodity.getSkuId());
}
@Override
public Iterable<Commodity> getAll() {
return commodityRepository.findAll();
}
@Override
public List<Commodity> getByName(String name) {
List<Commodity> list = new ArrayList<>();
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", name);
Iterable<Commodity> iterable = commodityRepository.search(matchQueryBuilder);
iterable.forEach(e->list.add(e));
return list;
}
@Override
public Page<Commodity> pageQuery(Integer pageNo, Integer pageSize, String kw) {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchPhraseQuery("name", kw))
.withPageable(PageRequest.of(pageNo, pageSize))
.build();
return commodityRepository.search(searchQuery);
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
@SpringBootTest
public class CjsElasticsearchExampleApplicationTests {
@Autowired
private CommodityService commodityService;
@Test
public void contextLoads() {
System.out.println(commodityService.count());
}
@Test
public void testInsert() {
Commodity commodity = new Commodity();
commodity.setSkuId("1501009001");
commodity.setName("原味切片面包(10片装)");
commodity.setCategory("101");
commodity.setPrice(880);
commodity.setBrand("良品铺子");
commodityService.save(commodity);
commodity = new Commodity();
commodity.setSkuId("1501009002");
commodity.setName("原味切片面包(6片装)");
commodity.setCategory("101");
commodity.setPrice(680);
commodity.setBrand("良品铺子");
commodityService.save(commodity);
commodity = new Commodity();
commodity.setSkuId("1501009004");
commodity.setName("元气吐司850g");
commodity.setCategory("101");
commodity.setPrice(120);
commodity.setBrand("百草味");
commodityService.save(commodity);
}
@Test
public void testDelete() {
Commodity commodity = new Commodity();
commodity.setSkuId("1501009002");
commodityService.delete(commodity);
}
@Test
public void testGetAll() {
Iterable<Commodity> iterable = commodityService.getAll();
iterable.forEach(e->System.out.println(e.toString()));
}
@Test
public void testGetByName() {
List<Commodity> list = commodityService.getByName("面包");
System.out.println(list);
}
@Test
public void testPage() {
Page<Commodity> page = commodityService.pageQuery(0, 10, "切片");
System.out.println(page.getTotalPages());
System.out.println(page.getNumber());
System.out.println(page.getContent());
}
}

ElasticsearchTemplate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)
@SpringBootTest
public class ElasticsearchTemplateTest {
@Autowired
public ElasticsearchTemplate elasticsearchTemplate;
@Test
public void testInsert() {
Commodity commodity = new Commodity();
commodity.setSkuId("1501009005");
commodity.setName("葡萄吐司面包(10片装)");
commodity.setCategory("101");
commodity.setPrice(160);
commodity.setBrand("良品铺子");
IndexQuery indexQuery = new IndexQueryBuilder().withObject(commodity).build();
elasticsearchTemplate.index(indexQuery);
}
@Test
public void testQuery() {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("name", "吐司"))
.build();
List<Commodity> list = elasticsearchTemplate.queryForList(searchQuery, Commodity.class);
System.out.println(list);
}
}
文章目录
  1. 1. 安装ES
  2. 2. 依赖
  3. 3. 配置
  4. 4. 创建ES文档和映射
  5. 5. ElasticsearchRepository
  6. 6. ElasticsearchTemplate
|