Search in sources :

Example 1 with SortBy

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

the class SolrMetacardClientImpl method setSortProperty.

protected String setSortProperty(QueryRequest request, SolrQuery query, SolrFilterDelegate solrFilterDelegate) {
    SortBy sortBy = request.getQuery().getSortBy();
    String sortProperty = "";
    if (sortBy != null && sortBy.getPropertyName() != null) {
        sortProperty = sortBy.getPropertyName().getPropertyName();
        SolrQuery.ORDER order = SolrQuery.ORDER.desc;
        if (sortBy.getSortOrder() == SortOrder.ASCENDING) {
            order = SolrQuery.ORDER.asc;
        }
        query.setFields("*", RELEVANCE_SORT_FIELD);
        if (Result.RELEVANCE.equals(sortProperty)) {
            query.addSort(RELEVANCE_SORT_FIELD, order);
        } else if (Result.DISTANCE.equals(sortProperty)) {
            addDistanceSort(query, GEOMETRY_SORT_FIELD, order, solrFilterDelegate);
        } else if (sortProperty.equals(Result.TEMPORAL)) {
            query.addSort(resolver.getSortKey(resolver.getField(Metacard.EFFECTIVE, AttributeType.AttributeFormat.DATE, false)), order);
        } else {
            List<String> resolvedProperties = resolver.getAnonymousField(sortProperty);
            if (!resolvedProperties.isEmpty()) {
                for (String sortField : resolvedProperties) {
                    if (sortField.endsWith(SchemaFields.GEO_SUFFIX)) {
                        addDistanceSort(query, resolver.getSortKey(sortField), order, solrFilterDelegate);
                    } else if (!(sortField.endsWith(SchemaFields.BINARY_SUFFIX) || sortField.endsWith(SchemaFields.OBJECT_SUFFIX))) {
                        query.addSort(resolver.getSortKey(sortField), order);
                    }
                }
            } else {
                LOGGER.debug("No schema field was found for sort property [{}]. No sort field was added to the query.", sortProperty);
            }
        }
    }
    return resolver.getSortKey(sortProperty);
}
Also used : SortBy(org.opengis.filter.sort.SortBy) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 2 with SortBy

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

the class SolrProviderTest method testSpatialNearestNeighbor.

@Test
public void testSpatialNearestNeighbor() throws Exception {
    deleteAllIn(provider);
    MetacardImpl metacard1 = new MockMetacard(Library.getFlagstaffRecord());
    MetacardImpl metacard2 = new MockMetacard(Library.getTampaRecord());
    MetacardImpl metacard3 = new MockMetacard(Library.getShowLowRecord());
    // Add in the geometry
    metacard1.setLocation(FLAGSTAFF_AIRPORT_POINT_WKT);
    metacard2.setLocation(TAMPA_AIRPORT_POINT_WKT);
    metacard3.setLocation(SHOW_LOW_AIRPORT_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard1, metacard2, metacard3);
    create(list);
    // Ascending
    Filter positiveFilter = filterBuilder.attribute(Metacard.GEOGRAPHY).beyond().wkt(PHOENIX_POINT_WKT, 0);
    QueryImpl query = new QueryImpl(positiveFilter);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find two records within 1000 nautical miles.", 2, sourceResponse.getResults().size());
    assertTrue("Flagstaff record was not first in ascending order.", sourceResponse.getResults().get(0).getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE) > 0);
    // Descending
    SortBy sortby = new ddf.catalog.filter.impl.SortByImpl(Result.DISTANCE, org.opengis.filter.sort.SortOrder.DESCENDING);
    query.setSortBy(sortby);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find two records within 1000 nautical miles.", 2, sourceResponse.getResults().size());
    assertTrue("Flagstaff record was not last in descending order.", sourceResponse.getResults().get(1).getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE) > 0);
    // Using WKT polygon
    positiveFilter = filterBuilder.attribute(Metacard.GEOGRAPHY).beyond().wkt(ARIZONA_POLYGON_WKT, 0);
    query = new QueryImpl(positiveFilter);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find two records based on polygon centroid.", 2, sourceResponse.getResults().size());
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) SortByImpl(org.geotools.filter.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 3 with SortBy

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

the class SolrProviderTest method testSortedPointRadiusWithComplexQuery.

@Test
public void testSortedPointRadiusWithComplexQuery() throws Exception {
    deleteAllIn(provider);
    MetacardImpl metacard1 = new MockMetacard(Library.getFlagstaffRecord());
    MetacardImpl metacard2 = new MockMetacard(Library.getTampaRecord());
    MetacardImpl metacard3 = new MockMetacard(Library.getShowLowRecord());
    // Add in the geometry
    metacard1.setLocation(FLAGSTAFF_AIRPORT_POINT_WKT);
    metacard2.setLocation(TAMPA_AIRPORT_POINT_WKT);
    metacard3.setLocation(SHOW_LOW_AIRPORT_POINT_WKT);
    // Add in a content type
    metacard1.setAttribute(Metacard.CONTENT_TYPE, "product");
    List<Metacard> list = Arrays.asList((Metacard) metacard1, metacard2, metacard3);
    /** CREATE **/
    create(list);
    // create a filter that has spatial and content type criteria
    Filter contentFilter = filterBuilder.attribute(Metacard.CONTENT_TYPE).is().text("product");
    Filter spatialFilter = filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(FLAGSTAFF_AIRPORT_POINT_WKT);
    Filter finalFilter = filterBuilder.allOf(filterBuilder.attribute(Metacard.ANY_TEXT).like().text("flagstaff"), filterBuilder.allOf(contentFilter, spatialFilter));
    // sort by distance
    QueryImpl query = new QueryImpl(finalFilter);
    SortBy sortby = new ddf.catalog.filter.impl.SortByImpl(Result.DISTANCE, org.opengis.filter.sort.SortOrder.DESCENDING);
    query.setSortBy(sortby);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(1, sourceResponse.getResults().size());
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) SortByImpl(org.geotools.filter.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 4 with SortBy

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

the class CswQueryFactoryTest method testPostGetRecordsValidSort.

@Test
public void testPostGetRecordsValidSort() throws CswException, UnsupportedQueryException, SourceUnavailableException, FederationException {
    GetRecordsType grr = createDefaultPostRecordsRequest();
    grr.setResultType(ResultType.RESULTS);
    QueryType query = new QueryType();
    SortByType incomingSort = new SortByType();
    SortPropertyType propType = new SortPropertyType();
    PropertyNameType propName = new PropertyNameType();
    propName.setContent(Collections.singletonList(TITLE_TEST_ATTRIBUTE));
    propType.setPropertyName(propName);
    incomingSort.getSortProperty().add(propType);
    query.setSortBy(incomingSort);
    JAXBElement<QueryType> jaxbQuery = new JAXBElement<>(cswQnameOutPutSchema, QueryType.class, query);
    grr.setAbstractQuery(jaxbQuery);
    QueryRequest queryRequest = queryFactory.getQuery(grr);
    SortBy resultSort = queryRequest.getQuery().getSortBy();
    assertThat(resultSort.getPropertyName().getPropertyName(), is(CQL_FRAMEWORK_TEST_ATTRIBUTE));
    assertThat(resultSort.getSortOrder(), is(SortOrder.ASCENDING));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) SortBy(org.opengis.filter.sort.SortBy) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) SortPropertyType(net.opengis.filter.v_1_1_0.SortPropertyType) SortByType(net.opengis.filter.v_1_1_0.SortByType) JAXBElement(javax.xml.bind.JAXBElement) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) PropertyNameType(net.opengis.filter.v_1_1_0.PropertyNameType) Test(org.junit.Test)

Example 5 with SortBy

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

the class TestWfsSource method testResultNumReturnedNegative.

@Test
public void testResultNumReturnedNegative() throws WfsException, SecurityServiceException, TransformerConfigurationException, UnsupportedQueryException {
    //Setup
    final String TITLE = "title";
    final String searchPhrase = "*";
    final int pageSize = 1;
    WfsSource source = getWfsSource(ONE_TEXT_PROPERTY_SCHEMA, MockWfsServer.getFilterCapabilities(), GeospatialUtil.EPSG_4326_URN, 3, false, true, -1);
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase));
    query.setPageSize(pageSize);
    SortBy sortBy = new SortByImpl(TITLE, SortOrder.DESCENDING);
    query.setSortBy(sortBy);
    QueryRequestImpl queryReq = new QueryRequestImpl(query);
    // Perform test
    SourceResponse resp = source.query(queryReq);
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) SortByImpl(ddf.catalog.filter.impl.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

SortBy (org.opengis.filter.sort.SortBy)31 QueryImpl (ddf.catalog.operation.impl.QueryImpl)21 Test (org.junit.Test)20 SortByImpl (ddf.catalog.filter.impl.SortByImpl)18 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)12 SourceResponse (ddf.catalog.operation.SourceResponse)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 QueryRequest (ddf.catalog.operation.QueryRequest)6 ArrayList (java.util.ArrayList)6 GetFeatureType (net.opengis.wfs.v_2_0_0.GetFeatureType)6 QueryType (net.opengis.wfs.v_2_0_0.QueryType)6 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)5 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)5 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)5 Filter (org.opengis.filter.Filter)5 PropertyName (org.opengis.filter.expression.PropertyName)5 MetacardMapper (org.codice.ddf.spatial.ogc.wfs.catalog.mapper.MetacardMapper)4 SortByImpl (org.geotools.filter.SortByImpl)4 Matchers.anyString (org.mockito.Matchers.anyString)4 Result (ddf.catalog.data.Result)3