Search in sources :

Example 51 with QueryRequest

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

the class ValidationQueryFactoryTest method testSearchAnyAndShow.

@Test
public void testSearchAnyAndShow() throws StopProcessingException, PluginExecutionException, UnsupportedQueryException {
    QueryImpl query = new QueryImpl(filterBuilder.attribute(Metacard.MODIFIED).is().equalTo().text("sample"));
    assertThat(filterAdapter.adapt(query, testValidationQueryDelegate), is(false));
    QueryRequest sendQuery = new QueryRequestImpl(query);
    QueryRequest returnQuery = validationQueryFactory.getQueryRequestWithValidationFilter(sendQuery, true, true);
    assertThat(filterAdapter.adapt(returnQuery.getQuery(), testValidationQueryDelegate), is(false));
    assertThat(sendQuery, is(returnQuery));
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 52 with QueryRequest

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

the class SolrCacheTest method query.

@Test
public void query() throws UnsupportedQueryException {
    QueryRequest mockQuery = mock(QueryRequest.class);
    SourceResponse expectedResponse = new QueryResponseImpl(mockQuery);
    when(mockCacheSolrMetacardClient.query(mockQuery)).thenReturn(expectedResponse);
    SourceResponse actualResponse = solrCache.query(mockQuery);
    assertThat(actualResponse, is(expectedResponse));
    verify(mockSolrClientAdaptor).getSolrMetacardClient();
    verify(mockCacheSolrMetacardClient).query(mockQuery);
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) Test(org.junit.Test)

Example 53 with QueryRequest

use of ddf.catalog.operation.QueryRequest 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)

Example 54 with QueryRequest

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

the class SortedQueryMonitor method timeoutRemainingSources.

private void timeoutRemainingSources(Set<ProcessingDetails> processingDetails) {
    for (QueryRequest expiredSource : futures.values()) {
        if (expiredSource != null) {
            String sourceId = getSourceIdFromRequest(expiredSource);
            LOGGER.info("Search timed out for {}", sourceId);
            processingDetails.add(new ProcessingDetailsImpl(sourceId, new TimeoutException()));
        }
    }
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) ProcessingDetailsImpl(ddf.catalog.operation.impl.ProcessingDetailsImpl) TimeoutException(java.util.concurrent.TimeoutException)

Example 55 with QueryRequest

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

the class CachingFederationStrategyTest method testFederateQueryNoUpdateToCache.

@Test
public void testFederateQueryNoUpdateToCache() throws Exception {
    properties.put(QUERY_MODE, NATIVE_QUERY_MODE);
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, properties);
    Source mockSource = mock(Source.class);
    when(mockSource.query(any(QueryRequest.class))).thenReturn(mockResponse);
    QueryResponse federateResponse = strategy.federate(Arrays.asList(mockSource), fedQueryRequest);
    assertThat(requestArgumentCaptor.getValue().getPropertyValue(QUERY_MODE), is(NATIVE_QUERY_MODE));
    verify(mockSource).query(any(QueryRequest.class));
    verify(cache, times(0)).query(any(QueryRequest.class));
    verifyCacheNotUpdated();
    assertThat(federateResponse.getRequest().getQuery(), is(requestArgumentCaptor.getValue().getQuery()));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Source(ddf.catalog.source.Source) Test(org.junit.Test)

Aggregations

QueryRequest (ddf.catalog.operation.QueryRequest)153 Test (org.junit.Test)98 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)86 QueryImpl (ddf.catalog.operation.impl.QueryImpl)66 QueryResponse (ddf.catalog.operation.QueryResponse)57 ArrayList (java.util.ArrayList)41 SourceResponse (ddf.catalog.operation.SourceResponse)39 Metacard (ddf.catalog.data.Metacard)33 Result (ddf.catalog.data.Result)31 Filter (org.opengis.filter.Filter)31 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)30 Query (ddf.catalog.operation.Query)29 Source (ddf.catalog.source.Source)24 FederationException (ddf.catalog.federation.FederationException)20 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)15 InputStream (java.io.InputStream)15 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)14 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)13 SortByImpl (ddf.catalog.filter.impl.SortByImpl)11 HashMap (java.util.HashMap)11