spring boot 集成 elasticsearch 7.x (四)

上节讲完普通索引的创建以及索引文档的插入、修改、删除和文档分页查询。
接着讲 competion suggester 的索引。

competion suggester 提供自动完成 / 搜索即用类型功能。
是一个导航功能,以引导用户在键入相关结果时,提高搜索精度。
它不是用来拼写纠正或确实是你的意思功能,如术语或短语暗示。

理想情况下,自动完成功能应该和用户类型一样快,以提供与用户已经输入的内容相关的即时反馈。
因此,competion suggester 是优化速度。
suggester 使用的数据结构能够快速查找,但是构建和存储在内存中的成本很高。

创建索引

/***
     * 

* 创建索引: *

* @param indexName 索引名称 * @param completionIndexFieldEntityList 索引包含的字段 */ public ResponseResult createCompletionIndex(String indexName, List<CompletionIndexFieldEntity> completionIndexFieldEntityList) { IdxVo idxVo = new IdxVo(); idxVo.setIdxName(indexName); IdxVo.IdxSql tidxSql = new IdxVo.IdxSql(); tidxSql.setDynamic(false); for (CompletionIndexFieldEntity indexFieldEntity : completionIndexFieldEntityList) { Map<String, Object> map = new HashMap(); map.put(ESConstants.IndexConstans.TYPE, indexFieldEntity.getType()); // completion 的专用 if (StringUtils.isNotEmpty(indexFieldEntity.getPreservePositionIncrements())) { map.put(ESConstants.IndexConstans.PRESERVE_POSITION_INCREMENTS, indexFieldEntity.getPreservePositionIncrements()); } if (StringUtils.isNotEmpty(indexFieldEntity.getPreserveSeparators())) { map.put(ESConstants.IndexConstans.PRESERVE_SEPARATORS, indexFieldEntity.getPreserveSeparators()); } if (indexFieldEntity.getMaxInputLength() > -1) { map.put(ESConstants.IndexConstans.MAX_INPUT_LENGTH, indexFieldEntity.getMaxInputLength()); } if (indexFieldEntity.getContextsMapList() != null && indexFieldEntity.getContextsMapList().size() > 0) { map.put(ESConstants.IndexConstans.CONTEXTS, indexFieldEntity.getContextsMapList()); } tidxSql.getProperties().put(indexFieldEntity.getName(), map); } idxVo.setIdxSql(tidxSql); ResponseResult response = createIndex(idxVo); return response; }

插入文档

这里插入几个测试文档,包含上下文 category_code 和 channel

public void insertIndex4CompletionWithContexts() {


        Map<String, Object> suggestMap = new HashMap<>();

        String id = System.currentTimeMillis() + "_myId";

        List<String> inputList = new ArrayList<>();
        inputList.add("头发长,见识短");
        inputList.add("见识短,头发长");
        inputList.add(id);

        Map<String, List<String>> contextsMap1 = new HashMap<>();

        List<String> categoryCodeList = new ArrayList<>();
        categoryCodeList.add("100003");
        categoryCodeList.add("100001");
        contextsMap1.put("category_code", categoryCodeList);

        List<String> channelList = new ArrayList<>();
        channelList.add("web");
        channelList.add("miniapp");
        contextsMap1.put("channel", channelList);

        suggestMap.put("input", inputList);
        suggestMap.put("contexts", contextsMap1);
        //suggestMap.put("id", id);

        Map<String, Map> rootMap = new HashMap<>();
        rootMap.put("suggest", suggestMap);

        ElasticEntity entity = new ElasticEntity();
        entity.setId(id);
        entity.setData(rootMap);


        elasticService.insertOrUpdateOne(indexName, entity);
        log.debug("batch insert success");
    }
    
/**
     * @param idxName index
     * @param entity  对象
     * @return void
     */
    public void insertOrUpdateOne(String idxName, ElasticEntity entity) {
        IndexRequest request = new IndexRequest(idxName);
        log.info("Data : id={},entity={}", entity.getId(), JSON.toJSONString(entity.getData()));
        request.id(entity.getId());
        request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
        try {
            restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

查询

查询的时候,可以指定上下文进行范围圈定。

    @Test
    public void testCompletionSuggestion(){

        String completionFieldName = "suggest";
        String prefixWord = "佛山";

        Map<String, List> contextMap = new HashMap<>();

        List<String> categoryCodeList = new ArrayList<>();
        categoryCodeList.add("100001");
        categoryCodeList.add("100003");
        contextMap.put("category_code", categoryCodeList);

        List<String> channelList = new ArrayList<>();
        channelList.add("web");
        channelList.add("h5");
        contextMap.put("channel", channelList);

        List<String> completionSuggestList = elasticService.getCompletionSuggestList(indexName, completionFieldName, contextMap, prefixWord);

        log.error("---testSuggestion over!--" + JSON.toJSONString(completionSuggestList));

    }

你可能感兴趣的:(elasticsearch)