Search in sources :

Example 66 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class SolrProviderTest method testNumericSort.

@Test
public void testNumericSort() throws Exception {
    deleteAllIn(provider);
    /* SETUP */
    String doubleField = "hertz";
    double doubleFieldValue = 16065.435;
    String floatField = "inches";
    float floatFieldValue = 4.435f;
    String intField = "count";
    int intFieldValue = 4;
    String longField = "milliseconds";
    long longFieldValue = 9876543293L;
    String shortField = "daysOfTheWeek";
    short shortFieldValue = 1;
    int factor = 5;
    List<Metacard> list = new ArrayList<Metacard>();
    DateTime now = new DateTime();
    for (int i = 0; i < 5; i++) {
        Set<AttributeDescriptor> descriptors = numericalDescriptors(doubleField, floatField, intField, longField, shortField);
        MetacardTypeImpl mType = new MetacardTypeImpl("numberMetacardType", descriptors);
        MetacardImpl customMetacard1 = new MetacardImpl(mType);
        customMetacard1.setAttribute(Metacard.ID, "");
        customMetacard1.setAttribute(doubleField, doubleFieldValue + factor * i);
        customMetacard1.setAttribute(floatField, floatFieldValue + factor * i);
        customMetacard1.setAttribute(intField, intFieldValue + factor * i);
        customMetacard1.setAttribute(longField, longFieldValue + factor * i);
        customMetacard1.setAttribute(shortField, shortFieldValue + factor * i);
        list.add(customMetacard1);
    }
    create(list);
    Filter filter = null;
    QueryImpl query = null;
    SourceResponse sourceResponse = null;
    // Sort all Double ASCENDING
    filter = filterBuilder.attribute(Metacard.ANY_TEXT).like().text("*");
    query = new QueryImpl(filter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(doubleField, SortOrder.ASCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    for (int i = 0; i < list.size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(doubleFieldValue + factor * i, r.getMetacard().getAttribute(doubleField).getValue());
    }
    // Sort all double DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(doubleField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    int counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(doubleFieldValue + factor * counter, r.getMetacard().getAttribute(doubleField).getValue());
        counter++;
    }
    // Sort all Float ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(floatField, SortOrder.ASCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    for (int i = 0; i < list.size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(floatFieldValue + factor * i, r.getMetacard().getAttribute(floatField).getValue());
    }
    // Sort all Float DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(floatField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(floatFieldValue + factor * counter, r.getMetacard().getAttribute(floatField).getValue());
        counter++;
    }
    // Sort all int ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(intField, SortOrder.ASCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    for (int i = 0; i < list.size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(intFieldValue + factor * i, r.getMetacard().getAttribute(intField).getValue());
    }
    // Sort all int DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(intField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(intFieldValue + factor * counter, r.getMetacard().getAttribute(intField).getValue());
        counter++;
    }
    // Sort all long ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(longField, SortOrder.ASCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    for (int i = 0; i < list.size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(longFieldValue + factor * i, r.getMetacard().getAttribute(longField).getValue());
    }
    // Sort all long DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(longField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(longFieldValue + factor * counter, r.getMetacard().getAttribute(longField).getValue());
        counter++;
    }
    // Sort all short ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(shortField, SortOrder.ASCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    for (int i = 0; i < list.size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals((short) (shortFieldValue + factor * i), r.getMetacard().getAttribute(shortField).getValue());
    }
    // Sort all short DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(shortField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals((short) (shortFieldValue + factor * counter), r.getMetacard().getAttribute(shortField).getValue());
        counter++;
    }
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 67 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class SolrProviderTest method testSpatialQueryAcrossInternationalDateLine.

@Test
public void testSpatialQueryAcrossInternationalDateLine() throws Exception {
    deleteAllIn(provider);
    MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());
    metacard.setLocation(MIDWAY_ISLANDS_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard);
    create(list);
    /** POSITIVE - Counter Clockwise Orientation **/
    Filter filter = filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(ACROSS_INTERNATIONAL_DATELINE_LARGE_CCW_WKT);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Failed to find the correct record. ", 1, sourceResponse.getResults().size());
    for (Result r : sourceResponse.getResults()) {
        assertTrue("Wrong record, Flagstaff keyword was not found.", ALL_RESULTS != r.getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE));
    }
    /** POSITIVE - Clockwise Orientation **/
    filter = filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(ACROSS_INTERNATIONAL_DATELINE_LARGE_CW_WKT);
    sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Failed to find the correct record. ", 1, sourceResponse.getResults().size());
    for (Result r : sourceResponse.getResults()) {
        assertTrue("Wrong record, Flagstaff keyword was not found.", ALL_RESULTS != r.getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE));
    }
    /** NEGATIVE **/
    filter = filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(ACROSS_INTERNATIONAL_DATELINE_SMALL_WKT);
    sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Should not find a record. ", 0, 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) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 68 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class SolrProviderTest method testSpatialDistanceCalculationExactPoint.

@Test
public void testSpatialDistanceCalculationExactPoint() throws Exception {
    deleteAllIn(provider);
    // given
    double radiusInKilometers = 50;
    double radiusInMeters = radiusInKilometers * METERS_PER_KM;
    Filter positiveFilter = filterBuilder.attribute(Metacard.GEOGRAPHY).withinBuffer().wkt(LAS_VEGAS_POINT_WKT, radiusInMeters);
    MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());
    metacard.setLocation(LAS_VEGAS_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard);
    create(list);
    QueryImpl query = new QueryImpl(positiveFilter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(Result.DISTANCE, SortOrder.ASCENDING));
    // when
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    // then
    assertEquals("Failed to find metacard WKT with filter", 1, sourceResponse.getResults().size());
    Result result = sourceResponse.getResults().get(0);
    assertThat(result.getDistanceInMeters(), is(notNullValue()));
    assertThat("Point radius search should be less than the radius given.", result.getDistanceInMeters(), is(lessThanOrEqualTo(radiusInMeters)));
    double oneMeter = 1.0;
    assertThat("The distance should be close to zero since we are right upon the point.", result.getDistanceInMeters(), is(lessThanOrEqualTo(oneMeter)));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 69 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class SolrProviderTest method spatialDistanceCalculationBetweenTwoPoints.

private void spatialDistanceCalculationBetweenTwoPoints(String attribute, String sortBy) throws Exception {
    deleteAllIn(provider);
    // given
    double radiusInKilometers = 500;
    double radiusInMeters = radiusInKilometers * METERS_PER_KM;
    Filter positiveFilter = filterBuilder.attribute(attribute).withinBuffer().wkt(PHOENIX_POINT_WKT, radiusInMeters);
    MetacardImpl metacard;
    if (attribute.equals(Metacard.GEOGRAPHY)) {
        metacard = new MockMetacard(Library.getFlagstaffRecord());
    } else {
        metacard = new MockMetacard(Library.getFlagstaffRecord(), new MetacardTypeImpl("distanceTest", BasicTypes.BASIC_METACARD, Sets.newSet(new AttributeDescriptorImpl(attribute, true, true, true, true, BasicTypes.GEO_TYPE))));
    }
    metacard.setAttribute(attribute, LAS_VEGAS_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard);
    create(list);
    QueryImpl query = new QueryImpl(positiveFilter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(sortBy, SortOrder.ASCENDING));
    // when
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    // then
    assertEquals("Failed to find metacard WKT with filter", 1, sourceResponse.getResults().size());
    Result result = sourceResponse.getResults().get(0);
    assertThat(result.getDistanceInMeters(), is(notNullValue()));
    assertThat("Point radius search should be less than the radius given.", result.getDistanceInMeters(), is(lessThanOrEqualTo(radiusInMeters)));
    // expected distance calculated from
    // http://www.movable-type.co.uk/scripts/latlong.html
    double expectedDistanceBetweenCitiesInMeters = 412700;
    // +/-0.1%
    double precisionPercentage = .001;
    double lowerBound = expectedDistanceBetweenCitiesInMeters * (1 - precisionPercentage);
    double upperBound = expectedDistanceBetweenCitiesInMeters * (1 + precisionPercentage);
    assertThat("The distance returned should at least be above the lower bound of error.", result.getDistanceInMeters(), is(greaterThanOrEqualTo(lowerBound)));
    assertThat("The distance returned should at least be below the upper bound of error.", result.getDistanceInMeters(), is(lessThanOrEqualTo(upperBound)));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl)

Example 70 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl in project ddf by codice.

the class SolrProviderTest method testStartIndexWithSorting.

@Test
public void testStartIndexWithSorting() throws Exception {
    deleteAllIn(provider);
    List<Metacard> metacards = new ArrayList<Metacard>();
    DateTime dt = new DateTime(1985, 1, 1, 1, 1, 1, 1, DateTimeZone.UTC);
    TreeSet<Date> calculatedDates = new TreeSet<Date>();
    for (int j = 0; j < 10; j++) {
        for (int i = 0; i < 100; i = i + 10) {
            MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());
            // ingest sporadically the effective dates so the default return
            // order won't be ordered
            Date calculatedDate = dt.plusDays(100 - i + 10 - j).toDate();
            calculatedDates.add(calculatedDate);
            metacard.setEffectiveDate(calculatedDate);
            metacards.add(metacard);
        }
    }
    // The TreeSet will sort them, the array will give me access to everyone
    // without an iterator
    Date[] dates = new Date[calculatedDates.size()];
    calculatedDates.toArray(dates);
    /** CREATE **/
    CreateResponse response = create(metacards);
    LOGGER.info("CREATED {} records.", response.getCreatedMetacards().size());
    CommonQueryBuilder queryBuilder = new CommonQueryBuilder();
    QueryImpl query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    int maxSize = 20;
    int startIndex = 2;
    // STARTINDEX=2, MAXSIZE=20
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    SortByImpl sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
    // STARTINDEX=20, MAXSIZE=5
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    maxSize = 5;
    startIndex = 20;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
    // STARTINDEX=80, MAXSIZE=20
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    maxSize = 20;
    startIndex = 80;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
    // STARTINDEX=1, MAXSIZE=100
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    maxSize = 100;
    startIndex = 1;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) CreateResponse(ddf.catalog.operation.CreateResponse) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Date(java.util.Date) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(org.geotools.filter.SortByImpl) TreeSet(java.util.TreeSet) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Aggregations

MetacardImpl (ddf.catalog.data.impl.MetacardImpl)384 Test (org.junit.Test)247 Metacard (ddf.catalog.data.Metacard)144 ArrayList (java.util.ArrayList)102 Result (ddf.catalog.data.Result)62 HashMap (java.util.HashMap)59 Date (java.util.Date)52 ResultImpl (ddf.catalog.data.impl.ResultImpl)51 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)49 URI (java.net.URI)38 Matchers.anyString (org.mockito.Matchers.anyString)38 QueryRequest (ddf.catalog.operation.QueryRequest)36 QueryResponse (ddf.catalog.operation.QueryResponse)35 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)35 Serializable (java.io.Serializable)33 HashSet (java.util.HashSet)31 QueryImpl (ddf.catalog.operation.impl.QueryImpl)29 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)28 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)27 List (java.util.List)27