Search in sources :

Example 1 with HighlightField

use of org.opensearch.search.fetch.subphase.highlight.HighlightField in project fess by codelibs.

the class QueryResponseList method parseSearchHit.

protected Map<String, Object> parseSearchHit(final FessConfig fessConfig, final String hlPrefix, final SearchHit searchHit) {
    final Map<String, Object> docMap = new HashMap<>(32);
    if (searchHit.getSourceAsMap() == null) {
        searchHit.getFields().forEach((key, value) -> {
            docMap.put(key, value.getValue());
        });
    } else {
        docMap.putAll(searchHit.getSourceAsMap());
    }
    final ViewHelper viewHelper = ComponentUtil.getViewHelper();
    final Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
    try {
        if (highlightFields != null) {
            highlightFields.values().stream().forEach(highlightField -> {
                final String text = viewHelper.createHighlightText(highlightField);
                if (text != null) {
                    docMap.put(hlPrefix + highlightField.getName(), text);
                }
            });
            if (Constants.TEXT_FRAGMENT_TYPE_HIGHLIGHT.equals(fessConfig.getQueryHighlightTextFragmentType())) {
                docMap.put(Constants.TEXT_FRAGMENTS, viewHelper.createTextFragmentsByHighlight(highlightFields.values().toArray(n -> new HighlightField[n])));
            }
        }
    } catch (final Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Could not create a highlighting value: {}", docMap, e);
        }
    }
    if (Constants.TEXT_FRAGMENT_TYPE_QUERY.equals(fessConfig.getQueryHighlightTextFragmentType())) {
        docMap.put(Constants.TEXT_FRAGMENTS, viewHelper.createTextFragmentsByQuery());
    }
    // ContentTitle
    if (viewHelper != null) {
        docMap.put(fessConfig.getResponseFieldContentTitle(), viewHelper.getContentTitle(docMap));
        docMap.put(fessConfig.getResponseFieldContentDescription(), viewHelper.getContentDescription(docMap));
        docMap.put(fessConfig.getResponseFieldUrlLink(), viewHelper.getUrlLink(docMap));
        docMap.put(fessConfig.getResponseFieldSitePath(), viewHelper.getSitePath(docMap));
    }
    if (!docMap.containsKey(Constants.SCORE)) {
        docMap.put(Constants.SCORE, searchHit.getScore());
    }
    if (!docMap.containsKey(fessConfig.getIndexFieldId())) {
        docMap.put(fessConfig.getIndexFieldId(), searchHit.getId());
    }
    return docMap;
}
Also used : HashMap(java.util.HashMap) HighlightField(org.opensearch.search.fetch.subphase.highlight.HighlightField) ViewHelper(org.codelibs.fess.helper.ViewHelper)

Aggregations

HashMap (java.util.HashMap)1 ViewHelper (org.codelibs.fess.helper.ViewHelper)1 HighlightField (org.opensearch.search.fetch.subphase.highlight.HighlightField)1