Search in sources :

Example 1 with SearchRequestBuilder

use of org.opensearch.action.search.SearchRequestBuilder in project fess by codelibs.

the class EsAbstractBehavior method delegateQueryDelete.

@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        esCb.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    int count = 0;
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
            }
            count += hits.length;
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
            if (bulkResponse.hasFailures()) {
                throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
            }
            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
        }
    } finally {
        deleteScrollContext(scrollId);
    }
    return count;
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchHit(org.opensearch.search.SearchHit) IllegalBehaviorStateException(org.dbflute.exception.IllegalBehaviorStateException) BulkResponse(org.opensearch.action.bulk.BulkResponse) SearchHits(org.opensearch.search.SearchHits) BulkRequestBuilder(org.opensearch.action.bulk.BulkRequestBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with SearchRequestBuilder

use of org.opensearch.action.search.SearchRequestBuilder 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 3 with SearchRequestBuilder

use of org.opensearch.action.search.SearchRequestBuilder in project fess by codelibs.

the class EsAbstractBehavior method delegateSelectCountUniquely.

// ===================================================================================
// Select
// ======
@Override
protected int delegateSelectCountUniquely(final ConditionBean cb) {
    // #pending check response and cast problem
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex());
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    return (int) getSearchHits(esCb.build(builder).execute().actionGet(searchTimeout)).getTotalHits().value;
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder)

Example 4 with SearchRequestBuilder

use of org.opensearch.action.search.SearchRequestBuilder in project fess by codelibs.

the class EsAbstractBehavior method delegateQueryDelete.

@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        esCb.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    int count = 0;
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
            }
            count += hits.length;
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
            if (bulkResponse.hasFailures()) {
                throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
            }
            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
        }
    } finally {
        deleteScrollContext(scrollId);
    }
    return count;
}
Also used : SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchHit(org.opensearch.search.SearchHit) IllegalBehaviorStateException(org.dbflute.exception.IllegalBehaviorStateException) BulkResponse(org.opensearch.action.bulk.BulkResponse) SearchHits(org.opensearch.search.SearchHits) BulkRequestBuilder(org.opensearch.action.bulk.BulkRequestBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with SearchRequestBuilder

use of org.opensearch.action.search.SearchRequestBuilder 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.log.allcommon.EsAbstractEntity.DocMeta) SearchHits(org.opensearch.search.SearchHits)

Aggregations

SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)13 SearchResponse (org.opensearch.action.search.SearchResponse)9 SearchHits (org.opensearch.search.SearchHits)9 SearchHit (org.opensearch.search.SearchHit)6 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 List (java.util.List)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1 Resource (javax.annotation.Resource)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 StringUtil (org.codelibs.core.lang.StringUtil)1 ThreadUtil (org.codelibs.core.lang.ThreadUtil)1 Constants (org.codelibs.fess.Constants)1 Crawler (org.codelibs.fess.crawler.Crawler)1