public <T> List<Hit<T, Void>> getHits(Class<T> sourceType, boolean addEsMetadataFields) { return getHits(sourceType, Void.class, addEsMetadataFields); }
public <T> List<Hit<T, Void>> getHits(Class<T> sourceType) { return getHits(sourceType, true); }
public <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType) { return getHits(sourceType, explanationType, false, true); }
public <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean addEsMetadataFields) { return getHits(sourceType, explanationType, false, addEsMetadataFields); }
public <T, K> Hit<T, K> getFirstHit(Class<T> sourceType, Class<K> explanationType) { Hit<T, K> hit = null; List<Hit<T, K>> hits = getHits(sourceType, explanationType, true); if (!hits.isEmpty()) { hit = hits.get(0); } return hit; }
public ScrollChunk nextChunk() throws IOException { final JestResult search; final List<ResultMessage> hits; if (initialResult == null) { search = getNextScrollResult(); hits = StreamSupport.stream(search.getJsonObject().path("hits").path("hits").spliterator(), false) .map(hit -> ResultMessage.parseFromSource(hit.path("_id").asText(), hit.path("_index").asText(), objectMapper.convertValue(hit.get("_source"), TypeReferences.MAP_STRING_OBJECT))) .collect(Collectors.toList()); } else { // make sure to return the initial hits, see https://github.com/Graylog2/graylog2-server/issues/2126 search = initialResult; hits = initialResult.getHits(Map.class, false).stream() .map(hit -> ResultMessage.parseFromSource(hit.id, hit.index, (Map<String, Object>)hit.source)) .collect(Collectors.toList()); this.initialResult = null; } if (hits.size() == 0) { // scroll exhausted LOG.debug("[{}] Reached end of scroll results.", queryHash, getOriginalQuery()); return null; } LOG.debug("[{}][{}] New scroll id {}, number of hits in chunk: {}", queryHash, chunkId, getScrollIdFromResult(search), hits.size()); scrollId = getScrollIdFromResult(search); // save the id for the next request. return new ScrollChunk(hits, fields, chunkId++); }
@SuppressWarnings("unchecked") public SearchResult search(SearchesConfig config) { final Set<IndexRange> indexRanges = determineAffectedIndicesWithRanges(config.range(), config.filter()); final SearchSourceBuilder requestBuilder = searchRequest(config); if (indexRanges.isEmpty()) { return SearchResult.empty(config.query(), requestBuilder.toString()); } final Set<String> indices = extractIndexNamesFromIndexRanges(indexRanges); final Search.Builder searchBuilder = new Search.Builder(requestBuilder.toString()) .addType(IndexMapping.TYPE_MESSAGE) .addIndex(indices); final io.searchbox.core.SearchResult searchResult = wrapInMultiSearch(searchBuilder.build(), () -> "Unable to perform search query"); final List<ResultMessage> hits = searchResult.getHits(Map.class, false).stream() .map(hit -> ResultMessage.parseFromSource(hit.id, hit.index, (Map<String, Object>) hit.source, hit.highlight)) .collect(Collectors.toList()); recordEsMetrics(searchResult, config.range()); return new SearchResult(hits, searchResult.getTotal(), indexRanges, config.query(), requestBuilder.toString(), tookMsFromSearchResult(searchResult)); }
.build(); final io.searchbox.core.SearchResult searchResponse = wrapInMultiSearch(searchRequest, () -> "Unable to retrieve fields stats"); final List<ResultMessage> hits = searchResponse.getHits(Map.class, false).stream() .map(hit -> ResultMessage.parseFromSource(hit.id, hit.index, (Map<String, Object>) hit.source)) .collect(Collectors.toList());
public <T> List<Hit<T, Void>> getHits(Class<T> sourceType) { return getHits(sourceType, true); }
public <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean addEsMetadataFields) { return getHits(sourceType, explanationType, false, addEsMetadataFields); }
public <T, K> Hit<T, K> getFirstHit(Class<T> sourceType, Class<K> explanationType) { Hit<T, K> hit = null; List<Hit<T, K>> hits = getHits(sourceType, explanationType, true); if (!hits.isEmpty()) { hit = hits.get(0); } return hit; }
private List<String> extractIds(SearchResult result) { List<String> ids = new ArrayList<>(); for (SearchResult.Hit<JsonObject, Void> hit : result.getHits(JsonObject.class)) { if (hit != null) { ids.add(hit.id); } } return ids; } }
public <T, K> Hit<T, K> getFirstHit(Class<T> sourceType, Class<K> explanationType) { Hit<T, K> hit = null; List<Hit<T, K>> hits = getHits(sourceType, explanationType, true); if (!hits.isEmpty()) { hit = hits.get(0); } return hit; }
private List<AlarmFeedback> execute(Search search) throws IOException, FeedbackException { SearchResult result = client.execute(search); if (result == null) { throw new FeedbackException("Failed to get result"); } List<Hit<FeedbackDocument, Void>> feedback = result.getHits(FeedbackDocument.class); if (feedback == null) { return Collections.emptyList(); } return feedback.stream().map(hit -> hit.source).map(FeedbackDocument::toAlarmFeedback).collect(Collectors.toList()); }
@Override public <U> AggregatedPage<U> mapResults(SearchResult response, Class<U> clazz, Pageable pageable) { List<String> result = new ArrayList<>(); for (SearchResult.Hit<JsonObject, Void> searchHit : response.getHits(JsonObject.class)) { result.add(searchHit.source.get(JestResult.ES_METADATA_ID).getAsString()); } if (result.size() > 0) { return new AggregatedPageImpl<U>((List<U>) result, ((ExtendedSearchResult)response).getScrollId()); } return new AggregatedPageImpl<U>(Collections.emptyList(), ((ExtendedSearchResult)response).getScrollId()); }
public <T> AggregatedPage<T> mapResults(SearchResult response, Class<T> clazz, List<AbstractAggregationBuilder> aggregations, Pageable pageable) { LinkedList<T> results = new LinkedList<>(); for (SearchResult.Hit<JsonObject, Void> hit : response.getHits(JsonObject.class)) { if (hit != null) { results.add(mapSource(hit.source, clazz)); } } String scrollId = null; if (response instanceof ExtendedSearchResult) { scrollId = ((ExtendedSearchResult) response).getScrollId(); } return new AggregatedPageImpl<>(results, pageable, response.getTotal(), response.getAggregations(), scrollId); }
@SuppressWarnings("rawtypes") public String getPropertyTechIdByName(String name) { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("name", name)); Search search = new Search.Builder(searchSourceBuilder.toString()) // .addIndex(connection.getIndexName()) // .addType(ElasticConstants.TYPE_PROPERTY) // .build(); List<Hit<Map, Void>> items = connection.search(search).getHits(Map.class); if (null != items && !items.isEmpty()) { return connection.search(search).getHits(Map.class).get(0).source.get(JestResult.ES_METADATA_ID).toString(); } return null; }
public String getFeatureTechId(String uid) { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("uid", uid)); Search search = new Search.Builder(searchSourceBuilder.toString()) // .addIndex(connection.getIndexName()) // .addType(ElasticConstants.TYPE_FEATURE) // .build(); // feature existence must have been checked before (technical function) @SuppressWarnings("rawtypes") List<Hit<Map, Void>> items = connection.search(search).getHits(Map.class); if (null != items && !items.isEmpty()) { return connection.search(search).getHits(Map.class).get(0).source.get(JestResult.ES_METADATA_ID).toString(); } return null; }
/** {@inheritDoc} */ @Override public Map<String, Feature> readGroup(String groupName) { assertGroupExist(groupName); SearchResult result = getConnection().search(getBuilder().queryReadGroup(groupName)); LinkedHashMap<String, Feature> mapOfFeatures = new LinkedHashMap<String, Feature>(); if (null != result && result.isSucceeded()) { for (Hit<Feature, Void> hit : result.getHits(Feature.class)) { mapOfFeatures.put(hit.source.getUid(), hit.source); } } return mapOfFeatures; }
@Override public void purgeAuditTrail(EventQueryDefinition query) { SearchResult result = getConnection().search(getBuilder().queryReadAllEvents(), true); if (result != null && result.isSucceeded()) { for (Hit<Event, Void> event : result.getHits(Event.class)) { getConnection().execute(getBuilder().queryDeleteEvent(event.source.getUuid())); } } }