Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
76f15fb0
Commit
76f15fb0
authored
Jul 29, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
es 搜索 添加多关键词支持
parent
254617c7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
6 deletions
+12
-6
ESService.java
...main/java/com/tanpu/community/service/base/ESService.java
+11
-5
application-dev.yml
community-service/src/main/resources/application-dev.yml
+1
-1
No files found.
community-service/src/main/java/com/tanpu/community/service/base/ESService.java
View file @
76f15fb0
...
@@ -8,6 +8,7 @@ import com.tanpu.community.api.beans.qo.ThemeQo;
...
@@ -8,6 +8,7 @@ import com.tanpu.community.api.beans.qo.ThemeQo;
import
com.tanpu.community.dao.entity.community.ThemeEntity
;
import
com.tanpu.community.dao.entity.community.ThemeEntity
;
import
com.tanpu.community.model.ESWrapper
;
import
com.tanpu.community.model.ESWrapper
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.elasticsearch.index.query.BoolQueryBuilder
;
import
org.elasticsearch.index.query.BoolQueryBuilder
;
import
org.elasticsearch.index.query.MatchPhraseQueryBuilder
;
import
org.elasticsearch.index.query.MatchPhraseQueryBuilder
;
import
org.elasticsearch.index.query.MatchQueryBuilder
;
import
org.elasticsearch.index.query.MatchQueryBuilder
;
...
@@ -45,12 +46,17 @@ public class ESService {
...
@@ -45,12 +46,17 @@ public class ESService {
SearchSourceBuilder
search
=
new
SearchSourceBuilder
();
SearchSourceBuilder
search
=
new
SearchSourceBuilder
();
BoolQueryBuilder
boolQb
=
QueryBuilders
.
boolQuery
();
BoolQueryBuilder
boolQb
=
QueryBuilders
.
boolQuery
();
MatchPhraseQueryBuilder
contentQb
=
QueryBuilders
.
matchPhraseQuery
(
"textContent"
,
keyword
);
MatchPhraseQueryBuilder
titleQb
=
QueryBuilders
.
matchPhraseQuery
(
"title"
,
keyword
);
boolQb
.
should
(
contentQb
);
boolQb
.
should
(
titleQb
);
String
[]
includes
=
new
String
[]{
"id"
,
"themeId"
,
"createTime"
};
// 如果关键词带空格,则拆分
String
[]
ks
=
StringUtils
.
split
(
keyword
,
' '
);
for
(
String
k
:
ks
)
{
MatchPhraseQueryBuilder
contentQb
=
QueryBuilders
.
matchPhraseQuery
(
"textContent"
,
k
);
MatchPhraseQueryBuilder
titleQb
=
QueryBuilders
.
matchPhraseQuery
(
"title"
,
k
);
boolQb
.
should
(
contentQb
);
boolQb
.
should
(
titleQb
);
}
String
[]
includes
=
new
String
[]{
"id"
,
"themeId"
,
"createTime"
,
"_score"
};
String
[]
excludes
=
new
String
[]{};
String
[]
excludes
=
new
String
[]{};
search
.
query
(
boolQb
).
fetchSource
(
includes
,
excludes
).
sort
(
"createTime"
,
SortOrder
.
DESC
).
from
(
from
).
size
(
size
);
search
.
query
(
boolQb
).
fetchSource
(
includes
,
excludes
).
sort
(
"createTime"
,
SortOrder
.
DESC
).
from
(
from
).
size
(
size
);
search
.
query
(
boolQb
).
sort
(
"createTime"
,
SortOrder
.
DESC
).
from
(
from
).
size
(
size
);
search
.
query
(
boolQb
).
sort
(
"createTime"
,
SortOrder
.
DESC
).
from
(
from
).
size
(
size
);
...
...
community-service/src/main/resources/application-dev.yml
View file @
76f15fb0
...
@@ -79,7 +79,7 @@ es:
...
@@ -79,7 +79,7 @@ es:
port
:
9200
port
:
9200
userName
:
1
userName
:
1
userPasswd
:
2
userPasswd
:
2
index
:
dev
index
:
new-community
tencent
:
tencent
:
cloud
:
cloud
:
...
...
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