基于Elasticsearch实现搜索推荐及搜索词高亮状态

废话不说,直接上代码。

private static final String GOODS_NAME = "goodsName";
private static final String SEARCH_NAME = "searchName";

    /**
     * 权重分求和模式 由于无相关性的分值默认为 1 ,设置权重分最小值为 10
     */
Float MIN_SCORE = 1F;


public PageUtils<EsDocument> findDocumentPage(Map<String, Object> params) {
        int page = TryParse.parseInt(params.get("page"));
        int limit = TryParse.parseInt(params.get("limit"));
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        String searchKey = TryParse.parseString(params.get("queryName"));
        String searchValue = TryParse.parseString(params.get("queryValue"));
        String hotSearchId = TryParse.parseString(params.get("hotSerachId"));
        int sort = TryParse.parseInt(params.get("sort"));
        String userId = TryParse.parseString(params.get("userId"));
        if (StringUtils.isNotEmpty(searchKey) && StringUtils.isNotEmpty(searchValue)) {
            if (GOODS_NAME.equals(searchKey)) {
                builder.must(QueryBuilders.matchPhraseQuery(GOODS_NAME, searchValue));
            }
            if (SEARCH_NAME.equals(searchKey)) {
                //matchPhraseQuery 搜索的词不会被分词器分词
                //matchQuery 搜索的词会被分词器分词
                builder.must(QueryBuilders.matchPhraseQuery(SEARCH_NAME, searchValue));
            }
        }
        //权重分权计算
        WeightBuilder weightBuilder = ScoreFunctionBuilders.weightFactorFunction(MIN_SCORE);
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builder, weightBuilder);
        //降序
        Pageable pageable = null;
        //价格排序
        if (sort == 0){
            pageable = PageRequest.of(page - 1, limit, Direction.DESC, "_score");
        }else if (sort == 1){
            pageable = PageRequest.of(page - 1, limit, Direction.ASC, "minNormalPrice");
        }else if (sort == 2){
            pageable = PageRequest.of(page - 1, limit, Direction.DESC, "minNormalPrice");
        }
        /**
         * 高亮条件
         * highlightField高亮字段
         * preTags高亮前段
         * preTags高亮后段
         * requireFieldMatch(false)支持多字段高亮,改为true只能高亮搜索的字段
         */
        String highlightField = "goodsName";
        String preTags = "<b class=\"dzgoodsName\">";
        String postTags = "</b>";
        HighlightBuilder.Field goodsField = new HighlightBuilder.Field(highlightField).preTags(preTags).postTags(postTags).requireFieldMatch(false);
        //实例化查询对象
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(functionScoreQueryBuilder)
                .withHighlightFields(goodsField)
                .withPageable(pageable)
                .build();
        //查询并封装成page对象
        //Page<EsDocument> result = esDao.search(query);
        Page<EsDocument> result = elasticsearchTemplate.queryForPage(searchQuery, EsDocument.class, new HighlightResultMapper());
        return new PageUtils<>(result.getContent(), result.getTotalElements(), limit, page);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它提供了全文搜索、实时数据分析和数据可视化等功能。在Elasticsearch实现全文搜索高亮显示可以通过以下步骤来完成: 1. 创建索引:首先,你需要创建一个索引来存储你的文档数据。索引是Elasticsearch中用于组织和存储数据的逻辑容器。你可以使用Elasticsearch的API或者客户端库来创建索引。 2. 定义映射:在创建索引之前,你需要定义文档的映射(mapping)。映射定义了文档中的字段以及每个字段的类型。对于需要进行全文搜索高亮显示的字段,你可以将其类型设置为"text"。 3. 添加文档:将你的文档数据添加到索引中。你可以使用Elasticsearch的API或者客户端库来添加文档。 4. 执行搜索:使用Elasticsearch搜索API来执行全文搜索。你可以指定搜索条件、过滤条件和排序规则等。在搜索结果中,Elasticsearch会返回匹配的文档以及相关的元数据。 5. 高亮显示:为了实现全文搜索高亮显示,你可以使用Elasticsearch高亮功能。在搜索请求中,你可以指定需要高亮显示的字段以及高亮显示的样式。在搜索结果中,Elasticsearch会将匹配的关键词用指定的样式进行标记。 下面是一个示例的搜索请求,用于实现全文搜索高亮显示: ``` GET /your_index/_search { "query": { "match": { "your_field": "your_query" } }, "highlight": { "fields": { "your_field": {} } } } ``` 在上面的请求中,你需要将"your_index"替换为你的索引名称,"your_field"替换为你要搜索的字段名称,"your_query"替换为你的搜索关键词。搜索结果中,匹配的关键词会被用默认的高亮样式进行标记。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值