SpringBoot操作elasticsearch

注意: elasticsearch版本一定要和SpringBoot版本相对应。

这里使用的是:elasticsearch : 6.5.3, Springboot: 2.1.0

下载Elasticsearch:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.3.zip

监控Elasticsearch中的值:https://artifacts.elastic.co/downloads/kibana/kibana-6.5.3-windows-x86_64.zip

  1. 启动elasticsearch

    1
    elasticsearch-6.5.3/bin/ealsticsearch.batelasticsearch-6.5.3/bin/ealsticsearch.bat
  2. 启动监控

    1
    kibana-6.5.3-windows-x86_64/bin/kibana.bat

启动成功后访问:127.0.0.1:5601

1. 添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>

2. 添加配置信息

1
2
3
4
5
6
7
server:
port: 8080
spring:
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300
cluster-name: elasticsearch

3. 标记bean

1
2
3
4
5
6
7
8
// index索引就相当于数据库,type就相当于表
@Document(indexName = "pibigstar",type = "user")
public class ElasticUser {
private Long id;
private String username;
private String password;
//setter,getter方法
}

4. dao层

1
2
3
4
5
import com.pibgstar.demo.elastic.bean.ElasticUser;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ElasticUserDao extends ElasticsearchRepository<ElasticUser,Long> {
}

5. Controller层

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
68
69
70
71
72
73
74
75
76
@RestController
@RequestMapping("/user")
public class ElasticUserController {
private long sum = 1;

@Autowired
private ElasticUserDao userDao;
// 获取全部
@GetMapping("/list")
public List<ElasticUser> list(){
Iterable<ElasticUser> all = userDao.findAll();
Iterator<ElasticUser> iterator = all.iterator();
List<ElasticUser> list=new ArrayList<>();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
// 添加
@RequestMapping("/add")
public ElasticUser add(ElasticUser user) {
user.setId(sum++);
ElasticUser save = userDao.save(user);
return save;
}
// 根据id获取
@GetMapping("/get")
public ElasticUser get(Long id){
Optional<ElasticUser> byId = userDao.findById(id);
return byId.get();
}
// 删除
@RequestMapping("/delete")
public void delete(ElasticUser user){
userDao.delete(user);
}
// 更新
@RequestMapping("/update")
public ElasticUser update(ElasticUser user) {
ElasticUser save = userDao.save(user);
return save;
}
// 搜索
@GetMapping("/query")
public List<ElasticUser> query(@RequestParam(name = "value") String value){
// 当id、username、password字段的值等于value时可查出数据(里面的值要有value,不然会报错)
// QueryStringQueryBuilder builder = new QueryStringQueryBuilder(value);

// 搜索 id、username字段的值等于value
// QueryBuilder builder = QueryBuilders.multiMatchQuery(value,"id","username");

// 模糊查询,username字段中的值含有value
QueryBuilder builder1 = QueryBuilders.wildcardQuery("username","*"+value+"*");
// password字段等于1
QueryBuilder builder2 = QueryBuilders.wildcardQuery("password","*456*");

// 复合查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// and 结果中 username含有value 并且 password = 456
// boolQueryBuilder.must(builder1);
// boolQueryBuilder.must(builder2);

// or 结果中 username含有value 或 password = 456
boolQueryBuilder.should(builder1);
boolQueryBuilder.should(builder2);

System.out.println("查询的语句:"+boolQueryBuilder);
Iterable<ElasticUser> searchResult = userDao.search(boolQueryBuilder);
Iterator<ElasticUser> iterator = searchResult.iterator();
List<ElasticUser> list=new ArrayList<>();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
}
-------------本文结束感谢您的阅读-------------