Search in sources :

Example 1 with AttributeFormat

use of ddf.catalog.data.AttributeType.AttributeFormat 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);
}
Also used : Serializable(java.io.Serializable) MetacardCreationException(ddf.catalog.data.MetacardCreationException) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) MetacardType(ddf.catalog.data.MetacardType) AttributeFormat(ddf.catalog.data.AttributeType.AttributeFormat) XMLStreamException(javax.xml.stream.XMLStreamException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with AttributeFormat

use of ddf.catalog.data.AttributeType.AttributeFormat in project ddf by codice.

the class DynamicSchemaResolver method addToFieldsCache.

private void addToFieldsCache(Set<AttributeDescriptor> descriptors) {
    for (AttributeDescriptor ad : descriptors) {
        AttributeFormat format = ad.getType().getAttributeFormat();
        fieldsCache.add(ad.getName() + schemaFields.getFieldSuffix(format));
        if (!getSpecialIndexSuffix(format).equals("")) {
            fieldsCache.add(ad.getName() + schemaFields.getFieldSuffix(format) + getSpecialIndexSuffix(format));
        }
        if (format.equals(AttributeFormat.STRING)) {
            fieldsCache.add(ad.getName() + schemaFields.getFieldSuffix(format) + getSpecialIndexSuffix(format) + SchemaFields.HAS_CASE);
            anyTextFieldsCache.add(ad.getName() + schemaFields.getFieldSuffix(format));
        }
        if (format.equals(AttributeFormat.XML)) {
            fieldsCache.add(ad.getName() + SchemaFields.TEXT_SUFFIX + SchemaFields.TOKENIZED);
            fieldsCache.add(ad.getName() + SchemaFields.TEXT_SUFFIX + SchemaFields.TOKENIZED + SchemaFields.HAS_CASE);
            fieldsCache.add(ad.getName() + schemaFields.getFieldSuffix(format) + getSpecialIndexSuffix(format));
        }
    }
}
Also used : AttributeFormat(ddf.catalog.data.AttributeType.AttributeFormat) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor)

Aggregations

AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)2 AttributeFormat (ddf.catalog.data.AttributeType.AttributeFormat)2 MetacardCreationException (ddf.catalog.data.MetacardCreationException)1 MetacardType (ddf.catalog.data.MetacardType)1 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)1