Search in sources :

Example 46 with SearchHits

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project muikku by otavanopisto.

the class ElasticSearchProvider method search.

@Override
public SearchResult search(String query, String[] fields, int start, int maxResults, Class<?>... types) {
    try {
        query = sanitizeSearchString(query);
        String[] typenames = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            typenames[i] = types[i].getSimpleName();
        }
        SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes(typenames).setFrom(start).setSize(maxResults);
        BoolQueryBuilder boolQuery = boolQuery();
        for (String field : fields) {
            boolQuery.should(prefixQuery(field, query));
        }
        SearchResponse response = requestBuilder.setQuery(boolQuery).execute().actionGet();
        List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
        SearchHits searchHits = response.getHits();
        long totalHitCount = searchHits.getTotalHits();
        SearchHit[] results = searchHits.getHits();
        for (SearchHit hit : results) {
            Map<String, Object> hitSource = hit.getSource();
            hitSource.put("indexType", hit.getType());
            searchResults.add(hitSource);
        }
        SearchResult result = new SearchResult(start, maxResults, searchResults, totalHitCount);
        return result;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "ElasticSearch query failed unexpectedly", e);
        return new SearchResult(0, 0, new ArrayList<Map<String, Object>>(), 0);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UnknownHostException(java.net.UnknownHostException) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with SearchHits

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project muikku by otavanopisto.

the class ElasticSearchProvider method matchAllSearch.

@Override
public SearchResult matchAllSearch(int start, int maxResults, Class<?>... types) {
    try {
        String[] typenames = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            typenames[i] = types[i].getSimpleName();
        }
        SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setQuery(matchAllQuery()).setTypes(typenames).setFrom(start).setSize(maxResults);
        SearchResponse response = requestBuilder.execute().actionGet();
        List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
        SearchHits searchHits = response.getHits();
        long totalHitCount = searchHits.getTotalHits();
        SearchHit[] results = searchHits.getHits();
        for (SearchHit hit : results) {
            Map<String, Object> hitSource = hit.getSource();
            hitSource.put("indexType", hit.getType());
            searchResults.add(hitSource);
        }
        SearchResult result = new SearchResult(start, maxResults, searchResults, totalHitCount);
        return result;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "ElasticSearch query failed unexpectedly", e);
        return new SearchResult(0, 0, new ArrayList<Map<String, Object>>(), 0);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UnknownHostException(java.net.UnknownHostException) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with SearchHits

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project vertigo by KleeGroup.

the class ESFacetedQueryResultBuilder method populateCluster.

private void populateCluster(final Bucket bucket, final FacetValue facetValue, final Map<FacetValue, DtList<I>> resultCluster, final Map<String, I> dtcIndex, final Map<I, Map<DtField, String>> resultHighlights) {
    final SearchHits facetSearchHits = ((TopHits) bucket.getAggregations().get(TOPHITS_SUBAGGREAGTION_NAME)).getHits();
    final DtList<I> facetDtc = new DtList<>(indexDefinition.getIndexDtDefinition());
    for (final SearchHit searchHit : facetSearchHits) {
        I result = dtcIndex.get(searchHit.getId());
        if (result == null) {
            final SearchIndex<?, I> index = esDocumentCodec.searchHit2Index(indexDefinition, searchHit);
            result = index.getIndexDtObject();
            dtcIndex.put(searchHit.getId(), result);
            final Map<DtField, String> highlights = createHighlight(searchHit, indexDefinition.getIndexDtDefinition());
            resultHighlights.put(result, highlights);
        }
        facetDtc.add(result);
    }
    resultCluster.put(facetValue, facetDtc);
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits) DtList(io.vertigo.dynamo.domain.model.DtList) DtField(io.vertigo.dynamo.domain.metamodel.DtField)

Example 49 with SearchHits

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project vertexium by visallo.

the class ElasticsearchSearchQueryBase method searchResponseToSearchHitsIterable.

private ElasticsearchGraphQueryIterable<SearchHit> searchResponseToSearchHitsIterable(SearchResponse response) {
    SearchHits hits = response.getHits();
    QueryParameters filterParameters = getParameters().clone();
    Iterable<SearchHit> hitsIterable = IterableUtils.toIterable(hits.getHits());
    return createIterable(response, filterParameters, hitsIterable, false, false, false, response.getTookInMillis(), hits);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits)

Example 50 with SearchHits

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project fess-crawler by codelibs.

the class EsUrlQueueService method updateSessionId.

@Override
public void updateSessionId(final String oldSessionId, final String newSessionId) {
    SearchResponse response = null;
    while (true) {
        if (response == null) {
            response = getClient().get(c -> c.prepareSearch(index).setTypes(type).setScroll(new TimeValue(scrollTimeout)).setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(SESSION_ID, oldSessionId))).setSize(scrollSize).execute());
        } else {
            final String scrollId = response.getScrollId();
            response = getClient().get(c -> c.prepareSearchScroll(scrollId).setScroll(new TimeValue(scrollTimeout)).execute());
        }
        final SearchHits searchHits = response.getHits();
        if (searchHits.getHits().length == 0) {
            break;
        }
        final BulkResponse bulkResponse = getClient().get(c -> {
            final BulkRequestBuilder builder = c.prepareBulk();
            for (final SearchHit searchHit : searchHits) {
                final UpdateRequestBuilder updateRequest = c.prepareUpdate(index, type, searchHit.getId()).setDoc(SESSION_ID, newSessionId);
                builder.add(updateRequest);
            }
            return builder.execute();
        });
        if (bulkResponse.hasFailures()) {
            throw new EsAccessException(bulkResponse.buildFailureMessage());
        }
    }
}
Also used : SortBuilders(org.elasticsearch.search.sort.SortBuilders) SearchHits(org.elasticsearch.search.SearchHits) LoggerFactory(org.slf4j.LoggerFactory) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ArrayList(java.util.ArrayList) PreDestroy(javax.annotation.PreDestroy) OpType(org.elasticsearch.action.DocWriteRequest.OpType) EsUrlQueue(org.codelibs.fess.crawler.entity.EsUrlQueue) Map(java.util.Map) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) RefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy) SearchHit(org.elasticsearch.search.SearchHit) Logger(org.slf4j.Logger) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Collectors(java.util.stream.Collectors) Constants(org.codelibs.fess.crawler.Constants) UrlQueueService(org.codelibs.fess.crawler.service.UrlQueueService) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) SortOrder(org.elasticsearch.search.sort.SortOrder) AccessResult(org.codelibs.fess.crawler.entity.AccessResult) Queue(java.util.Queue) UrlQueue(org.codelibs.fess.crawler.entity.UrlQueue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) SearchHits(org.elasticsearch.search.SearchHits) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)93 SearchHit (org.elasticsearch.search.SearchHit)86 ArrayList (java.util.ArrayList)45 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)27 IOException (java.io.IOException)25 Map (java.util.Map)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 HashMap (java.util.HashMap)19 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchRequest (org.elasticsearch.action.search.SearchRequest)16 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)16 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)14 TotalHits (org.apache.lucene.search.TotalHits)12 Test (org.junit.Test)12 ScoreDoc (org.apache.lucene.search.ScoreDoc)11 Text (org.elasticsearch.common.text.Text)11 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)10