|
|
|
@ -1,14 +1,7 @@
|
|
|
|
package com.hongshu.web.service.impl;
|
|
|
|
package com.hongshu.web.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
import co.elastic.clients.elasticsearch._types.SortOrder;
|
|
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.core.*;
|
|
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
|
|
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.core.search.TotalHits;
|
|
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
|
|
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.indices.DeleteIndexRequest;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
@ -32,6 +25,26 @@ import com.hongshu.web.service.IWebEsNoteService;
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
|
|
|
|
|
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
|
|
|
|
|
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
|
|
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
|
|
|
|
|
import org.elasticsearch.action.delete.DeleteRequest;
|
|
|
|
|
|
|
|
import org.elasticsearch.action.delete.DeleteResponse;
|
|
|
|
|
|
|
|
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.update.UpdateRequest;
|
|
|
|
|
|
|
|
import org.elasticsearch.action.update.UpdateResponse;
|
|
|
|
|
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
|
|
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
|
|
|
|
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.builder.SearchSourceBuilder;
|
|
|
|
|
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
@ -54,7 +67,7 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private WebNoteMapper noteMapper;
|
|
|
|
private WebNoteMapper noteMapper;
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private ElasticsearchClient elasticsearchClient;
|
|
|
|
private RestHighLevelClient restHighLevelClient;
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private WebLikeOrCollectMapper likeOrCollectionMapper;
|
|
|
|
private WebLikeOrCollectMapper likeOrCollectionMapper;
|
|
|
|
|
|
|
|
|
|
|
|
@ -71,40 +84,48 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
Page<NoteSearchVO> page = new Page<>();
|
|
|
|
Page<NoteSearchVO> page = new Page<>();
|
|
|
|
List<NoteSearchVO> noteSearchVOList = new ArrayList<>();
|
|
|
|
List<NoteSearchVO> noteSearchVOList = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder().index(NoteConstant.NOTE_INDEX);
|
|
|
|
SearchRequest searchRequest = new SearchRequest(NoteConstant.NOTE_INDEX);
|
|
|
|
|
|
|
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
|
|
|
|
|
BoolQueryBuilder mainQuery = QueryBuilders.boolQuery();
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(esNoteDTO.getKeyword())) {
|
|
|
|
if (StringUtils.isNotBlank(esNoteDTO.getKeyword())) {
|
|
|
|
builder.query(q -> q.bool(b -> b
|
|
|
|
BoolQueryBuilder keywordQuery = QueryBuilders.boolQuery()
|
|
|
|
.should(h -> h.match(f -> f.field("title").boost(1f).query(esNoteDTO.getKeyword())))
|
|
|
|
.should(QueryBuilders.matchQuery("title", esNoteDTO.getKeyword()).boost(1f))
|
|
|
|
.should(h -> h.match(f -> f.field("username").boost(0.5f).query(esNoteDTO.getKeyword())))
|
|
|
|
.should(QueryBuilders.matchQuery("username", esNoteDTO.getKeyword()).boost(0.5f))
|
|
|
|
.should(h -> h.match(f -> f.field("content").boost(1f).query(esNoteDTO.getKeyword())))
|
|
|
|
.should(QueryBuilders.matchQuery("content", esNoteDTO.getKeyword()).boost(1f))
|
|
|
|
.should(h -> h.match(f -> f.field("tags").boost(4f).query(esNoteDTO.getKeyword())))
|
|
|
|
.should(QueryBuilders.matchQuery("tags", esNoteDTO.getKeyword()).boost(4f))
|
|
|
|
.should(h -> h.match(f -> f.field("categoryName").boost(2f).query(esNoteDTO.getKeyword())))
|
|
|
|
.should(QueryBuilders.matchQuery("categoryName", esNoteDTO.getKeyword()).boost(2f))
|
|
|
|
.should(h -> h.match(f -> f.field("categoryParentName").boost(1.5f).query(esNoteDTO.getKeyword())))
|
|
|
|
.should(QueryBuilders.matchQuery("categoryParentName", esNoteDTO.getKeyword()).boost(1.5f));
|
|
|
|
));
|
|
|
|
mainQuery.must(keywordQuery);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(esNoteDTO.getCpid()) && StringUtils.isNotBlank(esNoteDTO.getCid())) {
|
|
|
|
if (StringUtils.isNotBlank(esNoteDTO.getCpid()) && StringUtils.isNotBlank(esNoteDTO.getCid())) {
|
|
|
|
builder.query(q -> q.bool(b -> b
|
|
|
|
mainQuery.must(QueryBuilders.matchQuery("cid", esNoteDTO.getCid()))
|
|
|
|
.must(h -> h.match(m -> m.field("cid").query(esNoteDTO.getCid())))
|
|
|
|
.must(QueryBuilders.matchQuery("cpid", esNoteDTO.getCpid()));
|
|
|
|
.must(h -> h.match(m -> m.field("cpid").query(esNoteDTO.getCpid())))
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
} else if (StringUtils.isNotBlank(esNoteDTO.getCpid())) {
|
|
|
|
} else if (StringUtils.isNotBlank(esNoteDTO.getCpid())) {
|
|
|
|
builder.query(h -> h.match(m -> m.field("cpid").query(esNoteDTO.getCpid())));
|
|
|
|
mainQuery.must(QueryBuilders.matchQuery("cpid", esNoteDTO.getCpid()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mainQuery.hasClauses()) {
|
|
|
|
|
|
|
|
sourceBuilder.query(mainQuery);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (esNoteDTO.getType() == 1) {
|
|
|
|
if (esNoteDTO.getType() == 1) {
|
|
|
|
builder.sort(o -> o.field(f -> f.field("likeCount").order(SortOrder.Desc)));
|
|
|
|
sourceBuilder.sort("likeCount", SortOrder.DESC);
|
|
|
|
} else if (esNoteDTO.getType() == 2) {
|
|
|
|
} else if (esNoteDTO.getType() == 2) {
|
|
|
|
builder.sort(o -> o.field(f -> f.field("time").order(SortOrder.Desc)));
|
|
|
|
sourceBuilder.sort("time", SortOrder.DESC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.from((int) (currentPage - 1) * (int) pageSize);
|
|
|
|
|
|
|
|
builder.size((int) pageSize);
|
|
|
|
sourceBuilder.from((int) (currentPage - 1) * (int) pageSize);
|
|
|
|
SearchRequest searchRequest = builder.build();
|
|
|
|
sourceBuilder.size((int) pageSize);
|
|
|
|
SearchResponse<NoteSearchVO> searchResponse = elasticsearchClient.search(searchRequest, NoteSearchVO.class);
|
|
|
|
searchRequest.source(sourceBuilder);
|
|
|
|
TotalHits totalHits = searchResponse.hits().total();
|
|
|
|
|
|
|
|
page.setTotal(Objects.requireNonNull(totalHits).value());
|
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
List<Hit<NoteSearchVO>> hits = searchResponse.hits().hits();
|
|
|
|
long totalHits = searchResponse.getHits().getTotalHits().value;
|
|
|
|
for (Hit<NoteSearchVO> hit : hits) {
|
|
|
|
page.setTotal(totalHits);
|
|
|
|
NoteSearchVO noteSearchVo = hit.source();
|
|
|
|
|
|
|
|
|
|
|
|
for (SearchHit hit : searchResponse.getHits().getHits()) {
|
|
|
|
|
|
|
|
NoteSearchVO noteSearchVo = JSON.parseObject(hit.getSourceAsString(), NoteSearchVO.class);
|
|
|
|
noteSearchVOList.add(noteSearchVo);
|
|
|
|
noteSearchVOList.add(noteSearchVo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
@ -139,23 +160,25 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
List<NoteSearchVO> noteSearchVOList = new ArrayList<>();
|
|
|
|
List<NoteSearchVO> noteSearchVOList = new ArrayList<>();
|
|
|
|
//得到当前用户的浏览记录
|
|
|
|
//得到当前用户的浏览记录
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder().index(NoteConstant.NOTE_INDEX);
|
|
|
|
SearchRequest searchRequest = new SearchRequest(NoteConstant.NOTE_INDEX);
|
|
|
|
builder.size(1000);
|
|
|
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
|
SearchRequest searchRequest = builder.build();
|
|
|
|
sourceBuilder.size(1000);
|
|
|
|
SearchResponse<NoteSearchVO> searchResponse = elasticsearchClient.search(searchRequest, NoteSearchVO.class);
|
|
|
|
searchRequest.source(sourceBuilder);
|
|
|
|
TotalHits totalHits = searchResponse.hits().total();
|
|
|
|
|
|
|
|
//得到所有的数据
|
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
List<Hit<NoteSearchVO>> hits = searchResponse.hits().hits();
|
|
|
|
long totalHits = searchResponse.getHits().getTotalHits().value;
|
|
|
|
if (CollectionUtil.isNotEmpty(hits)) {
|
|
|
|
|
|
|
|
for (Hit<NoteSearchVO> hit : hits) {
|
|
|
|
SearchHit[] hits = searchResponse.getHits().getHits();
|
|
|
|
NoteSearchVO noteSearchVo = hit.source();
|
|
|
|
if (hits.length > 0) {
|
|
|
|
|
|
|
|
for (SearchHit hit : hits) {
|
|
|
|
|
|
|
|
NoteSearchVO noteSearchVo = JSON.parseObject(hit.getSourceAsString(), NoteSearchVO.class);
|
|
|
|
noteSearchVOList.add(noteSearchVo);
|
|
|
|
noteSearchVOList.add(noteSearchVo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Collections.shuffle(noteSearchVOList);
|
|
|
|
Collections.shuffle(noteSearchVOList);
|
|
|
|
List<List<NoteSearchVO>> partition = Lists.partition(noteSearchVOList, (int) pageSize);
|
|
|
|
List<List<NoteSearchVO>> partition = Lists.partition(noteSearchVOList, (int) pageSize);
|
|
|
|
List<NoteSearchVO> noteSearchVOS = partition.get((int) currentPage - 1);
|
|
|
|
List<NoteSearchVO> noteSearchVOS = partition.get((int) currentPage - 1);
|
|
|
|
page.setTotal(totalHits != null ? totalHits.value() : 0);
|
|
|
|
page.setTotal(totalHits);
|
|
|
|
page.setRecords(noteSearchVOS);
|
|
|
|
page.setRecords(noteSearchVOS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
@ -176,15 +199,19 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
Page<WebUser> page = new Page<>();
|
|
|
|
Page<WebUser> page = new Page<>();
|
|
|
|
String userId = WebUtils.getRequestHeader(UserConstant.USER_ID);
|
|
|
|
String userId = WebUtils.getRequestHeader(UserConstant.USER_ID);
|
|
|
|
// 用户ID为空 默认随机加载100条数据
|
|
|
|
// 用户ID为空 默认随机加载100条数据
|
|
|
|
List<WebUser> recommendList = null;
|
|
|
|
List<WebUser> recommendList = new ArrayList<>();
|
|
|
|
if (StringUtils.isBlank(userId)) {
|
|
|
|
if (StringUtils.isBlank(userId)) {
|
|
|
|
SearchRequest searchRequest = SearchRequest.of(s -> s
|
|
|
|
SearchRequest searchRequest = new SearchRequest(NoteConstant.NOTE_INDEX);
|
|
|
|
.index(NoteConstant.NOTE_INDEX)
|
|
|
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
|
.size(100));
|
|
|
|
sourceBuilder.size(100);
|
|
|
|
SearchResponse<WebUser> searchResponse = elasticsearchClient.search(searchRequest, WebUser.class);
|
|
|
|
searchRequest.source(sourceBuilder);
|
|
|
|
recommendList = searchResponse.hits().hits().stream()
|
|
|
|
|
|
|
|
.map(Hit::source)
|
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
|
|
for (SearchHit hit : searchResponse.getHits().getHits()) {
|
|
|
|
|
|
|
|
WebUser user = JSON.parseObject(hit.getSourceAsString(), WebUser.class);
|
|
|
|
|
|
|
|
recommendList.add(user);
|
|
|
|
|
|
|
|
}
|
|
|
|
// 随机排序
|
|
|
|
// 随机排序
|
|
|
|
Collections.shuffle(recommendList, new Random());
|
|
|
|
Collections.shuffle(recommendList, new Random());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@ -198,6 +225,7 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
page.setRecords(Collections.emptyList());
|
|
|
|
page.setRecords(Collections.emptyList());
|
|
|
|
return page;
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<WebUser> userList = partition.get((int) currentPage - 1);
|
|
|
|
List<WebUser> userList = partition.get((int) currentPage - 1);
|
|
|
|
page.setTotal(recommendList.size());
|
|
|
|
page.setTotal(recommendList.size());
|
|
|
|
page.setRecords(userList);
|
|
|
|
page.setRecords(userList);
|
|
|
|
@ -216,35 +244,29 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
List<NoteSearchVO> noteSearchVOList = new ArrayList<>();
|
|
|
|
List<NoteSearchVO> noteSearchVOList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// 构建搜索请求
|
|
|
|
SearchRequest searchRequest = new SearchRequest(NoteConstant.NOTE_INDEX);
|
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder()
|
|
|
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
|
.index(NoteConstant.NOTE_INDEX)
|
|
|
|
sourceBuilder.from(Math.toIntExact((currentPage - 1) * pageSize));
|
|
|
|
.from(Math.toIntExact((currentPage - 1) * pageSize)) // 设置分页起始点
|
|
|
|
sourceBuilder.size(Math.toIntExact(pageSize));
|
|
|
|
.size(Math.toIntExact(pageSize)) // 设置分页大小
|
|
|
|
sourceBuilder.sort("likeCount", SortOrder.DESC);
|
|
|
|
.sort(s -> s.field(f -> f.field("likeCount").order(SortOrder.Desc))); // 按 likeCount 降序排序
|
|
|
|
searchRequest.source(sourceBuilder);
|
|
|
|
|
|
|
|
|
|
|
|
SearchRequest searchRequest = builder.build();
|
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
|
|
|
|
long totalHits = searchResponse.getHits().getTotalHits().value;
|
|
|
|
// 执行搜索请求
|
|
|
|
|
|
|
|
SearchResponse<NoteSearchVO> searchResponse = elasticsearchClient.search(searchRequest, NoteSearchVO.class);
|
|
|
|
SearchHit[] hits = searchResponse.getHits().getHits();
|
|
|
|
TotalHits totalHits = searchResponse.hits().total();
|
|
|
|
if (hits.length > 0) {
|
|
|
|
|
|
|
|
for (SearchHit hit : hits) {
|
|
|
|
// 获取搜索结果
|
|
|
|
NoteSearchVO noteSearchVo = JSON.parseObject(hit.getSourceAsString(), NoteSearchVO.class);
|
|
|
|
List<Hit<NoteSearchVO>> hits = searchResponse.hits().hits();
|
|
|
|
|
|
|
|
if (CollectionUtil.isNotEmpty(hits)) {
|
|
|
|
|
|
|
|
for (Hit<NoteSearchVO> hit : hits) {
|
|
|
|
|
|
|
|
NoteSearchVO noteSearchVo = hit.source();
|
|
|
|
|
|
|
|
noteSearchVOList.add(noteSearchVo);
|
|
|
|
noteSearchVOList.add(noteSearchVo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置分页结果
|
|
|
|
page.setTotal(totalHits);
|
|
|
|
page.setTotal(totalHits != null ? totalHits.value() : 0);
|
|
|
|
|
|
|
|
page.setRecords(noteSearchVOList);
|
|
|
|
page.setRecords(noteSearchVOList);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
// 这里可以进一步处理异常,比如记录日志或者抛出自定义异常
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return page;
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -257,8 +279,11 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void addNote(NoteSearchVO noteSearchVo) {
|
|
|
|
public void addNote(NoteSearchVO noteSearchVo) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
CreateResponse createResponse = elasticsearchClient.create(e -> e.index(NoteConstant.NOTE_INDEX).id(noteSearchVo.getId()).document(noteSearchVo));
|
|
|
|
IndexRequest indexRequest = new IndexRequest(NoteConstant.NOTE_INDEX)
|
|
|
|
log.info("createResponse.result{}", createResponse.result());
|
|
|
|
.id(noteSearchVo.getId())
|
|
|
|
|
|
|
|
.source(JSON.toJSONString(noteSearchVo), XContentType.JSON);
|
|
|
|
|
|
|
|
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
|
|
|
|
|
|
|
|
log.info("indexResponse.result{}", indexResponse.getResult());
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -272,8 +297,10 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void updateNote(NoteSearchVO noteSearchVo) {
|
|
|
|
public void updateNote(NoteSearchVO noteSearchVo) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
UpdateResponse<NoteSearchVO> updateResponse = elasticsearchClient.update(e -> e.index(NoteConstant.NOTE_INDEX).id(noteSearchVo.getId()).doc(noteSearchVo), NoteSearchVO.class);
|
|
|
|
UpdateRequest updateRequest = new UpdateRequest(NoteConstant.NOTE_INDEX, noteSearchVo.getId())
|
|
|
|
log.info("updateResponse.result() = " + updateResponse.result());
|
|
|
|
.doc(JSON.toJSONString(noteSearchVo), XContentType.JSON);
|
|
|
|
|
|
|
|
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
|
|
|
|
|
|
|
|
log.info("updateResponse.result() = " + updateResponse.getResult());
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -287,8 +314,9 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void deleteNote(String noteId) {
|
|
|
|
public void deleteNote(String noteId) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeleteResponse deleteResponse = elasticsearchClient.delete(e -> e.index(NoteConstant.NOTE_INDEX).id(String.valueOf(noteId)));
|
|
|
|
DeleteRequest deleteRequest = new DeleteRequest(NoteConstant.NOTE_INDEX, String.valueOf(noteId));
|
|
|
|
log.info("deleteResponse.result() ={} ", deleteResponse.result());
|
|
|
|
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
|
|
|
|
|
|
|
|
log.info("deleteResponse.result() ={} ", deleteResponse.getResult());
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -316,13 +344,15 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
noteSearchVo.setIsLike(likeOrCollectionIds.contains(noteSearchVo.getId()));
|
|
|
|
noteSearchVo.setIsLike(likeOrCollectionIds.contains(noteSearchVo.getId()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
List<BulkOperation> result = new ArrayList<>();
|
|
|
|
BulkRequest bulkRequest = new BulkRequest();
|
|
|
|
for (NoteSearchVO noteSearchVo : noteSearchVOList) {
|
|
|
|
for (NoteSearchVO noteSearchVo : noteSearchVOList) {
|
|
|
|
result.add(new BulkOperation.Builder().create(
|
|
|
|
IndexRequest indexRequest = new IndexRequest(NoteConstant.NOTE_INDEX)
|
|
|
|
d -> d.document(noteSearchVo).id(noteSearchVo.getId()).index(NoteConstant.NOTE_INDEX)).build());
|
|
|
|
.id(noteSearchVo.getId())
|
|
|
|
|
|
|
|
.source(JSON.toJSONString(noteSearchVo), XContentType.JSON);
|
|
|
|
|
|
|
|
bulkRequest.add(indexRequest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BulkResponse bulkResponse = elasticsearchClient.bulk(e -> e.index(NoteConstant.NOTE_INDEX).operations(result));
|
|
|
|
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
|
|
|
|
log.info("createResponse.result{}", bulkResponse.toString());
|
|
|
|
log.info("createResponse.result{}", bulkResponse.hasFailures() ? "has failures" : "success");
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -335,16 +365,19 @@ public class WebEsNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> im
|
|
|
|
public void delNoteBulkData() {
|
|
|
|
public void delNoteBulkData() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// 删除索引
|
|
|
|
// 删除索引
|
|
|
|
DeleteIndexRequest deleteIndexRequest = DeleteIndexRequest.of(builder -> builder.index(NoteConstant.NOTE_INDEX));
|
|
|
|
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(NoteConstant.NOTE_INDEX);
|
|
|
|
elasticsearchClient.indices().delete(deleteIndexRequest);
|
|
|
|
restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
|
|
|
|
log.info("删除索引:{}", NoteConstant.NOTE_INDEX);
|
|
|
|
log.info("删除索引:{}", NoteConstant.NOTE_INDEX);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("删除索引异常 (可能不存在): {}", NoteConstant.NOTE_INDEX);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// 重新创建索引
|
|
|
|
// 重新创建索引
|
|
|
|
CreateIndexRequest createIndexRequest = CreateIndexRequest.of(builder -> builder.index(NoteConstant.NOTE_INDEX));
|
|
|
|
CreateIndexRequest createIndexRequest = new CreateIndexRequest(NoteConstant.NOTE_INDEX);
|
|
|
|
elasticsearchClient.indices().create(createIndexRequest);
|
|
|
|
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
|
|
|
|
log.info("创建索引:{}", NoteConstant.NOTE_INDEX);
|
|
|
|
log.info("创建索引:{}", NoteConstant.NOTE_INDEX);
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("Error occurred while deleting and recreating index: {}", NoteConstant.NOTE_INDEX, e);
|
|
|
|
log.error("Error occurred while creating index: {}", NoteConstant.NOTE_INDEX, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|