mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-09 19:06:49 +08:00
课程学习的ES处理
This commit is contained in:
@@ -394,7 +394,6 @@ public class CourseElasticsearchImpl implements ICourseFullTextSearch{
|
|||||||
|
|
||||||
if(StringUtils.isNotBlank(params.getKeywords())) {
|
if(StringUtils.isNotBlank(params.getKeywords())) {
|
||||||
|
|
||||||
|
|
||||||
String words=QueryParser.escape(params.getKeywords());
|
String words=QueryParser.escape(params.getKeywords());
|
||||||
|
|
||||||
// System.out.println(params.getKeywords());
|
// System.out.println(params.getKeywords());
|
||||||
|
|||||||
@@ -0,0 +1,232 @@
|
|||||||
|
package com.xboe.module.elasticsearc;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.lucene.queryparser.classic.QueryParser;
|
||||||
|
import org.elasticsearch.action.DocWriteResponse;
|
||||||
|
import org.elasticsearch.action.delete.DeleteRequest;
|
||||||
|
import org.elasticsearch.action.delete.DeleteResponse;
|
||||||
|
import org.elasticsearch.action.get.GetRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.action.support.replication.ReplicationResponse;
|
||||||
|
import org.elasticsearch.action.update.UpdateRequest;
|
||||||
|
import org.elasticsearch.action.update.UpdateResponse;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||||
|
import org.elasticsearch.client.indices.CreateIndexResponse;
|
||||||
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.search.SearchHit;
|
||||||
|
import org.elasticsearch.search.SearchHits;
|
||||||
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.xboe.common.PageList;
|
||||||
|
import com.xboe.module.course.dto.CourseFullText;
|
||||||
|
import com.xboe.module.course.dto.CourseStudyDto;
|
||||||
|
import com.xboe.module.course.service.ICourseStudySearch;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 课程学习的ES实现
|
||||||
|
* @author seastar
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class CourseStudyElasticsearchImpl implements ICourseStudySearch{
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
RestHighLevelClient restHighLevelClient;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ElasticsearchUtil esUtil;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createIndex() throws Exception{
|
||||||
|
boolean isExists = esUtil.isIndexExists(IndexName);
|
||||||
|
if(isExists) {
|
||||||
|
throw new Exception("索引已存在");
|
||||||
|
}
|
||||||
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||||
|
builder.startObject();
|
||||||
|
builder.field("properties").startObject();
|
||||||
|
builder.field("id").startObject().field("index", "true").field("type", "keyword").endObject();
|
||||||
|
builder.field("accountId").startObject().field("type", "keyword").endObject();
|
||||||
|
builder.field("courseId").startObject().field("type", "keyword").endObject();
|
||||||
|
builder.field("courseName").startObject().field("type", "keyword").endObject();
|
||||||
|
builder.field("courseImage").startObject().field("type", "keyword").endObject();
|
||||||
|
builder.field("courseType").startObject().field("type", "integer").endObject();
|
||||||
|
builder.field("source").startObject().field("type", "integer").endObject();
|
||||||
|
builder.field("startTime").startObject().field("type", "integer").endObject();
|
||||||
|
builder.field("status").startObject().field("type", "integer").endObject();
|
||||||
|
builder.field("progress").startObject().field("type", "integer").endObject();
|
||||||
|
builder.endObject();
|
||||||
|
builder.endObject();
|
||||||
|
|
||||||
|
CreateIndexRequest createIndexRequest = new CreateIndexRequest(IndexName);
|
||||||
|
createIndexRequest.mapping(builder);
|
||||||
|
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
|
||||||
|
boolean acknowledged = createIndexResponse.isAcknowledged();
|
||||||
|
if (!acknowledged) {
|
||||||
|
log.error("创建课程学习索引失败",createIndexResponse);
|
||||||
|
throw new Exception("创建索引失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteIndex() {
|
||||||
|
esUtil.deleteIndex(IndexName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String publishOrUpdate(CourseStudyDto dto,String docId) throws IOException {
|
||||||
|
|
||||||
|
boolean exists=false;
|
||||||
|
if(StringUtils.isNotBlank(docId)) {
|
||||||
|
GetRequest getRequest=new GetRequest(IndexName,docId);
|
||||||
|
exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
|
||||||
|
}
|
||||||
|
//System.out.println(item.getName()+"="+item.getType());
|
||||||
|
if(exists) {
|
||||||
|
UpdateRequest updateRequest = new UpdateRequest(IndexName, docId);
|
||||||
|
ObjectMapper mapper=new ObjectMapper();
|
||||||
|
String textJson =mapper.writeValueAsString(dto);
|
||||||
|
updateRequest.doc(textJson, XContentType.JSON);
|
||||||
|
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
|
||||||
|
int ok=updateResponse.status().getStatus();
|
||||||
|
if (ok!=200) {
|
||||||
|
log.error("更新【"+docId+"】失败",updateResponse);
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
IndexRequest indexRequest = new IndexRequest(IndexName);
|
||||||
|
ObjectMapper mapper=new ObjectMapper();
|
||||||
|
String textJson =mapper.writeValueAsString(dto);
|
||||||
|
indexRequest.source(textJson, XContentType.JSON);
|
||||||
|
|
||||||
|
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
|
||||||
|
if (indexResponse != null) {
|
||||||
|
String id = indexResponse.getId();
|
||||||
|
String index = indexResponse.getIndex();
|
||||||
|
long version = indexResponse.getVersion();
|
||||||
|
|
||||||
|
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
|
||||||
|
log.info("新增全文索引文档成功! " + index + "-" + id + "-" + version);
|
||||||
|
return id;
|
||||||
|
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
|
||||||
|
log.info("修改文档成功!");
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return docId;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateFieldByDocId(String field, Object value, String docId) throws Exception {
|
||||||
|
UpdateRequest updateRequest = new UpdateRequest(IndexName, docId);
|
||||||
|
ObjectMapper mapper=new ObjectMapper();
|
||||||
|
Map<String,Object> map=new HashMap<String,Object>();
|
||||||
|
map.put(field, value);
|
||||||
|
String textJson =mapper.writeValueAsString(map);
|
||||||
|
|
||||||
|
updateRequest.doc(textJson, XContentType.JSON);
|
||||||
|
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
|
||||||
|
if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
|
||||||
|
log.info("更新课程学习字段【"+docId+"】【"+field+"】成功!");
|
||||||
|
} else {
|
||||||
|
log.error("更新课程学习字段【"+docId+"】【"+field+"】失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeByDocId(String id) throws Exception {
|
||||||
|
|
||||||
|
DeleteRequest deleteRequest = new DeleteRequest(IndexName);
|
||||||
|
deleteRequest.id(id);
|
||||||
|
try {
|
||||||
|
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
|
||||||
|
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
|
||||||
|
log.error("删除失败,未找到索引id是【"+id+"】的文档");
|
||||||
|
}
|
||||||
|
//restHighLevelClient.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("删除ES索引错误",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PageList<CourseStudyDto> search(int startRow, int pageSize, CourseStudyDto dto) throws Exception {
|
||||||
|
BoolQueryBuilder boolQuery= QueryBuilders.boolQuery();
|
||||||
|
if(StringUtils.isNotBlank(dto.getCourseName())) {
|
||||||
|
String words=QueryParser.escape(dto.getCourseName());
|
||||||
|
boolQuery.filter(QueryBuilders.wildcardQuery("courseName", "*"+words+"*").boost(9f));
|
||||||
|
}
|
||||||
|
if(dto.getCourseType()!=null) {
|
||||||
|
boolQuery.filter(QueryBuilders.termQuery("courseType",dto.getCourseType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dto.getStatus()!=null) {
|
||||||
|
boolQuery.filter(QueryBuilders.termQuery("status",dto.getStatus()));
|
||||||
|
}
|
||||||
|
|
||||||
|
SearchRequest searchRequest = new SearchRequest();
|
||||||
|
searchRequest.indices(IndexName);
|
||||||
|
|
||||||
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
||||||
|
sourceBuilder.from(startRow);
|
||||||
|
sourceBuilder.size(pageSize);
|
||||||
|
|
||||||
|
if(boolQuery.hasClauses()) {
|
||||||
|
sourceBuilder.query(boolQuery);
|
||||||
|
}
|
||||||
|
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
|
||||||
|
|
||||||
|
searchRequest.source(sourceBuilder);
|
||||||
|
|
||||||
|
//进行查询
|
||||||
|
List<CourseStudyDto> list=new ArrayList<>();
|
||||||
|
ObjectMapper mapper=new ObjectMapper();
|
||||||
|
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
SearchHits hits = response.getHits();
|
||||||
|
//log.info("共查询到 : "+hits.getTotalHits());
|
||||||
|
for (SearchHit hit : hits) {
|
||||||
|
String sourceAsString = hit.getSourceAsString();
|
||||||
|
try {
|
||||||
|
CourseStudyDto cft =mapper.readValue(sourceAsString, CourseStudyDto.class);
|
||||||
|
cft.setEsId(hit.getId());
|
||||||
|
list.add(cft);
|
||||||
|
}catch(Exception e) {
|
||||||
|
log.error("转化json到对应失败",sourceAsString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PageList<CourseStudyDto> rs=new PageList<CourseStudyDto>();
|
||||||
|
rs.setCount((int)hits.getTotalHits().value);
|
||||||
|
rs.setPageSize(pageSize);
|
||||||
|
rs.setList(list);
|
||||||
|
return rs;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user