Search in sources :

Example 6 with SearchHits

use of org.opensearch.search.SearchHits 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 7 with SearchHits

use of org.opensearch.search.SearchHits in project fess by codelibs.

the class EsAbstractBehavior method delegateSelectList.

@Override
protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) {
    // #pending check response
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final int from;
    final int size;
    if (cb.isFetchScopeEffective()) {
        from = cb.getPageStartIndex();
        size = cb.getFetchSize();
    } else {
        from = 0;
        size = 10;
    }
    builder.setFrom(from);
    builder.setSize(size);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout);
    final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder);
    final SearchHits searchHits = getSearchHits(response);
    searchHits.forEach(hit -> {
        final Map<String, Object> source = hit.getSourceAsMap();
        final RESULT entity = createEntity(source, entityType);
        final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta();
        docMeta.id(hit.getId());
        docMeta.version(hit.getVersion());
        docMeta.seqNo(hit.getSeqNo());
        docMeta.primaryTerm(hit.getPrimaryTerm());
        list.add(entity);
    });
    list.setPageSize(size);
    list.setAllRecordCount((int) searchHits.getTotalHits().value);
    list.setCurrentPageNumber(cb.getFetchPageNumber());
    list.setTook(response.getTook().getMillis());
    list.setTotalShards(response.getTotalShards());
    list.setSuccessfulShards(response.getSuccessfulShards());
    list.setFailedShards(response.getFailedShards());
    list.setTotalHits(searchHits.getTotalHits());
    list.setAggregation(response.getAggregations());
    return list;
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) DocMeta(org.codelibs.fess.es.user.allcommon.EsAbstractEntity.DocMeta) SearchHits(org.opensearch.search.SearchHits)

Example 8 with SearchHits

use of org.opensearch.search.SearchHits in project fess by codelibs.

the class EsAbstractBehavior method delegateBulkRequest.

protected void delegateBulkRequest(final ConditionBean cb, Function<SearchHits, Boolean> handler) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForCursor).setSize(sizeForCursor);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }
            if (!handler.apply(searchHits)) {
                break;
            }
            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        deleteScrollContext(scrollId);
    }
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchHit(org.opensearch.search.SearchHit) SearchHits(org.opensearch.search.SearchHits) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 9 with SearchHits

use of org.opensearch.search.SearchHits in project fess by codelibs.

the class EsAbstractBehavior method delegateSelectList.

@Override
protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) {
    // #pending check response
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final int from;
    final int size;
    if (cb.isFetchScopeEffective()) {
        from = cb.getPageStartIndex();
        size = cb.getFetchSize();
    } else {
        from = 0;
        size = 10;
    }
    builder.setFrom(from);
    builder.setSize(size);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout);
    final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder);
    final SearchHits searchHits = getSearchHits(response);
    searchHits.forEach(hit -> {
        final Map<String, Object> source = hit.getSourceAsMap();
        final RESULT entity = createEntity(source, entityType);
        final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta();
        docMeta.id(hit.getId());
        docMeta.version(hit.getVersion());
        docMeta.seqNo(hit.getSeqNo());
        docMeta.primaryTerm(hit.getPrimaryTerm());
        list.add(entity);
    });
    list.setPageSize(size);
    list.setAllRecordCount((int) searchHits.getTotalHits().value);
    list.setCurrentPageNumber(cb.getFetchPageNumber());
    list.setTook(response.getTook().getMillis());
    list.setTotalShards(response.getTotalShards());
    list.setSuccessfulShards(response.getSuccessfulShards());
    list.setFailedShards(response.getFailedShards());
    list.setTotalHits(searchHits.getTotalHits());
    list.setAggregation(response.getAggregations());
    return list;
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) DocMeta(org.codelibs.fess.es.config.allcommon.EsAbstractEntity.DocMeta) SearchHits(org.opensearch.search.SearchHits)

Example 10 with SearchHits

use of org.opensearch.search.SearchHits in project fess by codelibs.

the class EsAbstractBehavior method delegateBulkRequest.

protected void delegateBulkRequest(final ConditionBean cb, Function<SearchHits, Boolean> handler) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForCursor).setSize(sizeForCursor);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }
            if (!handler.apply(searchHits)) {
                break;
            }
            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        deleteScrollContext(scrollId);
    }
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchHit(org.opensearch.search.SearchHit) SearchHits(org.opensearch.search.SearchHits) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

SearchResponse (org.opensearch.action.search.SearchResponse)10 SearchHits (org.opensearch.search.SearchHits)10 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)9 SearchHit (org.opensearch.search.SearchHit)7 IllegalBehaviorStateException (org.dbflute.exception.IllegalBehaviorStateException)3 BulkRequestBuilder (org.opensearch.action.bulk.BulkRequestBuilder)3 BulkResponse (org.opensearch.action.bulk.BulkResponse)3 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Map (java.util.Map)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 DocMeta (org.codelibs.fess.es.config.allcommon.EsAbstractEntity.DocMeta)1 DocMeta (org.codelibs.fess.es.log.allcommon.EsAbstractEntity.DocMeta)1