Search in sources :

Example 91 with SearchHits

use of org.elasticsearch.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()).setTypes(asEsSearchType());
    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 = response.getHits();
    searchHits.forEach(hit -> {
        final Map<String, Object> source = hit.getSource();
        final RESULT entity = createEntity(source, entityType);
        final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta();
        docMeta.id(hit.getId());
        docMeta.version(hit.getVersion());
        list.add(entity);
    });
    list.setPageSize(size);
    list.setAllRecordCount((int) searchHits.getTotalHits());
    list.setCurrentPageNumber(cb.getFetchPageNumber());
    list.setTook(response.getTookInMillis());
    list.setTotalShards(response.getTotalShards());
    list.setSuccessfulShards(response.getSuccessfulShards());
    list.setFailedShards(response.getFailedShards());
    list.setAggregation(response.getAggregations());
    return list;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) DocMeta(org.codelibs.fess.es.user.allcommon.EsAbstractEntity.DocMeta) SearchHits(org.elasticsearch.search.SearchHits)

Example 92 with SearchHits

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

the class EsAbstractBehavior method delegateBulkRequest.

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

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)92 SearchResponse (org.elasticsearch.action.search.SearchResponse)60 SearchHit (org.elasticsearch.search.SearchHit)49 ArrayList (java.util.ArrayList)22 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)15 IOException (java.io.IOException)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)13 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 ElasticsearchAssertions.assertSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits)13 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)10 TopDocs (org.apache.lucene.search.TopDocs)9 SearchHitField (org.elasticsearch.search.SearchHitField)9 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)9 Text (org.elasticsearch.common.text.Text)7 ElasticsearchAssertions.assertOrderedSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits)7