web端对ES增删改查
首先要有head插件
我这边需要自己配置内网映射,xshell打开
java代码
@RequestMapping(value = “/getSiteByEs”)
//@SessionAttribute User user, @RequestParam(defaultValue = “article_area”)String orderFile,//updateTime
// @RequestParam(defaultValue = “asc”)String orderType,
public ResponseEntity<Map<String, Object>> getSiteByEs( @RequestParam(defaultValue = “1”) int s,
@RequestParam(defaultValue = “5”) int n, @RequestParam(defaultValue = “”)String keywords) {
Map map =siteServiceInfoService.getSiteByEs(URLUtil.decode(keywords,“UTF-8”),(s - 1) * n, n);//URLUtil.decode(orderFile,“UTF-8”),URLUtil.decode(orderType,“UTF-8”),
Response response = Response.create();
if (MapUtil.isNotEmpty(map)) {
response.setErrCode(0).setData(map).setMsg("查询成功!");
} else {
response.setErrCode(1).setMsg("未查询到数据");
}
return ResponseEntity.accepted().body(response.getResponse());
}
serviceimpl
@Override
public Map getSiteByEs(String keywords, int startRow,
int size) {//String orderFile, String orderType,
BoolQueryBuilder bqb = new BoolQueryBuilder();
if (StringUtils.isNotEmpty(keywords)) {
//取标题
MatchQueryBuilder titleQb = QueryBuilders.matchQuery(“articleTitle”, keywords);
WildcardQueryBuilder wildcardTitleQuery = QueryBuilders.wildcardQuery(“articleTitle”, “" + keywords + "”);
bqb.must(wildcardTitleQuery);
//取摘要
MatchQueryBuilder summaryQb = QueryBuilders.matchQuery(“summary”, keywords);
WildcardQueryBuilder wildcardSummaryQuery = QueryBuilders.wildcardQuery(“summary”, “" + keywords + "”);
bqb.must(wildcardSummaryQuery);
}
SearchRequestBuilder resp = null;
resp = ESClient.client.prepareSearch("test").setTypes("user_relation_article").setQuery(bqb)
// .addSort(orderFile, SortOrder.fromString(orderType))
.setFrom(startRow)
.setSize(size);
SearchHits hits = resp.get().getHits();
Map map = new HashMap();
map.put("total", (int) hits.getTotalHits());
List<UserRelationArticle> userRelationArticles = new ArrayList<>();
for (SearchHit hit :
hits) {
UserRelationArticle userRelationArticle = new UserRelationArticle();
userRelationArticle.setId(hit.getSource().get("id").toString());
if (hit.getSource().get("article_id") != null) {
userRelationArticle.setArticleId(hit.getSource().get("article_id").toString());
}
if (hit.getSource().get("site_name") != null) {
userRelationArticle.setSiteName(hit.getSource().get("site_name").toString());
}
if (hit.getSource().get("media_cls") != null) {
userRelationArticle.setMediaCls(hit.getSource().get("media_cls").toString());
}
if (hit.getSource().get("article_title") != null) {
userRelationArticle.setArticleTitle(hit.getSource().get("article_title").toString());
}
if (hit.getSource().get("article_author") != null) {
userRelationArticle.setArticleAuthor(hit.getSource().get("article_author").toString());
}
if (hit.getSource().get("site_url") != null) {
userRelationArticle.setSiteUrl(hit.getSource().get("site_url").toString());
}
if (hit.getSource().get("article_area") != null) {
userRelationArticle.setArticleArea(hit.getSource().get("article_area").toString());
}
if (hit.getSource().get("article_pubdate") != null) {
userRelationArticle.setArticlePubdate(Timestamp.valueOf(hit.getSource().get("article_pubdate").toString()));
}
if (hit.getSource().get("hit_words") != null) {
userRelationArticle.setHitWords(hit.getSource().get("hit_words").toString());
}
if (hit.getSource().get("positive_word_cloud") != null) {
userRelationArticle.setPositiveWordCloud(hit.getSource().get("positive_word_cloud").toString());
}
if (hit.getSource().get("negative_word_cloud") != null) {
userRelationArticle.setNegativeWordCloud(hit.getSource().get("negative_word_cloud").toString());
}
if (hit.getSource().get("hot_words") != null) {
userRelationArticle.setHotWords(hit.getSource().get("hot_words").toString());
}
if (hit.getSource().get("sentiment_analysis") != null) {
userRelationArticle.setSentimentAnalysis(hit.getSource().get("sentiment_analysis").toString().replaceAll("\"", ""));
}
if (hit.getSource().get("summary") != null) {
userRelationArticle.setSummary(hit.getSource().get("summary").toString().replaceAll("\"", ""));
}
if (hit.getSource().get("nlp_sort") != null) {
userRelationArticle.setNlpSort(hit.getSource().get("nlp_sort").toString().replaceAll("\"", ""));
}
if (hit.getSource().get("relation_words") != null) {
userRelationArticle.setRelationWords(hit.getSource().get("relation_words").toString().replaceAll("\"", ""));
}
if (hit.getSource().get("risk_score") != null) {
userRelationArticle.setRiskScore(Integer.valueOf(hit.getSource().get("risk_score").toString().replaceAll("\"", "")));
}
if (hit.getSource().get("risk_reason") != null) {
userRelationArticle.setRiskReason(hit.getSource().get("risk_reason").toString().replaceAll("\"", ""));
}
if (hit.getSource().get("weight_sort") != null) {
userRelationArticle.setWeightSort(Integer.valueOf(hit.getSource().get("weight_sort").toString().replaceAll("\"", "")));
}
if (hit.getSource().get("update_time") != null) {
userRelationArticle.setUpdateTime(Timestamp.valueOf(hit.getSource().get("update_time").toString().replaceAll("\"", "")));
}
userRelationArticles.add(userRelationArticle);
}
map.put("data", userRelationArticles);
return map;
}