use of ddf.catalog.operation.impl.QueryRequestImpl in project ddf by codice.
the class SolrProviderTest method testDeleteAlternativeAttribute.
/**
* Testing if another attribute can be used to delete records other than {@link Metacard#ID}
*
* @throws IngestException
* @throws UnsupportedQueryException
*/
@Test
public void testDeleteAlternativeAttribute() throws IngestException, UnsupportedQueryException {
deleteAllIn(provider);
MockMetacard metacard = new MockMetacard(Library.getFlagstaffRecord());
CreateResponse createResponse = create(metacard);
DeleteResponse deleteResponse = provider.delete(new DeleteRequest() {
@Override
public boolean hasProperties() {
return false;
}
@Override
public Serializable getPropertyValue(String name) {
return null;
}
@Override
public Set<String> getPropertyNames() {
return null;
}
@Override
public Map<String, Serializable> getProperties() {
return null;
}
@Override
public boolean containsPropertyName(String name) {
return false;
}
@Override
public List<? extends Serializable> getAttributeValues() {
return Arrays.asList(MockMetacard.DEFAULT_TITLE);
}
@Override
public String getAttributeName() {
return Metacard.TITLE;
}
});
Metacard deletedMetacard = deleteResponse.getDeletedMetacards().get(0);
verifyDeletedRecord(metacard, createResponse, deleteResponse, deletedMetacard);
// verify it is really not in SOLR
Filter filter = filterBuilder.attribute(Metacard.TITLE).like().text(MockMetacard.DEFAULT_TITLE);
QueryImpl query = new QueryImpl(filter);
SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
List<Result> results = sourceResponse.getResults();
assertEquals(0, results.size());
}
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)));
}
Aggregations