Search in sources :

Example 1 with SortOrder

use of org.opengis.filter.sort.SortOrder in project ddf by codice.

the class QueryRunnable method getResultComparator.

protected Comparator<Result> getResultComparator(Query query) {
    Comparator<Result> sortComparator = new RelevanceResultComparator(SortOrder.DESCENDING);
    SortBy sortBy = query.getSortBy();
    if (sortBy != null && sortBy.getPropertyName() != null) {
        PropertyName sortingProp = sortBy.getPropertyName();
        String sortType = sortingProp.getPropertyName();
        SortOrder sortOrder = (sortBy.getSortOrder() == null) ? SortOrder.DESCENDING : sortBy.getSortOrder();
        // Temporal searches are currently sorted by the effective time
        if (Metacard.EFFECTIVE.equals(sortType) || Result.TEMPORAL.equals(sortType)) {
            sortComparator = new TemporalResultComparator(sortOrder);
        } else if (Metacard.CREATED.equals(sortType) || Metacard.MODIFIED.equals(sortType)) {
            sortComparator = new TemporalResultComparator(sortOrder, sortType);
        } else if (Result.DISTANCE.equals(sortType)) {
            sortComparator = new DistanceResultComparator(sortOrder);
        } else if (Result.RELEVANCE.equals(sortType)) {
            sortComparator = new RelevanceResultComparator(sortOrder);
        }
    }
    return sortComparator;
}
Also used : TemporalResultComparator(ddf.catalog.util.impl.TemporalResultComparator) PropertyName(org.opengis.filter.expression.PropertyName) SortBy(org.opengis.filter.sort.SortBy) SortOrder(org.opengis.filter.sort.SortOrder) RelevanceResultComparator(ddf.catalog.util.impl.RelevanceResultComparator) DistanceResultComparator(ddf.catalog.util.impl.DistanceResultComparator) Result(ddf.catalog.data.Result)

Example 2 with SortOrder

use of org.opengis.filter.sort.SortOrder in project ddf by codice.

the class WfsSource method buildGetFeatureRequest.

protected GetFeatureType buildGetFeatureRequest(Query query) throws UnsupportedQueryException {
    List<ContentType> contentTypes = getContentTypesFromQuery(query);
    List<QueryType> queries = new ArrayList<QueryType>();
    for (Entry<QName, WfsFilterDelegate> filterDelegateEntry : featureTypeFilters.entrySet()) {
        if (contentTypes.isEmpty() || isFeatureTypeInQuery(contentTypes, filterDelegateEntry.getKey().getLocalPart())) {
            QueryType wfsQuery = new QueryType();
            String typeName = null;
            if (StringUtils.isEmpty(filterDelegateEntry.getKey().getPrefix())) {
                typeName = filterDelegateEntry.getKey().getLocalPart();
            } else {
                typeName = filterDelegateEntry.getKey().getPrefix() + ":" + filterDelegateEntry.getKey().getLocalPart();
            }
            if (StringUtils.isNotBlank(srsName)) {
                wfsQuery.setSrsName(srsName);
            }
            wfsQuery.setTypeNames(Arrays.asList(typeName));
            wfsQuery.setHandle(filterDelegateEntry.getKey().getLocalPart());
            FilterType filter = filterAdapter.adapt(query, filterDelegateEntry.getValue());
            if (filter != null) {
                if (areAnyFiltersSet(filter)) {
                    wfsQuery.setAbstractSelectionClause(new net.opengis.filter.v_2_0_0.ObjectFactory().createFilter(filter));
                }
                if (!this.disableSorting) {
                    if (query.getSortBy() != null) {
                        SortOrder sortOrder = query.getSortBy().getSortOrder();
                        if (filterDelegateEntry.getValue().isSortingSupported() && filterDelegateEntry.getValue().getAllowedSortOrders().contains(sortOrder)) {
                            JAXBElement<SortByType> sortBy = buildSortBy(filterDelegateEntry.getKey(), query.getSortBy());
                            if (sortBy != null) {
                                LOGGER.debug("Sorting using sort order of [{}].", sortOrder.identifier());
                                wfsQuery.setAbstractSortingClause(sortBy);
                            }
                        } else if (filterDelegateEntry.getValue().isSortingSupported() && CollectionUtils.isEmpty(filterDelegateEntry.getValue().getAllowedSortOrders())) {
                            JAXBElement<SortByType> sortBy = buildSortBy(filterDelegateEntry.getKey(), query.getSortBy());
                            if (sortBy != null) {
                                LOGGER.debug("No sort orders defined in getCapabilities.  Attempting to sort using sort order of [{}].", sortOrder.identifier());
                                wfsQuery.setAbstractSortingClause(sortBy);
                            }
                        } else if (filterDelegateEntry.getValue().isSortingSupported() && !filterDelegateEntry.getValue().getAllowedSortOrders().contains(sortOrder)) {
                            LOGGER.debug("Unsupported sort order of [{}]. Supported sort orders are {}.", sortOrder, filterDelegateEntry.getValue().getAllowedSortOrders());
                        } else if (!filterDelegateEntry.getValue().isSortingSupported()) {
                            LOGGER.debug("Sorting is not supported.");
                        }
                    }
                } else {
                    LOGGER.debug("Sorting is disabled.");
                }
                queries.add(wfsQuery);
            } else {
                LOGGER.debug("WFS Source {}: {} has an invalid filter.", getId(), filterDelegateEntry.getKey());
            }
        }
    }
    if (queries != null && !queries.isEmpty()) {
        GetFeatureType getFeatureType = new GetFeatureType();
        int pageSize = query.getPageSize();
        if (pageSize < 0) {
            LOGGER.debug("Page size has a negative value");
            throw new UnsupportedQueryException("Unable to build query. Page size has a negative value.");
        }
        int startIndex = query.getStartIndex();
        if (startIndex < 0) {
            LOGGER.debug("Start index has a negative value");
            throw new UnsupportedQueryException("Unable to build query. Start index has a negative value.");
        } else if (startIndex != 0) {
            //Convert DDF index of 1 back to index of 0 for WFS 2.0
            startIndex = query.getStartIndex() - 1;
        } else {
            LOGGER.debug("Query already has a start index of 0");
        }
        getFeatureType.setCount(BigInteger.valueOf(query.getPageSize()));
        getFeatureType.setStartIndex(BigInteger.valueOf(startIndex));
        List<JAXBElement<?>> incomingQueries = getFeatureType.getAbstractQueryExpression();
        for (QueryType queryType : queries) {
            incomingQueries.add(new net.opengis.wfs.v_2_0_0.ObjectFactory().createQuery(queryType));
        }
        logMessage(getFeatureType);
        return getFeatureType;
    } else {
        throw new UnsupportedQueryException("Unable to build query. No filters could be created from query criteria.");
    }
}
Also used : ContentType(ddf.catalog.data.ContentType) QName(javax.xml.namespace.QName) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) SortOrder(org.opengis.filter.sort.SortOrder) SortByType(net.opengis.filter.v_2_0_0.SortByType) JAXBElement(javax.xml.bind.JAXBElement) FilterType(net.opengis.filter.v_2_0_0.FilterType) QueryType(net.opengis.wfs.v_2_0_0.QueryType) GetFeatureType(net.opengis.wfs.v_2_0_0.GetFeatureType)

Example 3 with SortOrder

use of org.opengis.filter.sort.SortOrder in project ddf by codice.

the class WfsSource method buildSortBy.

private JAXBElement<SortByType> buildSortBy(QName featureType, SortBy incomingSortBy) throws UnsupportedQueryException {
    net.opengis.filter.v_2_0_0.ObjectFactory filterObjectFactory = new net.opengis.filter.v_2_0_0.ObjectFactory();
    String propertyName = mapSortByPropertyName(featureType, incomingSortBy.getPropertyName().getPropertyName());
    if (propertyName != null) {
        SortOrder sortOrder = incomingSortBy.getSortOrder();
        SortPropertyType sortPropertyType = filterObjectFactory.createSortPropertyType();
        sortPropertyType.setValueReference(propertyName);
        if (SortOrder.ASCENDING.equals(sortOrder)) {
            sortPropertyType.setSortOrder(SortOrderType.ASC);
        } else if (SortOrder.DESCENDING.equals(sortOrder)) {
            sortPropertyType.setSortOrder(SortOrderType.DESC);
        } else {
            throw new UnsupportedQueryException("Unable to build query. Unknown sort order of [" + sortOrder.identifier() + "].");
        }
        SortByType sortByType = filterObjectFactory.createSortByType();
        sortByType.getSortProperty().add(sortPropertyType);
        return filterObjectFactory.createSortBy(sortByType);
    } else {
        return null;
    }
}
Also used : UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) SortOrder(org.opengis.filter.sort.SortOrder) SortPropertyType(net.opengis.filter.v_2_0_0.SortPropertyType) SortByType(net.opengis.filter.v_2_0_0.SortByType)

Example 4 with SortOrder

use of org.opengis.filter.sort.SortOrder 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

SortOrder (org.opengis.filter.sort.SortOrder)4 Result (ddf.catalog.data.Result)2 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)2 DistanceResultComparator (ddf.catalog.util.impl.DistanceResultComparator)2 RelevanceResultComparator (ddf.catalog.util.impl.RelevanceResultComparator)2 TemporalResultComparator (ddf.catalog.util.impl.TemporalResultComparator)2 ArrayList (java.util.ArrayList)2 SortByType (net.opengis.filter.v_2_0_0.SortByType)2 PropertyName (org.opengis.filter.expression.PropertyName)2 SortBy (org.opengis.filter.sort.SortBy)2 ContentType (ddf.catalog.data.ContentType)1 ProcessingDetails (ddf.catalog.operation.ProcessingDetails)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 SourceResponse (ddf.catalog.operation.SourceResponse)1 PluginExecutionException (ddf.catalog.plugin.PluginExecutionException)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 JAXBElement (javax.xml.bind.JAXBElement)1