Search in sources :

Example 6 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class OpenSearchSiteUtil method translateToOpenSearchSort.

public static String translateToOpenSearchSort(SortBy ddfSort) {
    String openSearchSortStr = null;
    String orderType = null;
    if (ddfSort == null || ddfSort.getSortOrder() == null) {
        return openSearchSortStr;
    }
    if (ddfSort.getSortOrder().equals(SortOrder.ASCENDING)) {
        orderType = ORDER_ASCENDING;
    } else {
        orderType = ORDER_DESCENDING;
    }
    // QualifiedString type = ddfSort.getType();
    PropertyName sortByField = ddfSort.getPropertyName();
    if (Result.RELEVANCE.equals(sortByField.getPropertyName())) {
        // asc relevance not supported by spec
        openSearchSortStr = SORT_RELEVANCE + SORT_DELIMITER + ORDER_DESCENDING;
    } else if (Result.TEMPORAL.equals(sortByField.getPropertyName())) {
        openSearchSortStr = SORT_TEMPORAL + SORT_DELIMITER + orderType;
    } else {
        LOGGER.debug("Couldn't determine sort policy, not adding sorting in request to federated site.");
    }
    return openSearchSortStr;
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName)

Example 7 with PropertyName

use of org.opengis.filter.expression.PropertyName 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 8 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class FederationAdminServiceImpl method getRegistryMetacardsByFilter.

private List<Metacard> getRegistryMetacardsByFilter(Filter filter, Set<String> sourceIds) throws FederationAdminException {
    if (filter == null) {
        throw new FederationAdminException("Error getting registry metacards. Null filter provided.");
    }
    PropertyName propertyName = new PropertyNameImpl(Metacard.MODIFIED);
    SortBy sortBy = new SortByImpl(propertyName, SortOrder.ASCENDING);
    QueryImpl query = new QueryImpl(filter);
    query.setSortBy(sortBy);
    query.setPageSize(PAGE_SIZE);
    QueryRequest queryRequest = new QueryRequestImpl(query, sourceIds);
    try {
        QueryResponse queryResponse = security.runWithSubjectOrElevate(() -> catalogFramework.query(queryRequest));
        return queryResponse.getResults().stream().map(Result::getMetacard).filter(Objects::nonNull).collect(Collectors.toList());
    } catch (SecurityServiceException | InvocationTargetException e) {
        String message = "Error querying for registry metacards.";
        LOGGER.debug("{} For Filter: {}", message, filter);
        throw new FederationAdminException(message, e);
    }
}
Also used : FederationAdminException(org.codice.ddf.registry.federationadmin.service.internal.FederationAdminException) PropertyName(org.opengis.filter.expression.PropertyName) SecurityServiceException(ddf.security.service.SecurityServiceException) QueryRequest(ddf.catalog.operation.QueryRequest) SortBy(org.opengis.filter.sort.SortBy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(org.geotools.filter.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) PropertyNameImpl(ddf.catalog.filter.impl.PropertyNameImpl)

Example 9 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class CswQueryFactory method buildSort.

private SortBy buildSort(SortByType sort) throws CswException {
    if (sort == null || sort.getSortProperty() == null) {
        return null;
    }
    SortBy[] sortByArr = parseSortBy(sort);
    if (sortByArr.length > 1) {
        LOGGER.debug("Query request has multiple sort criteria, only primary will be used");
    }
    SortBy sortBy = sortByArr[0];
    if (sortBy.getPropertyName() == null) {
        LOGGER.debug("No property name in primary sort criteria");
        return null;
    }
    if (!attributeRegistry.lookup(sortBy.getPropertyName().getPropertyName()).isPresent() && !DefaultCswRecordMap.hasDefaultMetacardFieldForPrefixedString(sortBy.getPropertyName().getPropertyName(), sortBy.getPropertyName().getNamespaceContext())) {
        throw new CswException("Property " + sortBy.getPropertyName().getPropertyName() + " is not a valid SortBy Field", CswConstants.INVALID_PARAMETER_VALUE, "SortProperty");
    }
    String name = DefaultCswRecordMap.getDefaultMetacardFieldForPrefixedString(sortBy.getPropertyName().getPropertyName(), sortBy.getPropertyName().getNamespaceContext());
    PropertyName propName = new AttributeExpressionImpl(new NameImpl(name));
    return new SortByImpl(propName, sortBy.getSortOrder());
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) NameImpl(org.geotools.feature.NameImpl) AttributeExpressionImpl(org.geotools.filter.AttributeExpressionImpl) SortByImpl(ddf.catalog.filter.impl.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)

Example 10 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class CswRecordMapperFilterVisitor method visit.

@Override
public Object visit(Literal expression, Object extraData) {
    if (extraData != null && extraData instanceof PropertyName && expression.getValue() instanceof String) {
        String propName = ((PropertyName) extraData).getPropertyName();
        AttributeDescriptor attrDesc = metacardType.getAttributeDescriptor(propName);
        if (attrDesc != null && attrDesc.getType() != null) {
            String value = (String) expression.getValue();
            Serializable convertedValue = CswRecordConverter.convertStringValueToMetacardValue(attrDesc.getType().getAttributeFormat(), value);
            return getFactory(extraData).literal(convertedValue);
        }
    }
    return getFactory(extraData).literal(expression.getValue());
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) Serializable(java.io.Serializable) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor)

Aggregations

PropertyName (org.opengis.filter.expression.PropertyName)14 Test (org.junit.Test)5 SortBy (org.opengis.filter.sort.SortBy)5 AttributeExpressionImpl (org.geotools.filter.AttributeExpressionImpl)4 Result (ddf.catalog.data.Result)3 ArrayList (java.util.ArrayList)3 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)3 NameImpl (org.geotools.feature.NameImpl)3 Filter (org.opengis.filter.Filter)3 FilterBuilder (ddf.catalog.filter.FilterBuilder)2 PropertyNameImpl (ddf.catalog.filter.impl.PropertyNameImpl)2 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)2 FuzzyFunction (ddf.catalog.impl.filter.FuzzyFunction)2 QueryRequest (ddf.catalog.operation.QueryRequest)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2 DistanceResultComparator (ddf.catalog.util.impl.DistanceResultComparator)2 RelevanceResultComparator (ddf.catalog.util.impl.RelevanceResultComparator)2 TemporalResultComparator (ddf.catalog.util.impl.TemporalResultComparator)2 Serializable (java.io.Serializable)2