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);
}
}
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);
}
}
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);
}
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);
}
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());
}
}
}
Aggregations