public SearchResponse search(SearchRequest searchRequest, String fieldList) throws InvalidSearchException { if (searchRequest.getQuery() == null) { throw new InvalidSearchException("Search query is invalid: null"); } if (client == null) { throw new InvalidSearchException("Uninitialized Dao! You must call init() prior to use."); } if (searchRequest.getSize() > accessConfig.getMaxSearchResults()) { throw new InvalidSearchException( "Search result size must be less than " + accessConfig.getMaxSearchResults()); } try { SolrQuery query = buildSearchRequest(searchRequest, fieldList); QueryResponse response = client.query(query); return buildSearchResponse(searchRequest, response); } catch (SolrException | IOException | SolrServerException e) { String msg = e.getMessage(); LOG.error(msg, e); throw new InvalidSearchException(msg, e); } }
/** * Defers to a provided {@link org.elasticsearch.index.query.QueryBuilder} for the query. * @param groupRequest The request defining the parameters of the grouping * @param queryBuilder The actual query to be run. Intended for if the SearchRequest requires wrapping * @return The results of the query * @throws InvalidSearchException When the query is malformed or the current state doesn't allow search */ protected GroupResponse group(GroupRequest groupRequest, QueryBuilder queryBuilder) throws InvalidSearchException { org.elasticsearch.action.search.SearchRequest esRequest; org.elasticsearch.action.search.SearchResponse esResponse; if (client == null) { throw new InvalidSearchException("Uninitialized Dao! You must call init() prior to use."); } if (groupRequest.getGroups() == null || groupRequest.getGroups().size() == 0) { throw new InvalidSearchException("At least 1 group must be provided."); } esRequest = buildGroupRequest(groupRequest, queryBuilder); esResponse = requestSubmitter.submitSearch(esRequest); GroupResponse response = buildGroupResponse(groupRequest, esResponse); return response; }
@Override public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException { if(searchRequest.getQuery() == null) { throw new InvalidSearchException("Search query is invalid: null"); } return search(searchRequest, new QueryStringQueryBuilder(searchRequest.getQuery())); }
/** * Defers to a provided {@link org.elasticsearch.index.query.QueryBuilder} for the query. * @param request The request defining the parameters of the search * @param queryBuilder The actual query to be run. Intended for if the SearchRequest requires wrapping * @return The results of the query * @throws InvalidSearchException When the query is malformed or the current state doesn't allow search */ protected SearchResponse search(SearchRequest request, QueryBuilder queryBuilder) throws InvalidSearchException { org.elasticsearch.action.search.SearchRequest esRequest; org.elasticsearch.action.search.SearchResponse esResponse; if(client == null) { throw new InvalidSearchException("Uninitialized Dao! You must call init() prior to use."); } if (request.getSize() > accessConfig.getMaxSearchResults()) { throw new InvalidSearchException("Search result size must be less than " + accessConfig.getMaxSearchResults()); } esRequest = buildSearchRequest(request, queryBuilder); esResponse = requestSubmitter.submitSearch(esRequest); return buildSearchResponse(request, esResponse); }
@Override public SearchResponse getAllMetaAlertsForAlert(String guid) throws InvalidSearchException { SearchRequest request; try { String replacedQuery = metaAlertsForAlertQuery.replace("${GUID}", guid); request = JSONUtils.INSTANCE.load(replacedQuery, SearchRequest.class); } catch (IOException e) { throw new InvalidSearchException("Unable to process query:", e); } return search(request); }
ElasticsearchUtils.toJSON(request).orElse("???")); LOG.error(msg, e); throw new InvalidSearchException(msg, e); esResponse.status(), esResponse.isTimedOut(), esResponse.isTerminatedEarly()); LOG.error(msg); throw new InvalidSearchException(msg);
@Override public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException { try { String groupNames = groupRequest.getGroups().stream().map(Group::getField).collect( Collectors.joining(",")); SolrQuery query = new SolrQuery() .setStart(0) .setRows(0) .setQuery(groupRequest.getQuery()); query.set("collection", getCollections(groupRequest.getIndices())); Optional<String> scoreField = groupRequest.getScoreField(); if (scoreField.isPresent()) { query.set("stats", true); query.set("stats.field", String.format("{!tag=piv1 sum=true}%s", scoreField.get())); } query.set("facet", true); query.set("facet.pivot", String.format("{!stats=piv1}%s", groupNames)); QueryResponse response = client.query(query); return buildGroupResponse(groupRequest, response); } catch (IOException | SolrServerException e) { String msg = e.getMessage(); LOG.error(msg, e); throw new InvalidSearchException(msg, e); } }
/** * Build a group response. * @param groupRequest The original group request. * @param response The search response. * @return A group response. * @throws InvalidSearchException */ private GroupResponse buildGroupResponse( GroupRequest groupRequest, org.elasticsearch.action.search.SearchResponse response) throws InvalidSearchException { // build the search response Map<String, FieldType> commonColumnMetadata; try { commonColumnMetadata = columnMetadataDao.getColumnMetadata(groupRequest.getIndices()); } catch (IOException e) { throw new InvalidSearchException(String.format("Could not get common column metadata for indices %s", Arrays.toString(groupRequest.getIndices().toArray()))); } GroupResponse groupResponse = new GroupResponse(); groupResponse.setGroupedBy(groupRequest.getGroups().get(0).getField()); groupResponse.setGroupResults(getGroupResults(groupRequest, 0, response.getAggregations(), commonColumnMetadata)); return groupResponse; }
commonColumnMetadata = columnMetadataDao.getColumnMetadata(searchRequest.getIndices()); } catch (IOException e) { throw new InvalidSearchException(String.format( "Could not get common column metadata for indices %s", Arrays.toString(searchRequest.getIndices().toArray())));
throw new InvalidSearchException("Error when retrieving child alerts for metaalerts", e);
@Override public SearchResponse getAllMetaAlertsForAlert(String guid) throws InvalidSearchException { if (guid == null || guid.trim().isEmpty()) { throw new InvalidSearchException("Guid cannot be empty"); throw new InvalidSearchException("Unable to complete search", e);
@Override public SearchResponse getAllMetaAlertsForAlert(String guid) throws InvalidSearchException, IOException { if (guid == null || guid.trim().isEmpty()) { throw new InvalidSearchException("Guid cannot be empty"); } // Searches for all alerts containing the meta alert guid in it's "metalerts" array QueryBuilder qb = boolQuery() .must( nestedQuery( MetaAlertConstants.ALERT_FIELD, boolQuery() .must(termQuery(MetaAlertConstants.ALERT_FIELD + "." + GUID, guid)), ScoreMode.None ).innerHit(new InnerHitBuilder()) ) .must(termQuery(MetaAlertConstants.STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString())); return queryAllResults(elasticsearchDao.getClient().getHighLevelClient(), qb, config.getMetaAlertIndex(), pageSize); } }
@Override public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException { if(config.getMaxSearchResults() != null && searchRequest.getSize() > config.getMaxSearchResults()) { throw new InvalidSearchException("Search result size must be less than " + config.getMaxSearchResults());
meta = columnMetadataDao.getColumnMetadata(searchRequest.getIndices()); } catch(IOException e) { throw new InvalidSearchException("Unable to get column metadata", e);