Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jd_goods
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bigdata
jd_goods
Commits
0a654366
Commit
0a654366
authored
Jun 11, 2021
by
杨林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ES搜索工具
parent
e7393403
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
267 additions
and
0 deletions
+267
-0
src/main/scala/tools/es/ESUtils.scala
src/main/scala/tools/es/ESUtils.scala
+267
-0
No files found.
src/main/scala/tools/es/ESUtils.scala
0 → 100644
View file @
0a654366
package
tools.es
import
org.elasticsearch.action.delete.
{
DeleteRequest
,
DeleteResponse
}
import
org.elasticsearch.action.get.
{
GetRequest
,
GetResponse
}
import
org.elasticsearch.action.index.
{
IndexRequest
,
IndexResponse
}
import
org.elasticsearch.action.search.SearchRequest
import
org.elasticsearch.action.update.
{
UpdateRequest
,
UpdateResponse
}
import
org.elasticsearch.client.indices.GetIndexRequest
import
org.elasticsearch.client.
{
RequestOptions
,
RestHighLevelClient
}
import
org.elasticsearch.common.unit.
{
Fuzziness
,
TimeValue
}
import
org.elasticsearch.common.xcontent.XContentType
import
org.elasticsearch.index.query.QueryBuilders
import
org.elasticsearch.search.builder.SearchSourceBuilder
import
org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder
import
org.elasticsearch.search.sort.SortOrder
import
org.elasticsearch.search.
{
SearchHit
,
SearchHits
}
import
tools.properties.PropertiesTools
import
java.io.IOException
import
java.util.Properties
import
java.util.concurrent.TimeUnit
/**
* @ClassName: ESUtil
* @Description: ES的工具类,实现ES的增删查改
* @Create by: LinYang
* @Date: 2020/12/17 15:27
*/
object
ESUtils
{
private
[
this
]
val
sourceBuilder
=
new
SearchSourceBuilder
private
[
this
]
var
searchRequest
:
SearchRequest
=
_
private
[
this
]
val
client
:
RestHighLevelClient
=
ESClient
.
getClient
private
[
this
]
val
properties
:
Properties
=
PropertiesTools
.
getProperties
private
[
this
]
val
indexName
:
String
=
properties
.
getProperty
(
"es.indices"
)
//增
/**
* 添加数据到ES,指定索引名和id
*
* @param id id
* @param content 要添加的内容,格式为json
*/
def
addOne
(
id
:
String
,
content
:
String
)
:
String
=
{
val
indexRequest
=
new
IndexRequest
(
indexName
)
indexRequest
.
id
(
id
)
indexRequest
.
source
(
content
,
XContentType
.
JSON
)
var
response
:
IndexResponse
=
null
try
{
response
=
client
.
index
(
indexRequest
,
RequestOptions
.
DEFAULT
)
}
catch
{
case
e
:
IOException
=>
e
.
printStackTrace
()
}
response
.
getResult
.
toString
}
//删
/**
* 删除索引
*
* @return 是否删除成功
*/
def
deleteIndex
()
:
String
=
{
val
deleteRequest
=
new
DeleteRequest
(
indexName
)
val
response
=
client
.
delete
(
deleteRequest
,
RequestOptions
.
DEFAULT
)
response
.
getResult
.
toString
}
/**
* 删除指定的数据
*
* @param id 数据id
* @return 删除结果
*/
def
deleteDoc
(
id
:
String
)
:
String
=
{
val
request
=
new
DeleteRequest
(
indexName
,
id
)
val
response
:
DeleteResponse
=
client
.
delete
(
request
,
RequestOptions
.
DEFAULT
)
response
.
getResult
.
toString
}
//改
/**
* 修改指定文件的数据
*
* @param id 指定id
* @param content 修改的内容
* @return 返回修改结果
*/
def
updateIndex
(
id
:
String
,
content
:
String
,
routing
:
String
)
:
String
=
{
val
request
=
new
UpdateRequest
(
indexName
,
id
).
doc
(
content
,
XContentType
.
JSON
).
routing
(
routing
)
var
response
:
UpdateResponse
=
null
try
{
response
=
client
.
update
(
request
,
RequestOptions
.
DEFAULT
)
response
.
getResult
.
toString
}
catch
{
case
exception
:
Exception
=>
""
}
}
//查
/**
* 查询指定的索引是否存在
*
* @return 返回是否存在
*/
def
indexExists
()
:
Boolean
=
{
var
exits
:
Boolean
=
false
try
{
val
request
:
GetIndexRequest
=
new
GetIndexRequest
(
indexName
)
exits
=
client
.
indices
.
exists
(
request
,
RequestOptions
.
DEFAULT
)
}
catch
{
case
e
:
Exception
=>
e
.
printStackTrace
()
}
exits
}
/**
* 通过id获取一个文档
*
* @param id id
* @return 搜索结果
*/
def
getById
(
id
:
String
)
:
GetResponse
=
{
val
getRequest
=
new
GetRequest
(
indexName
,
id
)
client
.
get
(
getRequest
,
RequestOptions
.
DEFAULT
)
}
/**
* 无条件查询指定索引
*
* @param size 取出多少条数据
* @return 返回长度为size的搜索结果数组
*/
def
searchAll
(
size
:
Int
)
:
Array
[
SearchHit
]
=
{
if
(!
indexExists
)
{
return
null
}
searchRequest
=
new
SearchRequest
(
indexName
)
//构建查询
val
queryBuilder
=
QueryBuilders
.
matchAllQuery
()
//查询
sourceBuilder
.
query
(
queryBuilder
)
.
timeout
(
new
TimeValue
(
60
,
TimeUnit
.
SECONDS
))
.
size
(
size
)
searchRequest
.
source
(
sourceBuilder
)
//搜索
val
response
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
)
response
.
getHits
.
getHits
}
/**
* 根据字段精准匹配 term
*
* @param fieldName 字段名
* @param text 匹配的值
* @param size 取出的条数
* @return 返回搜索结果,长度为size的结果数组
*/
def
termMatching
(
fieldName
:
String
,
text
:
AnyRef
)
:
SearchHits
=
{
if
(!
indexExists
)
{
return
null
}
searchRequest
=
new
SearchRequest
(
indexName
)
val
termQueryBuilder
=
QueryBuilders
.
termQuery
(
fieldName
,
text
)
sourceBuilder
.
query
(
termQueryBuilder
)
searchRequest
.
source
(
sourceBuilder
)
val
response
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
)
response
.
getHits
}
/**
* 根据指定索引字段模糊匹配
*
* @param fieldName 字段名字
* @param text 匹配文本
* @param size 取出的长度
* @param sortField 排序字段
* @param sortOrder 排序方式
* 升序:SortOrder.ASC,
* 降序:SortOrder.DESC
* @return 返回size长度的结果数组
*/
def
fuzzyMatching
(
fieldName
:
String
,
text
:
AnyRef
,
size
:
Int
,
sortField
:
String
,
sortOrder
:
SortOrder
)
:
Array
[
SearchHit
]
=
{
if
(!
indexExists
)
{
return
null
}
searchRequest
=
new
SearchRequest
(
indexName
)
val
matchQueryBuilder
=
QueryBuilders
.
matchQuery
(
fieldName
,
text
)
.
fuzziness
(
Fuzziness
.
AUTO
)
.
prefixLength
(
3
)
.
maxExpansions
(
10
)
sourceBuilder
.
query
(
matchQueryBuilder
)
.
timeout
(
new
TimeValue
(
60
,
TimeUnit
.
SECONDS
))
.
size
(
size
)
searchRequest
.
source
(
sourceBuilder
)
val
response
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
)
response
.
getHits
.
getHits
}
/**
* 根据指定的多字段进行模糊匹配
*
* @param size 取出的长度
* @param text 匹配的文本
* @param fieldNames 字段名
* @return 返回结果的数组
*/
def
multiFuzzyMatching
(
size
:
Int
,
text
:
Any
,
fieldNames
:
String*
)
:
SearchHits
=
{
// 多字段匹配
if
(!
indexExists
)
{
return
null
}
searchRequest
=
new
SearchRequest
(
indexName
)
val
multiMatchQueryBuilder
=
QueryBuilders
.
multiMatchQuery
(
text
,
fieldNames
:
_
*
)
.
fuzziness
(
Fuzziness
.
AUTO
)
.
maxExpansions
(
10
)
.
prefixLength
(
3
)
sourceBuilder
.
query
(
multiMatchQueryBuilder
).
size
(
size
)
searchRequest
.
source
(
sourceBuilder
)
val
response
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
)
response
.
getHits
}
//复合查询
def
boolQuery
(
source
:
String
,
name
:
String
,
price
:
Double
,
value
:
Any
,
size
:
Int
)
:
SearchHits
=
{
searchRequest
=
new
SearchRequest
(
indexName
)
val
boolQueryBuilder
=
QueryBuilders
.
boolQuery
//精准查询source字段的值,filedName写‘source’,source写‘淘宝’、‘拼多多’……
.
must
(
QueryBuilders
.
matchQuery
(
"source"
,
source
))
//
.
must
(
QueryBuilders
.
rangeQuery
(
"price"
).
gte
(
price
*
0.8
))
//匹配item_name字段的值
.
should
(
QueryBuilders
.
matchQuery
(
name
,
value
))
sourceBuilder
.
query
(
boolQueryBuilder
).
size
(
size
)
searchRequest
.
source
(
sourceBuilder
)
val
highlightBuilder
=
new
HighlightBuilder
highlightBuilder
.
field
(
"item_name"
,
10
)
.
preTags
(
"<"
)
.
postTags
(
">"
)
sourceBuilder
.
highlighter
(
highlightBuilder
)
val
response
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
)
response
.
getHits
}
}
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