Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
elasticsearch-demo
提交
df491981
E
elasticsearch-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
elasticsearch-demo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
elasticsearch-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
df491981
编写于
5月 13, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:搜索java api
上级
07d36e09
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
743 addition
and
0 deletion
+743
-0
src/test/java/es_03_search/TestSearch_01_all.java
src/test/java/es_03_search/TestSearch_01_all.java
+71
-0
src/test/java/es_03_search/TestSearch_02_page.java
src/test/java/es_03_search/TestSearch_02_page.java
+78
-0
src/test/java/es_03_search/TestSearch_03_ids.java
src/test/java/es_03_search/TestSearch_03_ids.java
+70
-0
src/test/java/es_03_search/TestSearch_04_match.java
src/test/java/es_03_search/TestSearch_04_match.java
+70
-0
src/test/java/es_03_search/TestSearch_05_term.java
src/test/java/es_03_search/TestSearch_05_term.java
+71
-0
src/test/java/es_03_search/TestSearch_06_multi_match.java
src/test/java/es_03_search/TestSearch_06_multi_match.java
+71
-0
src/test/java/es_03_search/TestSearch_07_bool.java
src/test/java/es_03_search/TestSearch_07_bool.java
+94
-0
src/test/java/es_03_search/TestSearch_08_filter.java
src/test/java/es_03_search/TestSearch_08_filter.java
+104
-0
src/test/java/es_03_search/TestSearch_09_sort.java
src/test/java/es_03_search/TestSearch_09_sort.java
+114
-0
未找到文件。
src/test/java/es_03_search/TestSearch_01_all.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* 搜索全部记录
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_01_all
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchAll
()
throws
IOException
{
// GET book/_search
// {
// "query": {
// "match_all": {}
// }
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
QueryBuilders
.
matchAllQuery
());
//获取某些字段
searchSourceBuilder
.
fetchSource
(
new
String
[]{
"name"
},
new
String
[]{});
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"score:"
+
score
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_02_page.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* 分页搜索
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_02_page
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchPage
()
throws
IOException
{
// GET book/_search
// {
// "query": {
// "match_all": {}
// },
// "from": 0,
// "size": 2
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
QueryBuilders
.
matchAllQuery
());
//第几页
int
page
=
1
;
//每页几个
int
size
=
2
;
//下标计算
int
from
=
(
page
-
1
)
*
size
;
searchSourceBuilder
.
from
(
from
);
searchSourceBuilder
.
size
(
size
);
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_03_ids.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* ids搜索
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_03_ids
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchIds
()
throws
IOException
{
// GET /book/_search
// {
// "query": {
// "ids" : {
// "values" : ["1", "4", "100"]
// }
// }
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
QueryBuilders
.
idsQuery
().
addIds
(
"1"
,
"4"
,
"100"
));
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_04_match.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* match搜索
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_04_match
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchMatch
()
throws
IOException
{
// GET /book/_search
// {
// "query": {
// "match": {
// "description": "java程序员"
// }
// }
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
QueryBuilders
.
matchQuery
(
"description"
,
"java程序员"
));
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_05_term.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* term 搜索 精确值
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_05_term
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchTerm
()
throws
IOException
{
//
// GET /book/_search
// {
// "query": {
// "term": {
// "description": "java程序员"
// }
// }
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
QueryBuilders
.
termQuery
(
"description"
,
"java程序员"
));
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_06_multi_match.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* multi_match搜索 多字段匹配
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_06_multi_match
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchMultiMatch
()
throws
IOException
{
// GET /book/_search
// {
// "query": {
// "multi_match": {
// "query": "java程序员",
// "fields": ["name", "description"]
// }
// }
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
searchSourceBuilder
.
query
(
QueryBuilders
.
multiMatchQuery
(
"java程序员"
,
"name"
,
"description"
));
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_07_bool.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.BoolQueryBuilder
;
import
org.elasticsearch.index.query.MatchQueryBuilder
;
import
org.elasticsearch.index.query.MultiMatchQueryBuilder
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* bool搜索
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_07_bool
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchBool
()
throws
IOException
{
// GET /book/_search
// {
// "query": {
// "bool": {
// "must": [
// {
// "multi_match": {
// "query": "java程序员",
// "fields": ["name","description"]
// }
// }
// ],
// "should": [
// {
// "match": {
// "studymodel": "201001"
// }
// }
// ]
// }
// }
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
//构建multiMatch请求
MultiMatchQueryBuilder
multiMatchQueryBuilder
=
QueryBuilders
.
multiMatchQuery
(
"java程序员"
,
"name"
,
"description"
);
//构建match请求
MatchQueryBuilder
matchQueryBuilder
=
QueryBuilders
.
matchQuery
(
"studymodel"
,
"201001"
);
BoolQueryBuilder
boolQueryBuilder
=
QueryBuilders
.
boolQuery
();
boolQueryBuilder
.
must
(
multiMatchQueryBuilder
);
boolQueryBuilder
.
should
(
matchQueryBuilder
);
searchSourceBuilder
.
query
(
boolQueryBuilder
);
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_08_filter.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.BoolQueryBuilder
;
import
org.elasticsearch.index.query.MatchQueryBuilder
;
import
org.elasticsearch.index.query.MultiMatchQueryBuilder
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* filter搜索
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_08_filter
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchFilter
()
throws
IOException
{
// GET /book/_search
// {
// "query": {
// "bool": {
// "must": [
// {
// "multi_match": {
// "query": "java程序员",
// "fields": ["name","description"]
// }
// }
// ],
// "should": [
// {
// "match": {
// "studymodel": "201001"
// }
// }
// ],
// "filter": {
// "range": {
// "price": {
// "gte": 50,
// "lte": 90
// }
// }
//
// }
// }
// }
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
//构建multiMatch请求
MultiMatchQueryBuilder
multiMatchQueryBuilder
=
QueryBuilders
.
multiMatchQuery
(
"java程序员"
,
"name"
,
"description"
);
//构建match请求
MatchQueryBuilder
matchQueryBuilder
=
QueryBuilders
.
matchQuery
(
"studymodel"
,
"201001"
);
BoolQueryBuilder
boolQueryBuilder
=
QueryBuilders
.
boolQuery
();
boolQueryBuilder
.
must
(
multiMatchQueryBuilder
);
boolQueryBuilder
.
should
(
matchQueryBuilder
);
boolQueryBuilder
.
filter
(
QueryBuilders
.
rangeQuery
(
"price"
).
gte
(
50
).
lte
(
90
));
searchSourceBuilder
.
query
(
boolQueryBuilder
);
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
src/test/java/es_03_search/TestSearch_09_sort.java
0 → 100644
浏览文件 @
df491981
package
es_03_search
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.index.query.BoolQueryBuilder
;
import
org.elasticsearch.index.query.MatchQueryBuilder
;
import
org.elasticsearch.index.query.MultiMatchQueryBuilder
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
org.elasticsearch.search.sort.SortOrder
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* sort排序搜索
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 18:01
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestSearch_09_sort
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testSearchSort
()
throws
IOException
{
// GET /book/_search
// {
// "query": {
// "bool": {
// "must": [
// {
// "multi_match": {
// "query": "java程序员",
// "fields": ["name","description"]
// }
// }
// ],
// "should": [
// {
// "match": {
// "studymodel": "201001"
// }
// }
// ],
// "filter": {
// "range": {
// "price": {
// "gte": 50,
// "lte": 90
// }
// }
//
// }
// }
// },
// "sort": [
// {
// "price": {
// "order": "asc"
// }
// }
// ]
// }
//1构建搜索请求
SearchRequest
searchRequest
=
new
SearchRequest
(
"book"
);
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
();
//构建multiMatch请求
MultiMatchQueryBuilder
multiMatchQueryBuilder
=
QueryBuilders
.
multiMatchQuery
(
"java程序员"
,
"name"
,
"description"
);
//构建match请求
MatchQueryBuilder
matchQueryBuilder
=
QueryBuilders
.
matchQuery
(
"studymodel"
,
"201001"
);
BoolQueryBuilder
boolQueryBuilder
=
QueryBuilders
.
boolQuery
();
boolQueryBuilder
.
must
(
multiMatchQueryBuilder
);
boolQueryBuilder
.
should
(
matchQueryBuilder
);
boolQueryBuilder
.
filter
(
QueryBuilders
.
rangeQuery
(
"price"
).
gte
(
50
).
lte
(
90
));
searchSourceBuilder
.
query
(
boolQueryBuilder
);
//按照价格升序
searchSourceBuilder
.
sort
(
"price"
,
SortOrder
.
ASC
);
searchRequest
.
source
(
searchSourceBuilder
);
//2执行搜索
SearchResponse
searchResponse
=
client
.
search
(
searchRequest
,
RequestOptions
.
DEFAULT
);
//3获取结果
SearchHits
hits
=
searchResponse
.
getHits
();
//数据数据
SearchHit
[]
searchHits
=
hits
.
getHits
();
System
.
out
.
println
(
"--------------------------"
);
for
(
SearchHit
hit
:
searchHits
)
{
String
id
=
hit
.
getId
();
float
score
=
hit
.
getScore
();
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
String
name
=
(
String
)
sourceAsMap
.
get
(
"name"
);
String
description
=
(
String
)
sourceAsMap
.
get
(
"description"
);
Double
price
=
(
Double
)
sourceAsMap
.
get
(
"price"
);
System
.
out
.
println
(
"id:"
+
id
);
System
.
out
.
println
(
"name:"
+
name
);
System
.
out
.
println
(
"description:"
+
description
);
System
.
out
.
println
(
"price:"
+
price
);
System
.
out
.
println
(
"=========================="
);
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录