0.版本选择
我这里选择了5.6.x,记得如果spring-boot-starter-parent是1.x可以选择2.x版本的elasticsearch,版本要对应,不然会有莫名其妙的问题
1.安装elasticsearch
https://www.elastic.co/downloads/past-releases
windows 测试的,解压就能用
解压,到bin目录,双击elasticsearch.bat
1.1安装elasticsearch-head
https://github.com/mobz/elasticsearch-head
elasticsearch-head是一个网页查看elasticsearch状态,操作的第三方东西
1
2
3
4
5
|
git clone git: //github.com/mobz/elasticsearch-head.git cd elasticsearch-head npm install npm run start open http: //localhost:9100/ |
要先安装node.js
config/elasticsearch.yml 文件增加
http.cors.enabled: true
http.cors.allow-origin: “*”
elasticsearch-head/gruntfile.js
1
2
3
4
5
6
7
8
9
10
|
connect: { server: { options: { hostname: '0.0.0.0' , port: 9100 , base: '.' , keepalive: true } } } |
8082改成你自己的端口
http://127.0.0.1:8082/swagger-ui.html#/
0是第一页
application.properties
1
2
3
4
|
#===es start=== spring.data.elasticsearch.repositories.enabled = true spring.data.elasticsearch.cluster-nodes = 127.0 . 0.1 : 9300 #===es end=== |
2.porm
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 2.1 . 1 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.example</groupid> <artifactid>demo</artifactid> <version> 0.0 . 1 -snapshot</version> <name>demo</name> <description>demo project for spring boot</description> <packaging>jar</packaging> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <!--swagger-ui api文档生产工具--> <dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger2</artifactid> <version> 2.6 . 1 </version> </dependency> <dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger-ui</artifactid> <version> 2.6 . 1 </version> </dependency> <!--swagger-ui api文档生产工具--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-elasticsearch</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
主要添加spring-boot-starter-data-elasticsearch,注意spring-boot-starter-parent的版本号
3. goodsinfo
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
|
package com.example.demo.domain; import org.springframework.data.elasticsearch.annotations.document; import java.io.serializable; @document (indexname = "testgoods" , type = "goods" ) public class goodsinfo implements serializable { private long id; private string name; private string description; public long getid() { return id; } public void setid( long id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } public string getdescription() { return description; } public void setdescription(string description) { this .description = description; } public goodsinfo( long id, string name, string description) { this .id = id; this .name = name; this .description = description; } public goodsinfo() { } } |
indexname 类似数据库名称,type类似表名字
4. goodsrepository
1
2
3
4
5
6
7
8
9
|
package com.example.demo.repository; import com.example.demo.domain.goodsinfo; import org.springframework.data.elasticsearch.repository.elasticsearchrepository; import org.springframework.stereotype.component; @component public interface goodsrepository extends elasticsearchrepository<goodsinfo, long > { } |
这里会帮你封装了很多了
5. helloworldcontroller
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package com.example.demo.controller; import com.example.demo.domain.goodsinfo; import com.example.demo.repository.goodsrepository; import io.swagger.annotations.api; import io.swagger.annotations.apiimplicitparam; import io.swagger.annotations.apioperation; import org.elasticsearch.index.query.querybuilder; import org.elasticsearch.index.query.querybuilders; import org.elasticsearch.index.query.functionscore.functionscorequerybuilder; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.domain.page; import org.springframework.data.domain.pagerequest; import org.springframework.data.domain.pageable; import org.springframework.data.elasticsearch.core.query.nativesearchquery; import org.springframework.data.elasticsearch.core.query.nativesearchquerybuilder; import org.springframework.data.elasticsearch.core.query.searchquery; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.*; import java.util.list; import java.util.optional; import static org.elasticsearch.index.query.querybuilders.querystringquery; @controller @api (value = "helloworldcontroller|一个用来测试swagger注解的控制器" ,tags = "helloworldcontroller" , description = "helloworldcontroller" ) public class helloworldcontroller { @autowired private goodsrepository goodsrepository; @responsebody @requestmapping (value = "/hello" , method = requestmethod.get) @apioperation (value = "根据用户编号获取用户姓名" , notes = "test: 仅1和2有正确返回" ) @apiimplicitparam (paramtype= "query" , name = "usernumber" , value = "用户编号" , required = true , datatype = "integer" ) public string index( @requestparam integer usernumber){ if (usernumber == 1 ){ return "小白" ; } else if (usernumber == 2 ){ return "小红" ; } else { return "未知" ; } } @responsebody @requestmapping (value = "/save" , method = requestmethod.post) @apioperation (value = "新增商品" ) public string save(){ goodsinfo goodsinfo = new goodsinfo(system.currenttimemillis(), "商品" + system.currenttimemillis(), "这是一个测试商品" ); goodsrepository.save(goodsinfo); return "success" ; } @responsebody @requestmapping (value = "/delete" , method = requestmethod.post) @apioperation (value = "删除商品" ) @apiimplicitparam (paramtype= "query" , name = "id" , value = "商品id" , required = true , datatype = "long" ) public string delete( @requestparam (required = true ) long id){ goodsrepository.deletebyid(id); return "success" ; } @responsebody @requestmapping (value = "/update" , method = requestmethod.post) @apioperation (value = "更新商品" ) @apiimplicitparam (paramtype= "query" , name = "id" , value = "商品id" , required = true , datatype = "long" ) public string update( @requestparam (required = true ) long id, @requestparam (required = false ) string name, @requestparam (required = false ) string description){ optional<goodsinfo> goodsinfo = goodsrepository.findbyid(id); if (goodsinfo.ispresent()){ if (name != null ){ goodsinfo.get().setname(name); } if (description != null ){ goodsinfo.get().setdescription(description); } goodsrepository.save(goodsinfo.get()); return "success" ; } else { return "no find" ; } } @responsebody @requestmapping (value = "/getone" , method = requestmethod.get) @apioperation (value = "得到一个商品" ) @apiimplicitparam (paramtype= "query" , name = "id" , value = "商品id" , required = true , datatype = "long" ) public optional<goodsinfo> getone( @requestparam (required = true ) long id){ optional<goodsinfo> goodsinfo = goodsrepository.findbyid(id); return goodsinfo; } private searchquery getentitysearchquery( int pagenumber, string searchcontent){ pageable pageable = pagerequest.of(pagenumber, 20 ); searchquery searchquery = new nativesearchquerybuilder().withquery(querystringquery(searchcontent)).withpageable(pageable).build(); return searchquery; } @responsebody @requestmapping (value = "/search" , method = requestmethod.get) @apioperation (value = "搜索商品" ) public list<goodsinfo> search( @requestparam (required = true ) integer pagenumber, @requestparam (required = true ) string query){ searchquery searchquery = getentitysearchquery(pagenumber, query); page<goodsinfo> goodspage = goodsrepository.search(searchquery); return goodspage.getcontent(); } } |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://www.waitingfy.com/archives/5256