Search in sources :

Example 1 with SaxonApiException

use of net.sf.saxon.s9api.SaxonApiException 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 SaxonApiException

use of net.sf.saxon.s9api.SaxonApiException in project ddf by codice.

the class XpathFilterCollector method collect.

@Override
public void collect(int docId) throws IOException {
    Document doc = this.context.reader().document(docId);
    BytesRef binaryValue = doc.getBinaryValue(LUX_XML_FIELD_NAME);
    if (binaryValue != null) {
        byte[] bytes = binaryValue.bytes;
        // Lux update chain
        if (bytes.length > 4 && bytes[0] == 'T' && bytes[1] == 'I' && bytes[2] == 'N') {
            TinyBinary tb = new TinyBinary(bytes, TinyBinaryField.UTF8);
            XdmNode node = new XdmNode(tb.getTinyDocument(config));
            try {
                selector.setContextItem(node);
                XdmItem result = selector.evaluateSingle();
                if (result != null && result.size() > 0 && !(result.isAtomicValue() && !((XdmAtomicValue) result).getBooleanValue())) {
                    super.collect(docId);
                }
            } catch (SaxonApiException e) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to evaluate xpath: " + xpath, e);
            }
        }
    }
}
Also used : TinyBinary(lux.xml.tinybin.TinyBinary) Document(org.apache.lucene.document.Document) XdmNode(net.sf.saxon.s9api.XdmNode) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) BytesRef(org.apache.lucene.util.BytesRef) SolrException(org.apache.solr.common.SolrException) XdmItem(net.sf.saxon.s9api.XdmItem)

Aggregations

SaxonApiException (net.sf.saxon.s9api.SaxonApiException)2 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)1 AttributeFormat (ddf.catalog.data.AttributeType.AttributeFormat)1 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 TinyBinary (lux.xml.tinybin.TinyBinary)1 XdmItem (net.sf.saxon.s9api.XdmItem)1 XdmNode (net.sf.saxon.s9api.XdmNode)1 Document (org.apache.lucene.document.Document)1 BytesRef (org.apache.lucene.util.BytesRef)1 SolrException (org.apache.solr.common.SolrException)1