2.3.4 启动logstash.bat 启动logstash.bat采集teachplan_media_pub中的数据,向ES写入索引。
[mw_shl_code=applescript,true]logstash.bat ‐f ../config/mysql_course_media.conf
[/mw_shl_code]
2.4 搜索服务查询课程媒资接口 2.4.1 需求分析 搜索服务提供查询课程媒资接口,此接口供学习服务调用。 2.4.2 Api 在课程搜索包下定义Api
[mw_shl_code=applescript,true]@ApiOperation("根据课程计划查询媒资信息")
public TeachplanMediaPub getmedia(String teachplanId);[/mw_shl_code]
2.4.3 Service 1、配置课程计划媒资索引库等信息 在application.yml中配置
[mw_shl_code=applescript,true]xuecheng:
elasticsearch:
media:
index: xc_course_media
type: doc
source_field: courseid,media_id,media_url,teachplan_id,media_fileoriginalname[/mw_shl_code]
2、service方法开发 在课程搜索服务中定义课程媒资查询接口,为了适应后续需求,service参数定义为数组,可一次查询多个课程计划 的媒资信息。
[mw_shl_code=applescript,true]//根据课程计划查询媒资信息 public QueryResponseResult<TeachplanMediaPub> getmedia(String[] teachplanIds){ //设置索引 SearchRequest searchRequest = new SearchRequest(media_index);
//设置类型 searchRequest.types(media_type);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//source源字段过虑
String[] source_fields = media_source_field.split(",");
searchSourceBuilder.fetchSource(source_fields, new String[]{});
//查询条件,根据课程计划id查询(可传入多个id)
searchSourceBuilder.query(QueryBuilders.termsQuery("teachplan_id", teachplanIds)); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = null;
try {
//执行搜索
searchResponse = restHighLevelClient.search(searchRequest);
} catch (IOException e) {
e.printStackTrace();
}
//获取搜索结果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
Map<String,CoursePub> map = new HashMap<>();
//数据列表 List<TeachplanMediaPub> teachplanMediaPubList = new ArrayList<>();
for (SearchHit hit : searchHits) {
TeachplanMediaPub teachplanMediaPub =new TeachplanMediaPub();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
//取出课程计划媒资信息
String courseid = (String) sourceAsMap.get("courseid");
String media_id = (String) sourceAsMap.get("media_id");
String media_url = (String) sourceAsMap.get("media_url");
String teachplan_id = (String) sourceAsMap.get("teachplan_id");
String media_fileoriginalname = (String) sourceAsMap.get("media_fileoriginalname");
teachplanMediaPub.setCourseId(courseid);
teachplanMediaPub.setMediaUrl(media_url);
teachplanMediaPub.setMediaFileOriginalName(media_fileoriginalname);
teachplanMediaPub.setMediaId(media_id);
teachplanMediaPub.setTeachplanId(teachplan_id);
//将数据加入列表
teachplanMediaPubList.add(teachplanMediaPub);
}
//构建返回课程媒资信息对象
QueryResult<TeachplanMediaPub> queryResult = new QueryResult<>();
queryResult.setList(teachplanMediaPubList);
QueryResponseResult<TeachplanMediaPub> queryResponseResult = new
QueryResponseResult<TeachplanMediaPub>(CommonCode.SUCCESS,queryResult);
return queryResponseResult; }
[/mw_shl_code]
2.4.4 Controller
[mw_shl_code=applescript,true]
searchResponse = restHighLevelClient.search(searchRequest);
} catch (IOException e) {
e.printStackTrace();
}
//获取搜索结果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
Map<String,CoursePub> map = new HashMap<>();
//数据列表
List<TeachplanMediaPub> teachplanMediaPubList = new ArrayList<>();
for (SearchHit hit : searchHits) {
TeachplanMediaPub teachplanMediaPub =new TeachplanMediaPub();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
//取出课程计划媒资信息
String courseid = (String) sourceAsMap.get("courseid");
String media_id = (String) sourceAsMap.get("media_id");
String media_url = (String) sourceAsMap.get("media_url");
String teachplan_id = (String) sourceAsMap.get("teachplan_id");
String media_fileoriginalname = (String) sourceAsMap.get("media_fileoriginalname");
teachplanMediaPub.setCourseId(courseid);
teachplanMediaPub.setMediaUrl(media_url);
teachplanMediaPub.setMediaFileOriginalName(media_fileoriginalname);
teachplanMediaPub.setMediaId(media_id);
teachplanMediaPub.setTeachplanId(teachplan_id);
//将数据加入列表
teachplanMediaPubList.add(teachplanMediaPub);
}
//构建返回课程媒资信息对象
QueryResult<TeachplanMediaPub> queryResult = new QueryResult<>();
queryResult.setList(teachplanMediaPubList);
QueryResponseResult<TeachplanMediaPub>
queryResponseResult = new
QueryResponseResult<TeachplanMediaPub>(CommonCode.SUCCESS,queryResult);
return queryResponseResult;
}
@Override @GetMapping(value="/getmedia/{teachplanId}") public TeachplanMediaPub getmedia(@PathVariable("teachplanId") String teachplanId) {
//将课程计划id放在数组中,为调用service作准备
String[] teachplanIds = new String[]{teachplanId};
//通过service查询ES获取课程媒资信息
QueryResponseResult<TeachplanMediaPub> mediaPubQueryResponseResult = esCourseService.getmedia(teachplanIds);
QueryResult<TeachplanMediaPub> queryResult = mediaPubQueryResponseResult.getQueryResult();
if(queryResult!=null
&& queryResult.getList()!=null
&& queryResult.getList().size()>0){
//返回课程计划对应课程媒资
return queryResult.getList().get(0);
}
return new TeachplanMediaPub(); }
[/mw_shl_code]
2.4.5 测试 使用swagger-ui和postman测试课程媒资查询接口。