spring boot 集成 elasticsearch 7.x (四)

上節講完普通索引的創建以及索引文檔的插入、修改、刪除和文檔分頁查詢。
接着講 competion suggester 的索引。

competion suggester 提供自動完成 / 搜索即用類型功能。
是一個導航功能,以引導用戶在鍵入相關結果時,提高搜索精度。
它不是用來拼寫糾正或確實是你的意思功能,如術語或短語暗示。

理想情況下,自動完成功能應該和用戶類型一樣快,以提供與用戶已經輸入的內容相關的即時反饋。
因此,competion suggester 是優化速度。
suggester 使用的數據結構能夠快速查找,但是構建和存儲在內存中的成本很高。

創建索引

/***
     * <p>
     * 創建索引:
     * <p>
     * @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));

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章