use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.
the class AttributeInjectorImplTest method testInjectIntoMetacard.
@Test
public void testInjectIntoMetacard() {
final String title = "title";
final Date created = new Date();
final MetacardImpl basicMetacard = new MetacardImpl();
basicMetacard.setTitle(title);
basicMetacard.setCreatedDate(created);
final MetacardType expectedBasicMetacardType = new MetacardTypeImpl(BASIC_METACARD.getName(), BASIC_METACARD, Sets.newHashSet(globalAttribute, basicAttribute, basicAndNitfAttribute));
final Metacard injectedBasicMetacard = attributeInjector.injectAttributes(basicMetacard);
assertThat(injectedBasicMetacard.getMetacardType(), is(expectedBasicMetacardType));
assertThat(injectedBasicMetacard.getTitle(), is(title));
assertThat(injectedBasicMetacard.getCreatedDate(), is(created));
final MetacardImpl nitfMetacard = new MetacardImpl(NITF_TYPE);
nitfMetacard.setTitle(title);
nitfMetacard.setCreatedDate(created);
final MetacardType expectedNitfMetacardType = new MetacardTypeImpl(NITF, NITF_TYPE, Sets.newHashSet(globalAttribute, basicAndNitfAttribute));
final Metacard injectedNitfMetacard = attributeInjector.injectAttributes(nitfMetacard);
assertThat(injectedNitfMetacard.getMetacardType(), is(expectedNitfMetacardType));
assertThat(injectedNitfMetacard.getTitle(), is(title));
assertThat(injectedNitfMetacard.getCreatedDate(), is(created));
}
use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.
the class DynamicSchemaResolver method addFields.
/**
* Adds the fields of the Metacard into the {@link SolrInputDocument}
*/
public void addFields(Metacard metacard, SolrInputDocument solrInputDocument) throws MetacardCreationException {
MetacardType schema = metacard.getMetacardType();
for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
if (metacard.getAttribute(ad.getName()) != null) {
List<Serializable> attributeValues = metacard.getAttribute(ad.getName()).getValues();
if (attributeValues != null && attributeValues.size() > 0 && attributeValues.get(0) != null) {
AttributeFormat format = ad.getType().getAttributeFormat();
String formatIndexName = ad.getName() + getFieldSuffix(format);
if (AttributeFormat.XML.equals(format)) {
List<String> parsedTexts = parseTextFrom(attributeValues);
// text => metadata_txt_ws
String whitespaceTokenizedIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + SchemaFields.WHITESPACE_TEXT_SUFFIX;
solrInputDocument.addField(whitespaceTokenizedIndexName, parsedTexts);
// text => metadata_txt_ws_has_case
String whiteSpaceTokenizedHasCaseIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + SchemaFields.WHITESPACE_TEXT_SUFFIX + SchemaFields.HAS_CASE;
solrInputDocument.addField(whiteSpaceTokenizedHasCaseIndexName, parsedTexts);
// text => metadata_txt_tokenized
String specialStringIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + getSpecialIndexSuffix(AttributeFormat.STRING);
solrInputDocument.addField(specialStringIndexName, parsedTexts);
// text case sensitive
solrInputDocument.addField(specialStringIndexName + SchemaFields.HAS_CASE, parsedTexts);
} else if (AttributeFormat.OBJECT.equals(format)) {
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
List<Serializable> byteArrays = new ArrayList<>();
try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOS)) {
for (Serializable serializable : attributeValues) {
out.writeObject(serializable);
byteArrays.add(byteArrayOS.toByteArray());
out.reset();
}
} catch (IOException e) {
throw new MetacardCreationException(COULD_NOT_SERIALIZE_OBJECT_MESSAGE, e);
}
attributeValues = byteArrays;
}
// Prevent adding a field already on document
if (solrInputDocument.getFieldValue(formatIndexName) == null) {
solrInputDocument.addField(formatIndexName, attributeValues);
if (AttributeFormat.GEOMETRY.equals(format)) {
solrInputDocument.addField(formatIndexName + SchemaFields.SORT_KEY_SUFFIX, createCenterPoint(attributeValues));
} else if (!(AttributeFormat.BINARY.equals(format) || AttributeFormat.OBJECT.equals(format))) {
solrInputDocument.addField(formatIndexName + SchemaFields.SORT_KEY_SUFFIX, attributeValues.get(0));
}
} else {
LOGGER.trace("Skipping adding field already found on document ({})", formatIndexName);
}
}
}
}
if (!ConfigurationStore.getInstance().isDisableTextPath()) {
if (StringUtils.isNotBlank(metacard.getMetadata())) {
try {
byte[] luxXml = createTinyBinary(metacard.getMetadata());
solrInputDocument.addField(LUX_XML_FIELD_NAME, luxXml);
} catch (XMLStreamException | SaxonApiException e) {
LOGGER.debug("Unable to parse metadata field. XPath support unavailable for metacard {}", metacard.getId());
}
}
}
/*
* Lastly the metacardType must be added to the solr document. These are internal fields
*/
String schemaName = String.format("%s#%s", schema.getName(), schema.hashCode());
solrInputDocument.addField(SchemaFields.METACARD_TYPE_FIELD_NAME, schemaName);
byte[] metacardTypeBytes = metacardTypeNameToSerialCache.getIfPresent(schemaName);
if (metacardTypeBytes == null) {
MetacardType coreMetacardType = new MetacardTypeImpl(schema.getName(), convertAttributeDescriptors(schema.getAttributeDescriptors()));
metacardTypesCache.put(schemaName, coreMetacardType);
metacardTypeBytes = serialize(coreMetacardType);
metacardTypeNameToSerialCache.put(schemaName, metacardTypeBytes);
addToFieldsCache(coreMetacardType.getAttributeDescriptors());
}
solrInputDocument.addField(SchemaFields.METACARD_TYPE_OBJECT_FIELD_NAME, metacardTypeBytes);
}
use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.
the class SolrProviderTest method testNumericalFields.
@Test
public void testNumericalFields() 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;
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);
customMetacard1.setAttribute(floatField, floatFieldValue);
customMetacard1.setAttribute(intField, intFieldValue);
customMetacard1.setAttribute(longField, longFieldValue);
customMetacard1.setAttribute(shortField, shortFieldValue);
create(Arrays.asList((Metacard) customMetacard1));
// searching double field with int value
greaterThanQueryAssertion(doubleField, intFieldValue, 1);
// searching float field with double value
greaterThanQueryAssertion(floatField, 4.0, 1);
// searching long field with int value
greaterThanQueryAssertion(longField, intFieldValue, 1);
// searching int field with long value
greaterThanQueryAssertion(intField, 3L, 1);
// searching int field with long value
greaterThanQueryAssertion(shortField, 0L, 1);
}
use of ddf.catalog.data.impl.MetacardTypeImpl 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.data.impl.MetacardTypeImpl 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