elasticsearch-java 操作es 高亮模糊 简单操作等 版本 7.17.6

public class MethEsSerivceImpl implements MethEsSerivce {

    @Autowired
    private ElasticsearchClient client;

    @Override
    public boolean insertDoc(String indexName,EsBlog blog) throws IOException {

            final IndexResponse response = client.index(builder -> builder.index(indexName).id(String.valueOf(blog.getId())).document(blog));
            return  response.version() > 0;


    }

    @Override
    public List<Hit<EsBlog>> listByIndexName(String indexName) throws IOException {
        //查询所有文档
//        final SearchResponse<Object> response = client.search(builder -> builder.index(indexName), Object.class);
//        final List<Hit<Object>> hits = response.hits().hits();
//        return hits;
        //matchAll
        final SearchResponse<EsBlog> response = client.search(builder ->
                builder.index(indexName)
                        .query(q ->
                                q.matchAll(
                                        v->v
                                )), EsBlog.class);

        System.err.println(response.hits().hits());

        return  response.hits().hits();
    }

    @Override
    public void removeDocById(String indexName,Integer Id) throws IOException {
        //根据ID删除文档
        final DeleteResponse response = client.delete(builder -> builder.index(indexName).id(String.valueOf(Id)));
        System.err.println(response.shards().successful());

    }

    @Override
    public void updateDoc(String indexName, EsBlog blog) throws IOException {
        //修改文档(覆盖)
        final UpdateResponse<EsBlog> response = client.update(builder -> builder.index("produces").id(String.valueOf(blog.getId())).doc(blog), EsBlog.class);
        System.err.println(response.shards().successful());

    }

    @Override
    public boolean updateDocParm(String indexName, EsBlog blog) throws IOException {
        //修改文档(部分修改)
//        final Produce produce = new Produce("ccaabb123", "旺仔摇滚洞", "旺仔摇滚洞乱摇乱滚", 10.23D);

        final UpdateResponse<EsBlog> response = client.update(builder -> builder.index("produces").id(String.valueOf(blog.getId()))
                .doc(blog).docAsUpsert(true), EsBlog.class);
      return  response.version() > 0;

    }

    /**
     * 高亮分词多字段查询
     * @param indexName
     * @param str
     * @return
     * @throws IOException
     */
    @Override
    public List<EsBlog> listByQuery(String indexName, String str) throws IOException {
        //字段分词查询
        //高亮map字段
        Map<String, HighlightField> highlightFieldMap = new HashMap<>();
        highlightFieldMap.put("title",new HighlightField.Builder().build());
        highlightFieldMap.put("count",new HighlightField.Builder().build());
        //查询
        final SearchResponse<EsBlog> response = client.search(builder ->
                        builder.index(indexName)//索引名称
                                .query(q->q.queryString(qs->qs.fields(Arrays.asList("title","count")).query(str)))
                                .highlight(h -> h.preTags("<em>").postTags("</em>").fields(highlightFieldMap)),
                EsBlog.class);

        List<EsBlog> list = new ArrayList<>();
        response.hits().hits().forEach(esBlogHit -> {
            //高亮赋值
            setHighlightField(esBlogHit.highlight(),esBlogHit.source());
            assert esBlogHit.source() != null;
            esBlogHit.source().setId(Long.valueOf(esBlogHit.id()));
            list.add(esBlogHit.source());
        });
        return list;
    }


    public void setHighlightField(Map<String,List<String>> hightData,EsBlog esBlog){

        if (hightData.get("title") != null)
            esBlog.setTitle(hightData.get("title").get(0));

        if (hightData.get("count") != null)
            esBlog.setCount(hightData.get("count").get(0));
    }

个人博客已经应用 详见个人页网址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值