Search in sources :

Example 1 with DocumentField

use of org.opensearch.common.document.DocumentField in project fess by codelibs.

the class QueryResponseList method init.

public void init(final OptionalEntity<SearchResponse> searchResponseOpt, final int start, final int pageSize) {
    searchResponseOpt.ifPresent(searchResponse -> {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        final SearchHits searchHits = searchResponse.getHits();
        allRecordCount = searchHits.getTotalHits().value;
        allRecordCountRelation = searchHits.getTotalHits().relation.toString();
        queryTime = searchResponse.getTook().millis();
        if (searchResponse.getTotalShards() != searchResponse.getSuccessfulShards()) {
            partialResults = true;
        }
        // build highlighting fields
        final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
        final String hlPrefix = queryHelper.getHighlightPrefix();
        for (final SearchHit searchHit : searchHits.getHits()) {
            final Map<String, Object> docMap = parseSearchHit(fessConfig, hlPrefix, searchHit);
            if (fessConfig.isResultCollapsed()) {
                final Map<String, SearchHits> innerHits = searchHit.getInnerHits();
                if (innerHits != null) {
                    final SearchHits innerSearchHits = innerHits.get(fessConfig.getQueryCollapseInnerHitsName());
                    if (innerSearchHits != null) {
                        final long totalHits = innerSearchHits.getTotalHits().value;
                        if (totalHits > 1) {
                            docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_count", totalHits);
                            final DocumentField bitsField = searchHit.getFields().get(fessConfig.getIndexFieldContentMinhashBits());
                            if (bitsField != null && !bitsField.getValues().isEmpty()) {
                                docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_hash", bitsField.getValues().get(0));
                            }
                            docMap.put(fessConfig.getQueryCollapseInnerHitsName(), StreamUtil.stream(innerSearchHits.getHits()).get(stream -> stream.map(v -> parseSearchHit(fessConfig, hlPrefix, v)).toArray(n -> new Map[n])));
                        }
                    }
                }
            }
            parent.add(docMap);
        }
        // facet
        final Aggregations aggregations = searchResponse.getAggregations();
        if (aggregations != null) {
            facetResponse = new FacetResponse(aggregations);
        }
    });
    if (pageSize > 0) {
        calculatePageInfo(start, pageSize);
    }
}
Also used : Constants(org.codelibs.fess.Constants) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) OptionalEntity(org.dbflute.optional.OptionalEntity) SearchHit(org.opensearch.search.SearchHit) HighlightField(org.opensearch.search.fetch.subphase.highlight.HighlightField) Collection(java.util.Collection) HashMap(java.util.HashMap) Aggregations(org.opensearch.search.aggregations.Aggregations) DocumentField(org.opensearch.common.document.DocumentField) SearchHits(org.opensearch.search.SearchHits) ArrayList(java.util.ArrayList) List(java.util.List) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) StreamUtil(org.codelibs.core.stream.StreamUtil) Map(java.util.Map) QueryHelper(org.codelibs.fess.helper.QueryHelper) ViewHelper(org.codelibs.fess.helper.ViewHelper) SearchResponse(org.opensearch.action.search.SearchResponse) LogManager(org.apache.logging.log4j.LogManager) SearchHit(org.opensearch.search.SearchHit) DocumentField(org.opensearch.common.document.DocumentField) Aggregations(org.opensearch.search.aggregations.Aggregations) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) QueryHelper(org.codelibs.fess.helper.QueryHelper) SearchHits(org.opensearch.search.SearchHits)

Example 2 with DocumentField

use of org.opensearch.common.document.DocumentField in project fess by codelibs.

the class SearchHelper method scrollSearch.

public long scrollSearch(final SearchRequestParams params, final BooleanFunction<Map<String, Object>> cursor, final OptionalThing<FessUserBean> userBean) {
    LaRequestUtil.getOptionalRequest().ifPresent(request -> {
        request.setAttribute(Constants.REQUEST_LANGUAGES, params.getLanguages());
        request.setAttribute(Constants.REQUEST_QUERIES, params.getQuery());
    });
    final int pageSize = params.getPageSize();
    final String query = ComponentUtil.getQueryStringBuilder().params(params).sortField(params.getSort()).build();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    return ComponentUtil.getSearchEngineClient().<Map<String, Object>>scrollSearch(fessConfig.getIndexDocumentSearchIndex(), searchRequestBuilder -> {
        final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
        queryHelper.processSearchPreference(searchRequestBuilder, userBean, query);
        return SearchConditionBuilder.builder(searchRequestBuilder).scroll().query(query).size(pageSize).responseFields(queryHelper.getScrollResponseFields()).searchRequestType(params.getType()).build();
    }, (searchResponse, hit) -> {
        final Map<String, Object> docMap = new HashMap<>();
        final Map<String, Object> source = hit.getSourceAsMap();
        if (source != null) {
            docMap.putAll(source);
        }
        final Map<String, DocumentField> fields = hit.getFields();
        if (fields != null) {
            docMap.putAll(fields.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> (Object) e.getValue().getValues())));
        }
        final ViewHelper viewHelper = ComponentUtil.getViewHelper();
        if (viewHelper != null && !docMap.isEmpty()) {
            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, hit.getScore());
        }
        docMap.put(fessConfig.getIndexFieldId(), hit.getId());
        docMap.put(fessConfig.getIndexFieldVersion(), hit.getVersion());
        docMap.put(fessConfig.getIndexFieldSeqNo(), hit.getSeqNo());
        docMap.put(fessConfig.getIndexFieldPrimaryTerm(), hit.getPrimaryTerm());
        return docMap;
    }, cursor);
}
Also used : Entry(java.util.Map.Entry) DocumentField(org.opensearch.common.document.DocumentField) HashMap(java.util.HashMap) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)2 DocumentField (org.opensearch.common.document.DocumentField)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Entry (java.util.Map.Entry)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 StreamUtil (org.codelibs.core.stream.StreamUtil)1 Constants (org.codelibs.fess.Constants)1 QueryHelper (org.codelibs.fess.helper.QueryHelper)1 ViewHelper (org.codelibs.fess.helper.ViewHelper)1 OptionalEntity (org.dbflute.optional.OptionalEntity)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 SearchHit (org.opensearch.search.SearchHit)1 SearchHits (org.opensearch.search.SearchHits)1