use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project nuxeo-drive-server by nuxeo.
the class ESAuditChangeFinder method getUpperBound.
@Override
public long getUpperBound() {
SearchRequest request = new SearchRequest(getESIndexName()).types(ElasticSearchConstants.ENTRY_TYPE).searchType(SearchType.DFS_QUERY_THEN_FETCH);
// TODO refactor this to use max clause
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).sort("id", SortOrder.DESC).size(1));
logSearchRequest(request);
SearchResponse searchResponse = getClient().search(request);
logSearchResponse(searchResponse);
List<LogEntry> entries = new ArrayList<>();
SearchHits hits = searchResponse.getHits();
ObjectMapper mapper = new ObjectMapper();
for (SearchHit hit : hits) {
try {
entries.add(mapper.readValue(hit.getSourceAsString(), LogEntryImpl.class));
} catch (IOException e) {
log.error("Error while reading Audit Entry from ES", e);
}
}
return entries.size() > 0 ? entries.get(0).getId() : -1;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project muikku by otavanopisto.
the class ElasticSearchProvider method searchWorkspaces.
@Override
public SearchResult searchWorkspaces(String schoolDataSource, String subjectIdentifier, int courseNumber) {
BoolQueryBuilder query = boolQuery();
query.must(termQuery("published", Boolean.TRUE));
query.must(termQuery("subjectIdentifier", subjectIdentifier));
query.must(termQuery("courseNumber", courseNumber));
// query.must(termQuery("access", WorkspaceAccess.LOGGED_IN));
SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes("Workspace").setFrom(0).setSize(50).setQuery(query);
// logger.log(Level.INFO, "searchWorkspaces query: " + requestBuilder.internalBuilder());
SearchResponse response = requestBuilder.execute().actionGet();
List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
SearchHits searchHits = response.getHits();
SearchHit[] results = searchHits.getHits();
long totalHits = searchHits.getTotalHits();
for (SearchHit hit : results) {
Map<String, Object> hitSource = hit.getSource();
hitSource.put("indexType", hit.getType());
searchResults.add(hitSource);
}
SearchResult result = new SearchResult(0, 50, searchResults, totalHits);
return result;
}
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) {
try {
SearchResponse response = elasticClient.prepareSearch().setQuery(matchAllQuery()).setFrom(start).setSize(maxResults).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 findUser.
@Override
public SearchResult findUser(Long userEntityId, boolean includeInactive) {
// Query that checks activity based on user having a study end date set
BoolQueryBuilder query = boolQuery();
if (!includeInactive) {
query.mustNot(existsQuery("studyEndDate"));
}
query.must(termQuery("userEntityId", userEntityId));
// Search
SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes("User");
// Results processing
SearchResponse response = requestBuilder.setQuery(query).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();
if (hitSource == null) {
hitSource = new HashMap<>();
for (String key : hit.getFields().keySet()) {
hitSource.put(key, hit.getFields().get(key).getValue().toString());
}
}
hitSource.put("indexType", hit.getType());
searchResults.add(hitSource);
}
return new SearchResult(0, searchResults.size(), searchResults, totalHitCount);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project muikku by otavanopisto.
the class ElasticSearchProvider method freeTextSearch.
@Override
public SearchResult freeTextSearch(String text, int start, int maxResults) {
try {
text = sanitizeSearchString(text);
SearchResponse response = elasticClient.prepareSearch().setQuery(matchQuery("_all", text)).setFrom(start).setSize(maxResults).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);
}
}
Aggregations