JAVA读取ES

本文介绍了如何在JAVA中使用ES进行数据的增删改查操作,特别是在web端的应用。重点在于通过Java代码展示了如何查询ES,包括设置查询条件如标题和摘要的匹配,并对查询结果进行处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值