A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

【郑州校区】学成在线-第11天-讲义-搜索服务 五

4.4 Service
Service方法代码复杂,这里分三步完成。
4.4.1 按关键字搜索
1)在appliction.yml中配置source_fifield
[AppleScript] 纯文本查看 复制代码
elasticsearch:
hostlist: 127.0.0.1:9200 #多个结点中间用逗号分隔
course:
index: xc_course
type: doc
source_field:
id,name,grade,mt,st,charge,valid,pic,qq,price,price_old,status,studymodel,teachmode,expires,pub_
time,start_time,end_time 

2service完整代码如下

[AppleScript] 纯文本查看 复制代码
@Service
public class EsCourseService {
private static final Logger LOGGER = LoggerFactory
.
getLogger(EsCourseService.class);
@Value(
"
${xuecheng
.elasticsearch.course.index}
"
)
private String es_index;
@Value(
"
${xuecheng
.elasticsearch.course.type}
"
)
private String es_type;
@Value(
"
${xuecheng
.elasticsearch.course.source_field}
"
)
private String source_field;
@Autowired
RestHighLevelClient restHighLevelClient;
public QueryResponseResult<CoursePub> list(int page,int size,CourseSearchParam
courseSearchParam) {
//设置索引
SearchRequest searchRequest
= new SearchRequest(es_index);
//设置类型
searchRequest.types(es_type);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder =
QueryBuilders.boolQuery();
//source源字段过虑
String[] source_fields
=
source_field.split(
"
,
"
);
searchSourceBuilder.fetchSource(source_fields, new String[]{});
//关键字
if(StringUtils.isNotEmpty(courseSearchParam.
getKeyword())){
//匹配关键字
MultiMatchQueryBuilder multiMatchQueryBuilder =
QueryBuilders.multiMatchQuery(courseSearchParam.
getKeyword(),
"
name
"
,
"
teachplan
"
,
"
description
"
);
//设置匹配占比
multiMatchQueryBuilder.minimumShouldMatch(
"
70%
"
);
//提升另个字段的Boost值
multiMatchQueryBuilder.field(
"
name
"
,10);
boolQueryBuilder.must(multiMatchQueryBuilder);
}
//布尔查询
searchSourceBuilder.
query(boolQueryBuilder);
//请求搜索
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse
= null;
try {
searchResponse
= restHighLevelClient.search(searchRequest);
} catch (IOException e) {
e.
printStackTrace();
LOGGER.error(
"
xuecheng search error..
{}
"
,e.
getMessage());
return new QueryResponseResult(CommonCode.SUCCESS,new QueryResult<CoursePub>());
}
//结果集处理
SearchHits hits
=
searchResponse.
getHits();
SearchHit[] searchHits
= hits.
getHits();
//记录总数
long totalHits
= hits.
getTotalHits();
//数据列表
List<CoursePub> list
= new ArrayList<>();
for (SearchHit hit : searchHits) {
CoursePub coursePub
= new CoursePub();
//取出source
Map<String, Object> sourceAsMap
= hit.
getSourceAsMap();
//取出名称
String name
=
(String) sourceAsMap
.
get(
"
name
"
);
coursePub.setName(name);
//图片
String pic
=
(String) sourceAsMap
.
get(
"
pic
"
);
coursePub.setPic(pic);
//价格
Float price
= null;
try {
if(sourceAsMap
.
get(
"
price
"
)!
=null ){
price
= Float.
parseFloat((String) sourceAsMap
.
get(
"
price
"
));
}
} catch (Exception e) {
e.
printStackTrace();
}
coursePub.setPrice(price);
Float price_old
= null;
try {
if(sourceAsMap
.
get(
"
price_old
"
)!
=null ){
price_old
= Float.
parseFloat((String) sourceAsMap
.
get(
"
price_old
"
));
}
} catch (Exception e) {
e.
printStackTrace();
}
coursePub.setPrice_old(price_old);
list.add(coursePub);
}
QueryResult<CoursePub> queryResult
= new QueryResult<>();
queryResult.setList(list);
queryResult.setTotal(totalHits);
QueryResponseResult<CoursePub> coursePubQueryResponseResult
= new
QueryResponseResult<CoursePub>(CommonCode.SUCCESS,queryResult);
return coursePubQueryResponseResult;
}
}

4.4.2 按分类和难度等级搜索
按分类和难度等级搜索代码如下:
[AppleScript] 纯文本查看 复制代码
 @Service
public class EsCourseService {
private static final Logger LOGGER = LoggerFactory
.
getLogger(EsCourseService.class);
@Value(
"
${xuecheng
.elasticsearch.course.index}
"
)
private String es_index;
@Value(
"
${xuecheng
.elasticsearch.course.type}
"
)
private String es_type;
@Value(
"
${xuecheng
.elasticsearch.course.source_field}
"
)
private String source_field;
@Autowired
RestHighLevelClient restHighLevelClient;
public QueryResponseResult<CoursePub> list(int page,int size,CourseSearchParam
courseSearchParam) {
//设置索引
SearchRequest searchRequest
= new SearchRequest(es_index);
//设置类型
searchRequest.types(es_type);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder =
QueryBuilders.boolQuery();
//source源字段过虑
String[] source_fields
=
source_field.split(
"
,
"
);
searchSourceBuilder.fetchSource(source_fields, new String[]{});
//关键字
if(StringUtils.isNotEmpty(courseSearchParam.
getKeyword())){
//匹配关键字
MultiMatchQueryBuilder multiMatchQueryBuilder =
QueryBuilders.multiMatchQuery(courseSearchParam.
getKeyword(),
"
name
"
,
"
teachplan
"
,
"
description
"
);
//设置匹配占比
multiMatchQueryBuilder.minimumShouldMatch(
"
70%
"
);
//提升另个字段的Boost值
multiMatchQueryBuilder.field(
"
name
"
,10);
boolQueryBuilder.must(multiMatchQueryBuilder);
}
//过虑
if(StringUtils.isNotEmpty(courseSearchParam.
getMt())){
boolQueryBuilder.filter(QueryBuilders.termQuery(
"
mt
"
,courseSearchParam.
getMt()));
}
if(StringUtils.isNotEmpty(courseSearchParam.
getSt())){
boolQueryBuilder.filter(QueryBuilders.termQuery(
"
st
"
,courseSearchParam.
getSt()));
}
if(StringUtils.isNotEmpty(courseSearchParam.
getGrade())){
boolQueryBuilder.filter(QueryBuilders.termQuery(
"
grade
"
,courseSearchParam.
getGrade()));
}
//布尔查询
searchSourceBuilder.
query(boolQueryBuilder);
//请求搜索
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse
= null;
try {
searchResponse
= restHighLevelClient.search(searchRequest);
} catch (IOException e) {
e.
printStackTrace();
LOGGER.error(
"
xuecheng search error..
{}
"
,e.
getMessage());
return new QueryResponseResult(CommonCode.SUCCESS,new QueryResult<CoursePub>());
}
//结果集处理
SearchHits hits
=
searchResponse.
getHits();
SearchHit[] searchHits
= hits.
getHits();
//记录总数
long totalHits
= hits.
getTotalHits();
//数据列表
List<CoursePub> list
= new ArrayList<>();
for (SearchHit hit : searchHits) {
CoursePub coursePub
= new CoursePub();
//取出source
Map<String, Object> sourceAsMap
= hit.
getSourceAsMap();
//取出名称
String name
=
(String) sourceAsMap
.
get(
"
name
"
);
coursePub.setName(name);
//图片
String pic
=
(String) sourceAsMap
.
get(
"
pic
"
);
coursePub.setPic(pic);
//价格
Float price
= null;
try {
if(sourceAsMap
.
get(
"
price
"
)!
=null ){
price
= Float.
parseFloat((String) sourceAsMap
.
get(
"
price
"
));
}
} catch (Exception e) {
e.
printStackTrace();
}
coursePub.setPrice(price);
Float price_old
= null;
try {
if(sourceAsMap
.
get(
"
price_old
"
)!
=null ){
price_old
= Float.
parseFloat((String) sourceAsMap
.
get(
"
price_old
"
));
}
} catch (Exception e) {
e.
printStackTrace();
}
coursePub.setPrice_old(price_old);
list.add(coursePub);
}
QueryResult<CoursePub> queryResult
= new QueryResult<>();
queryResult.setList(list);
queryResult.setTotal(totalHits);
QueryResponseResult<CoursePub> coursePubQueryResponseResult
= new
QueryResponseResult<CoursePub>(CommonCode.SUCCESS,queryResult);
return coursePubQueryResponseResult;
}
}



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马