use of ddf.catalog.operation.impl.QueryRequestImpl 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++;
}
}
use of ddf.catalog.operation.impl.QueryRequestImpl 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());
}
use of ddf.catalog.operation.impl.QueryRequestImpl 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)));
}
use of ddf.catalog.operation.impl.QueryRequestImpl 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)));
}
use of ddf.catalog.operation.impl.QueryRequestImpl 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());
}
}
Aggregations