Search in sources :

Example 36 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class ResourceCacheService method queryForMetacard.

private Optional<Metacard> queryForMetacard(String metacardId) {
    Filter filter = frameworkProperties.getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(metacardId);
    QueryRequest queryRequest = new QueryRequestImpl(new QueryImpl(filter), true);
    QueryResponse queryResponse = null;
    try {
        queryResponse = catalogFramework.query(queryRequest);
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Unable to lookup metacard for metacard id [{}].", metacardId);
        return Optional.empty();
    }
    return queryResponse != null && queryResponse.getResults().size() == 1 ? Optional.of(queryResponse.getResults().get(0).getMetacard()) : Optional.empty();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequest(ddf.catalog.operation.QueryRequest) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException)

Example 37 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class FilterPluginTest method testPluginFilter.

@Test
public void testPluginFilter() {
    try {
        QueryResponse response = plugin.processPostQuery(incomingResponse);
        verifyFilterResponse(response);
    } catch (StopProcessingException e) {
        LOGGER.error("Stopped processing the redaction plugin", e);
    }
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Test(org.junit.Test)

Example 38 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class FilterPluginTest method testPluginFilterNoStrategies.

@Test
public void testPluginFilterNoStrategies() {
    plugin = new FilterPlugin();
    try {
        QueryResponse response = plugin.processPostQuery(incomingResponse);
        verifyFilterResponse(response);
    } catch (StopProcessingException e) {
        LOGGER.error("Stopped processing the redaction plugin", e);
    }
}
Also used : FilterPlugin(ddf.catalog.security.filter.plugin.FilterPlugin) QueryResponse(ddf.catalog.operation.QueryResponse) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Test(org.junit.Test)

Example 39 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class QueryOperations method doQuery.

/**
     * Executes a query using the specified {@link QueryRequest} and {@link FederationStrategy}.
     * Based on the isEnterprise and sourceIds list in the query request, the federated query may
     * include the local provider and {@link ConnectedSource}s.
     *
     * @param queryRequest the {@link QueryRequest}
     * @param strategy     the {@link FederationStrategy}
     * @return the {@link QueryResponse}
     * @throws FederationException
     */
QueryResponse doQuery(QueryRequest queryRequest, FederationStrategy strategy) throws FederationException {
    Set<String> sourceIds = getCombinedIdSet(queryRequest);
    LOGGER.debug("source ids: {}", sourceIds);
    QuerySources querySources = new QuerySources(frameworkProperties).initializeSources(this, queryRequest, sourceIds).addConnectedSources(this, frameworkProperties).addCatalogProvider(this);
    if (querySources.isEmpty()) {
        // TODO change to SourceUnavailableException
        throw new FederationException("SiteNames could not be resolved due to invalid site names, none of the sites " + "were available, or the current subject doesn't have permission to access the sites.");
    }
    LOGGER.debug("Calling strategy.federate()");
    Query originalQuery = queryRequest.getQuery();
    if (originalQuery != null && originalQuery.getTimeoutMillis() <= 0) {
        Query modifiedQuery = new QueryImpl(originalQuery, originalQuery.getStartIndex(), originalQuery.getPageSize(), originalQuery.getSortBy(), originalQuery.requestsTotalResultsCount(), queryTimeoutMillis);
        queryRequest = new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), queryRequest.getSourceIds(), queryRequest.getProperties());
    }
    QueryResponse response = strategy.federate(querySources.sourcesToQuery, queryRequest);
    frameworkProperties.getQueryResponsePostProcessor().processResponse(response);
    return addProcessingDetails(querySources.exceptions, response);
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Query(ddf.catalog.operation.Query) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) FederationException(ddf.catalog.federation.FederationException)

Example 40 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class SortedQueryMonitor method run.

@Override
public void run() {
    SortBy sortBy = query.getSortBy();
    // Prepare the Comparators that we will use
    Comparator<Result> coreComparator = CachingFederationStrategy.DEFAULT_COMPARATOR;
    if (sortBy != null && sortBy.getPropertyName() != null) {
        PropertyName sortingProp = sortBy.getPropertyName();
        String sortType = sortingProp.getPropertyName();
        SortOrder sortOrder = (sortBy.getSortOrder() == null) ? SortOrder.DESCENDING : sortBy.getSortOrder();
        LOGGER.debug("Sorting type: {}", sortType);
        LOGGER.debug("Sorting order: {}", sortBy.getSortOrder());
        // Temporal searches are currently sorted by the effective time
        if (Metacard.EFFECTIVE.equals(sortType) || Result.TEMPORAL.equals(sortType)) {
            coreComparator = new TemporalResultComparator(sortOrder);
        } else if (Result.DISTANCE.equals(sortType)) {
            coreComparator = new DistanceResultComparator(sortOrder);
        } else if (Result.RELEVANCE.equals(sortType)) {
            coreComparator = new RelevanceResultComparator(sortOrder);
        }
    }
    List<Result> resultList = new ArrayList<>();
    long totalHits = 0;
    Set<ProcessingDetails> processingDetails = returnResults.getProcessingDetails();
    Map<String, Serializable> returnProperties = returnResults.getProperties();
    HashMap<String, Long> hitsPerSource = new HashMap<>();
    for (int i = futures.size(); i > 0; i--) {
        String sourceId = "Unknown Source";
        QueryRequest queryRequest = null;
        SourceResponse sourceResponse = null;
        try {
            Future<SourceResponse> future;
            if (query.getTimeoutMillis() < 1) {
                future = completionService.take();
            } else {
                future = completionService.poll(getTimeRemaining(deadline), TimeUnit.MILLISECONDS);
                if (future == null) {
                    timeoutRemainingSources(processingDetails);
                    break;
                }
            }
            queryRequest = futures.remove(future);
            sourceId = getSourceIdFromRequest(queryRequest);
            sourceResponse = future.get();
            if (sourceResponse == null) {
                LOGGER.debug("Source {} returned null response", sourceId);
                executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, new NullPointerException(), processingDetails);
            } else {
                sourceResponse = executePostFederationQueryPlugins(sourceResponse, queryRequest);
                resultList.addAll(sourceResponse.getResults());
                long hits = sourceResponse.getHits();
                totalHits += hits;
                hitsPerSource.merge(sourceId, hits, (l1, l2) -> l1 + l2);
                Map<String, Serializable> properties = sourceResponse.getProperties();
                returnProperties.putAll(properties);
            }
        } catch (InterruptedException e) {
            if (queryRequest != null) {
                // First, add interrupted processing detail for this source
                LOGGER.debug("Search interrupted for {}", sourceId);
                executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, e, processingDetails);
            }
            // Then add the interrupted exception for the remaining sources
            interruptRemainingSources(processingDetails, e);
            break;
        } catch (ExecutionException e) {
            LOGGER.info("Couldn't get results from completed federated query. {}, {}", sourceId, Exceptions.getFullMessage(e), e);
            executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, e, processingDetails);
        }
    }
    returnProperties.put("hitsPerSource", hitsPerSource);
    LOGGER.debug("All sources finished returning results: {}", resultList.size());
    returnResults.setHits(totalHits);
    if (CachingFederationStrategy.INDEX_QUERY_MODE.equals(request.getPropertyValue(CachingFederationStrategy.QUERY_MODE))) {
        QueryResponse result = cachingFederationStrategy.queryCache(request);
        returnResults.addResults(result.getResults(), true);
    } else {
        returnResults.addResults(sortedResults(resultList, coreComparator), true);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SortBy(org.opengis.filter.sort.SortBy) ArrayList(java.util.ArrayList) Result(ddf.catalog.data.Result) ProcessingDetails(ddf.catalog.operation.ProcessingDetails) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) ExecutionException(java.util.concurrent.ExecutionException) PropertyName(org.opengis.filter.expression.PropertyName) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) SortOrder(org.opengis.filter.sort.SortOrder) TemporalResultComparator(ddf.catalog.util.impl.TemporalResultComparator) QueryResponse(ddf.catalog.operation.QueryResponse) RelevanceResultComparator(ddf.catalog.util.impl.RelevanceResultComparator) DistanceResultComparator(ddf.catalog.util.impl.DistanceResultComparator)

Aggregations

QueryResponse (ddf.catalog.operation.QueryResponse)120 QueryRequest (ddf.catalog.operation.QueryRequest)87 Test (org.junit.Test)74 Metacard (ddf.catalog.data.Metacard)67 Result (ddf.catalog.data.Result)54 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)52 ArrayList (java.util.ArrayList)51 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)34 QueryImpl (ddf.catalog.operation.impl.QueryImpl)34 FederationException (ddf.catalog.federation.FederationException)33 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)30 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)24 HashSet (java.util.HashSet)24 Filter (org.opengis.filter.Filter)24 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)23 Source (ddf.catalog.source.Source)22 ResultImpl (ddf.catalog.data.impl.ResultImpl)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 InputStream (java.io.InputStream)19