Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
elasticsearch-demo
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
北京-王志杰
elasticsearch-demo
Commits
f8c5fa8c
Commit
f8c5fa8c
authored
Dec 29, 2020
by
cloudgyb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:增加首页
parent
6dda3746
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
146 additions
and
9 deletions
+146
-9
README.md
README.md
+3
-2
BookController.java
...com/gyb/elasticsearch/demo/controller/BookController.java
+14
-5
ESBook.java
...ain/java/com/gyb/elasticsearch/demo/entity/es/ESBook.java
+2
-2
BookService.java
.../java/com/gyb/elasticsearch/demo/service/BookService.java
+3
-0
index.html
src/main/resources/static/index.html
+69
-0
search.html
src/main/resources/static/search.html
+55
-0
No files found.
README.md
View file @
f8c5fa8c
...
@@ -42,4 +42,5 @@ create table t_book
...
@@ -42,4 +42,5 @@ create table t_book
4.
启动项目
4.
启动项目
```
shell
```
shell
./mvnw spring-boot:run
./mvnw spring-boot:run
```
```
\ No newline at end of file
5.
访问http://localhost:8080 进入首页进行书籍信息的添加和搜索
\ No newline at end of file
src/main/java/com/gyb/elasticsearch/demo/controller/BookController.java
View file @
f8c5fa8c
...
@@ -4,6 +4,7 @@ import com.gyb.elasticsearch.demo.entity.db.Book;
...
@@ -4,6 +4,7 @@ import com.gyb.elasticsearch.demo.entity.db.Book;
import
com.gyb.elasticsearch.demo.entity.es.ESBook
;
import
com.gyb.elasticsearch.demo.entity.es.ESBook
;
import
com.gyb.elasticsearch.demo.service.BookService
;
import
com.gyb.elasticsearch.demo.service.BookService
;
import
org.springframework.data.elasticsearch.core.SearchHits
;
import
org.springframework.data.elasticsearch.core.SearchHits
;
import
org.springframework.util.StopWatch
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestBody
;
...
@@ -26,16 +27,24 @@ public class BookController {
...
@@ -26,16 +27,24 @@ public class BookController {
}
}
@PostMapping
(
"/book"
)
@PostMapping
(
"/book"
)
public
Map
<
String
,
String
>
addBook
(
@RequestBody
Book
book
)
{
public
Map
<
String
,
String
>
addBook
(
@RequestBody
Book
book
)
{
bookService
.
addBook
(
book
);
bookService
.
addBook
(
book
);
Map
<
String
,
String
>
map
=
new
HashMap
<>();
Map
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
"msg"
,
"ok"
);
map
.
put
(
"msg"
,
"ok"
);
return
map
;
return
map
;
}
}
@GetMapping
(
"/book/search"
)
@GetMapping
(
"/book/search"
)
public
SearchHits
<
ESBook
>
search
(
String
key
){
public
Map
<
String
,
Object
>
search
(
String
key
)
{
return
bookService
.
searchBook1
(
key
);
final
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
final
StopWatch
stopWatch
=
new
StopWatch
();
stopWatch
.
start
();
final
SearchHits
<
ESBook
>
searchHits
=
bookService
.
searchBook1
(
key
);
stopWatch
.
stop
();
map
.
put
(
"res"
,
searchHits
);
final
double
totalTimeSeconds
=
stopWatch
.
getTotalTimeSeconds
();
map
.
put
(
"elapsedTime"
,
totalTimeSeconds
);
return
map
;
}
}
}
}
src/main/java/com/gyb/elasticsearch/demo/entity/es/ESBook.java
View file @
f8c5fa8c
...
@@ -19,9 +19,9 @@ public class ESBook {
...
@@ -19,9 +19,9 @@ public class ESBook {
@Id
@Id
@Field
(
type
=
FieldType
.
Text
)
@Field
(
type
=
FieldType
.
Text
)
private
String
id
;
private
String
id
;
@Field
(
analyzer
=
"ik_max_word"
)
@Field
(
type
=
FieldType
.
Text
,
analyzer
=
"ik_max_word"
)
private
String
title
;
private
String
title
;
@Field
(
analyzer
=
"ik_max_word"
)
@Field
(
type
=
FieldType
.
Text
,
analyzer
=
"ik_max_word"
)
private
String
author
;
private
String
author
;
@Field
(
type
=
FieldType
.
Double
)
@Field
(
type
=
FieldType
.
Double
)
private
Double
price
;
private
Double
price
;
...
...
src/main/java/com/gyb/elasticsearch/demo/service/BookService.java
View file @
f8c5fa8c
...
@@ -10,6 +10,7 @@ import org.springframework.data.elasticsearch.core.SearchHits;
...
@@ -10,6 +10,7 @@ import org.springframework.data.elasticsearch.core.SearchHits;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.support.TransactionTemplate
;
import
org.springframework.transaction.support.TransactionTemplate
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -32,6 +33,8 @@ public class BookService {
...
@@ -32,6 +33,8 @@ public class BookService {
}
}
public
void
addBook
(
Book
book
)
{
public
void
addBook
(
Book
book
)
{
book
.
setCreateTime
(
new
Date
());
book
.
setUpdateTime
(
new
Date
());
final
Book
saveBook
=
transactionTemplate
.
execute
((
status
)
->
final
Book
saveBook
=
transactionTemplate
.
execute
((
status
)
->
bookRepository
.
save
(
book
)
bookRepository
.
save
(
book
)
);
);
...
...
src/main/resources/static/index.html
0 → 100644
View file @
f8c5fa8c
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Elasticsearch Demo
</title>
<style>
input
{
height
:
30px
;
width
:
100%
;
margin-bottom
:
10px
;
}
</style>
</head>
<body>
<div
style=
"width: 400px;height: 300px;
margin: 0 auto;padding: 20px;box-shadow:#ccc 0 0 10px"
>
<h2>
添加书籍信息
</h2>
<label
for=
"t"
></label>
<input
id=
"t"
name=
"title"
type=
"text"
placeholder=
"书名"
required
/>
<label
for=
"a"
></label>
<input
id=
"a"
name=
"author"
type=
"text"
placeholder=
"作者"
required
/>
<label
for=
"p"
></label>
<input
id=
"p"
name=
"price"
type=
"text"
placeholder=
"价格"
required
/>
<button
onclick=
"addBook()"
style=
"width: 100%;height: 35px"
>
添加
</button>
</div>
<div
style=
"width: 600px;
margin: 0 auto;padding: 20px;"
>
<button
onclick=
"goSearch()"
style=
"text-align: center;
width: 100%;height: 50px;background-color: #2498f1;
border: none;color: aliceblue"
>
搜索书籍
</button>
</div>
</body>
<script>
function
goSearch
(){
window
.
open
(
"/search.html"
,
"_blank"
)
}
function
addBook
()
{
let
data
=
{
"title"
:
document
.
getElementById
(
"t"
).
value
,
"author"
:
document
.
getElementById
(
"a"
).
value
,
"price"
:
document
.
getElementById
(
"p"
).
value
}
if
(
data
.
title
.
trim
()
===
''
||
data
.
author
.
trim
()
===
''
||
data
.
price
.
trim
()
===
''
){
alert
(
"请补全信息再提交!"
)
return
;
}
if
(
isNaN
(
parseFloat
(
data
.
price
.
trim
()))){
alert
(
"价格不合法!"
)
return
;
}
let
request
=
new
XMLHttpRequest
();
request
.
open
(
"post"
,
"/book"
,
true
);
request
.
setRequestHeader
(
"Content-Type"
,
"application/json"
)
request
.
send
(
JSON
.
stringify
(
data
));
request
.
onload
=
function
()
{
if
(
this
.
status
===
200
){
alert
(
"添加成功!"
)
}
else
{
alert
(
"添加失败!"
)
}
}
request
.
onerror
=
function
(){
alert
(
"请求失败!"
)
}
}
</script>
</html>
\ No newline at end of file
src/main/resources/static/search.html
0 → 100644
View file @
f8c5fa8c
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
搜索书籍
</title>
</head>
<body>
<div
style=
"width: 600px;
margin: 0 auto;padding: 20px;"
>
<label
for=
"key"
></label>
<input
id=
"key"
onkeypress=
"search(event)"
placeholder=
"输入关键字,回车"
style=
"text-align: center;
width: 100%;height: 50px;outline: none;
border: #2498f1 1px solid;border-radius: 10px;font-size: 30px"
>
<div
style=
"margin-top: 50px"
>
<label
for=
"res"
>
搜索结果:
</label>
<span
id=
"time"
style=
"float: right"
></span>
<textarea
disabled
id=
"res"
name=
""
rows=
"30"
style=
"width: 100%;border: 1px #ccc solid;outline: none"
></textarea>
</div>
</div>
<script>
function
search
(
e
)
{
if
(
e
.
keyCode
===
13
)
{
searchBook
(
document
.
getElementById
(
"key"
).
value
)
}
console
.
log
(
e
)
}
function
searchBook
(
key
)
{
if
(
key
===
undefined
||
key
.
trim
()
===
''
)
{
document
.
getElementById
(
"res"
).
value
=
''
document
.
getElementById
(
"time"
).
innerHTML
=
''
return
;
}
let
request
=
new
XMLHttpRequest
();
request
.
open
(
"get"
,
"/book/search?key="
+
key
,
true
);
request
.
send
();
request
.
onload
=
function
()
{
if
(
this
.
status
===
200
)
{
let
parse
=
JSON
.
parse
(
this
.
responseText
);
document
.
getElementById
(
"res"
).
value
=
this
.
response
document
.
getElementById
(
"time"
).
innerHTML
=
'耗时:'
+
parse
.
elapsedTime
+
's'
}
else
{
alert
(
"搜索失败!"
)
}
}
request
.
onerror
=
function
()
{
alert
(
"请求失败!"
)
}
}
</script>
</body>
</html>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment