Search in sources :

Example 1 with CQL

use of org.geotools.filter.text.cql2.CQL in project ddf by codice.

the class SearchService method executeQuery.

/**
     * Creates the query requests for each source and hands off the query to the Search Controller
     *
     * @param queryMessage
     *            - JSON message received from cometd
     */
public void executeQuery(Map<String, Object> queryMessage, Subject subject) {
    String sources = castObject(String.class, queryMessage.get(SOURCES));
    Long maxTimeout = castObject(Long.class, queryMessage.get(MAX_TIMEOUT));
    Long startIndex = castObject(Long.class, queryMessage.get(START_INDEX));
    Long count = castObject(Long.class, queryMessage.get(COUNT));
    String cql = castObject(String.class, queryMessage.get(CQL_FILTER));
    String sort = castObject(String.class, queryMessage.get(SORT));
    String id = castObject(String.class, queryMessage.get(ID));
    Set<String> sourceIds = getSourceIds(sources);
    Filter filter = null;
    try {
        if (StringUtils.isNotBlank(cql)) {
            filter = ECQL.toFilter(cql);
        }
    } catch (CQLException e) {
        LOGGER.debug("Unable to parse CQL filter", e);
        return;
    }
    Query query = createQuery(filter, startIndex, count, sort, maxTimeout);
    SearchRequest searchRequest = new SearchRequest(sourceIds, query, id);
    try {
        // Hand off to the search controller for the actual query
        searchController.executeQuery(searchRequest, serverSession, subject);
    } catch (Exception e) {
        LOGGER.debug("Exception while executing a query", e);
    }
}
Also used : SearchRequest(org.codice.ddf.ui.searchui.query.model.SearchRequest) Query(ddf.catalog.operation.Query) Filter(org.opengis.filter.Filter) CQLException(org.geotools.filter.text.cql2.CQLException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CQLException(org.geotools.filter.text.cql2.CQLException)

Example 2 with CQL

use of org.geotools.filter.text.cql2.CQL in project ddf by codice.

the class PersistentStoreImpl method get.

@Override
public // Returned Map will have suffixes in the key names - client is responsible for handling them
List<Map<String, Object>> get(String type, String cql) throws PersistenceException {
    if (StringUtils.isBlank(type)) {
        throw new PersistenceException("The type of object(s) to retrieve must be non-null and not blank, e.g., notification, metacard, etc.");
    }
    List<Map<String, Object>> results = new ArrayList<>();
    // Set Solr Core name to type and create/connect to Solr Core
    SolrClient solrClient = getSolrClient(type);
    if (solrClient == null) {
        throw new PersistenceException("Unable to create Solr client.");
    }
    SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrClient, type);
    try {
        SolrQuery solrQuery;
        // If not cql specified, then return all items
        if (StringUtils.isBlank(cql)) {
            solrQuery = new SolrQuery("*:*");
        } else {
            Filter filter = CQL.toFilter(cql);
            solrQuery = (SolrQuery) filter.accept(visitor, null);
        }
        QueryResponse solrResponse = solrClient.query(solrQuery, METHOD.POST);
        long numResults = solrResponse.getResults().getNumFound();
        LOGGER.debug("numResults = {}", numResults);
        SolrDocumentList docs = solrResponse.getResults();
        for (SolrDocument doc : docs) {
            PersistentItem result = new PersistentItem();
            Collection<String> fieldNames = doc.getFieldNames();
            for (String name : fieldNames) {
                LOGGER.debug("field name = {} has value = {}", name, doc.getFieldValue(name));
                if (name.endsWith(PersistentItem.TEXT_SUFFIX) && doc.getFieldValues(name).size() > 1) {
                    result.addProperty(name, doc.getFieldValues(name).stream().filter(s -> s instanceof String).map(s -> (String) s).collect(Collectors.toSet()));
                } else if (name.endsWith(PersistentItem.XML_SUFFIX)) {
                    result.addXmlProperty(name, (String) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.TEXT_SUFFIX)) {
                    result.addProperty(name, (String) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.LONG_SUFFIX)) {
                    result.addProperty(name, (Long) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.INT_SUFFIX)) {
                    result.addProperty(name, (Integer) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.DATE_SUFFIX)) {
                    result.addProperty(name, (Date) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.BINARY_SUFFIX)) {
                    result.addProperty(name, (byte[]) doc.getFirstValue(name));
                } else {
                    LOGGER.debug("Not adding field {} because it has invalid suffix", name);
                }
            }
            results.add(result);
        }
    } catch (CQLException e) {
        throw new PersistenceException("CQLException while getting Solr data with cql statement " + cql, e);
    } catch (SolrServerException | IOException e) {
        throw new PersistenceException("SolrServerException while getting Solr data with cql statement " + cql, e);
    }
    return results;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) SolrClientFactoryImpl(org.codice.solr.factory.impl.SolrClientFactoryImpl) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) ArrayList(java.util.ArrayList) PersistenceException(org.codice.ddf.persistence.PersistenceException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Future(java.util.concurrent.Future) METHOD(org.apache.solr.client.solrj.SolrRequest.METHOD) CQL(org.geotools.filter.text.cql2.CQL) SolrQueryFilterVisitor(org.codice.solr.query.SolrQueryFilterVisitor) Map(java.util.Map) CQLException(org.geotools.filter.text.cql2.CQLException) Logger(org.slf4j.Logger) PersistentItem(org.codice.ddf.persistence.PersistentItem) Collection(java.util.Collection) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SolrClient(org.apache.solr.client.solrj.SolrClient) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SolrDocument(org.apache.solr.common.SolrDocument) List(java.util.List) PersistentStore(org.codice.ddf.persistence.PersistentStore) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Filter(org.opengis.filter.Filter) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) PersistentItem(org.codice.ddf.persistence.PersistentItem) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) SolrQueryFilterVisitor(org.codice.solr.query.SolrQueryFilterVisitor) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Date(java.util.Date) SolrDocument(org.apache.solr.common.SolrDocument) SolrClient(org.apache.solr.client.solrj.SolrClient) Filter(org.opengis.filter.Filter) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) PersistenceException(org.codice.ddf.persistence.PersistenceException) CQLException(org.geotools.filter.text.cql2.CQLException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with CQL

use of org.geotools.filter.text.cql2.CQL in project ddf by codice.

the class CqlRequest method createFilter.

private Filter createFilter(FilterBuilder filterBuilder) {
    Filter filter = null;
    try {
        filter = ECQL.toFilter(cql);
    } catch (CQLException e) {
        halt(400, "Unable to parse CQL filter");
    }
    if (filter == null) {
        LOGGER.debug("Received an empty filter. Using a wildcard contextual filter instead.");
        filter = filterBuilder.attribute(Metacard.ANY_TEXT).is().like().text(FilterDelegate.WILDCARD_CHAR);
    }
    return filter;
}
Also used : Filter(org.opengis.filter.Filter) CQLException(org.geotools.filter.text.cql2.CQLException)

Example 4 with CQL

use of org.geotools.filter.text.cql2.CQL in project ddf by codice.

the class CswQueryFactory method buildFilter.

private CswRecordMapperFilterVisitor buildFilter(QueryConstraintType constraint) throws CswException {
    CswRecordMapperFilterVisitor visitor = new CswRecordMapperFilterVisitor(metacardType, metacardTypes);
    Filter filter = null;
    if (constraint != null) {
        if (constraint.isSetCqlText()) {
            try {
                filter = CQL.toFilter(constraint.getCqlText());
            } catch (CQLException e) {
                throw new CswException("Unable to parse CQL Constraint: " + e.getMessage(), e);
            }
        } else if (constraint.isSetFilter()) {
            FilterType constraintFilter = constraint.getFilter();
            filter = parseFilter(constraintFilter);
        }
    } else {
        // not supported by catalog:
        //filter = Filter.INCLUDE;
        filter = builder.attribute(Core.ID).is().like().text(FilterDelegate.WILDCARD_CHAR);
    }
    if (filter == null) {
        throw new CswException("Invalid Filter Expression", CswConstants.NO_APPLICABLE_CODE, null);
    }
    filter = transformCustomFunctionToFilter(filter);
    try {
        visitor.setVisitedFilter((Filter) filter.accept(visitor, new FilterFactoryImpl()));
    } catch (UnsupportedOperationException ose) {
        throw new CswException(ose.getMessage(), CswConstants.INVALID_PARAMETER_VALUE, null);
    }
    return visitor;
}
Also used : FilterType(net.opengis.filter.v_1_1_0.FilterType) Filter(org.opengis.filter.Filter) CswRecordMapperFilterVisitor(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.mappings.CswRecordMapperFilterVisitor) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) CQLException(org.geotools.filter.text.cql2.CQLException)

Example 5 with CQL

use of org.geotools.filter.text.cql2.CQL in project GeoGig by boundlessgeo.

the class RepositoryFilter method addFilter.

/**
     * Adds a new filter to the repository.
     * 
     * @param featurePath the path of the features to filter, "default" for a fall back filter
     * @param filterType the format of the filter text, for example "CQL"
     * @param filterText the filter text
     */
public void addFilter(String featurePath, String filterType, String filterText) {
    Preconditions.checkState(featurePath != null && filterType != null && filterText != null, "Missing filter parameter.");
    if (filterType.equals("CQL")) {
        try {
            Filter newFilter = CQL.toFilter(filterText);
            repositoryFilters.put(featurePath, newFilter);
            filterDescriptions.add(new FilterDescription(featurePath, filterType, filterText));
        } catch (CQLException e) {
            Throwables.propagate(e);
        }
    }
}
Also used : Filter(org.opengis.filter.Filter) CQLException(org.geotools.filter.text.cql2.CQLException)

Aggregations

CQLException (org.geotools.filter.text.cql2.CQLException)7 Filter (org.opengis.filter.Filter)6 IOException (java.io.IOException)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 PersistenceException (org.codice.ddf.persistence.PersistenceException)2 SolrQueryFilterVisitor (org.codice.solr.query.SolrQueryFilterVisitor)2 Query (ddf.catalog.operation.Query)1 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)1 Filter (eu.esdihumboldt.hale.common.instance.model.Filter)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)1 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1