Example usage for org.apache.solr.common SolrInputDocument getFieldNames

List of usage examples for org.apache.solr.common SolrInputDocument getFieldNames

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputDocument getFieldNames.

Prototype

@Override
public Collection<String> getFieldNames() 

Source Link

Document

Get all field names.

Usage

From source file:com.gu.solr.MergeUtils.java

License:Apache License

private static SolrInputDocument copy(SolrInputDocument document) {
    SolrInputDocument copy = new SolrInputDocument();

    for (String name : document.getFieldNames()) {
        copy.addField(name, document.getFieldValue(name));
    }// w w  w. j  a va  2s.c  o m

    return copy;
}

From source file:com.ngdata.hbaseindexer.indexer.FusionDocumentWriter.java

License:Apache License

/**
 * shs: This method was modified from its original to add the input parameters 'parent' and 'docCount'. This was done
 *      to enable recursion to be used to find all parent/child relationships to any level. The method will merge the
 *      fields in the parent document into the child document and will then convert that merged document into JSON
 *      format and return that JSON document to the caller.
 * @param parent    The parent document for the child document being passed in. Parent may be null if the child being
 *                  passed in is a member of the initial documents submitted.
 * @param child     This is the child document. It will have the parent's fields merged into it.
 * @param docCount  This is a count of the number of documents that have been added in this processing.
 * @return          The merged parent and child documents as a JSON formatted document, in a format acceptable to
 *                  Fusion./*from  w w w.  j  a  va  2 s.com*/
 */
protected Map<String, Object> doc2json(SolrInputDocument parent, SolrInputDocument child, int docCount) {
    Map<String, Object> json = new HashMap<String, Object>();
    if (child != null) {
        String docId = (String) child.getFieldValue("id");
        if (docId == null) {
            if (parent != null) {
                String parentId = (String) parent.getFieldValue("id");
                docId = parentId + "-" + docCount;
            }
            if (docId == null)
                throw new IllegalStateException("Couldn't resolve the id for document: " + child);
        }
        json.put("id", docId);

        List fields = new ArrayList();
        if (parent != null) {
            if (log.isDebugEnabled())
                log.debug("Method:doc2json - Merging parent and child docs, parent:[" + parent.toString()
                        + "]; child[" + child.toString() + "].");

            // have a parent doc ... flatten by adding all parent doc fields to the child with prefix _p_
            for (String f : parent.getFieldNames()) {
                if ("id".equals(f)) {
                    fields.add(mapField("_p_id", null /* field name prefix */,
                            parent.getField("id").getFirstValue()));
                } else {
                    appendField(parent, f, "_p_", fields);
                }
            }
        }
        for (String f : child.getFieldNames()) {
            if (!"id".equals(f)) { // id already added
                appendField(child, f, null, fields);
            }
        }
        // keep track of the time we saw this doc on the hbase side
        String tdt = DateUtil.getThreadLocalDateFormat().format(new Date());
        fields.add(mapField("_hbasets_tdt", null, tdt));
        if (log.isDebugEnabled())
            log.debug(strIndexName + " Reconcile id = " + docId + " and timestamp = " + tdt);

        json.put("fields", fields);
    } else {
        log.warn("method:doc2json - Input parameter 'child' was null.");
    }
    return json;
}

From source file:com.sindicetech.siren.solr.facet.SirenFieldFacetExtractor.java

License:Open Source License

/**
 * The main entry point of this class. Generates a list of {@link SirenFacetEntry} for
 * the given {@link SolrInputDocument} by performing a DFS through the doc.
 *
 * @param doc The document for which to generate facet entries
 * @throws IllegalStateException if IndexSchema was not set (either in constructor or via {@link #setSchema(IndexSchema)}
 *//*w  ww  . j av  a 2  s .  c om*/
@Override
public List<SirenFacetEntry> extractFacets(SolrInputDocument doc) throws FacetException {
    if (schema == null) {
        throw new IllegalStateException(
                "Schema field is null - probably the default constructor was used without calling setSchema() later.");
    }

    List<SirenFacetEntry> facets = new ArrayList<SirenFacetEntry>();

    for (String fieldName : doc.getFieldNames()) {
        FieldType ft = schema.getFieldOrNull(fieldName).getType();
        if (ft instanceof ExtendedJsonField) {
            String sirenField = (String) doc.getFieldValue(fieldName);
            try {
                JsonNode sirenNode = mapper.readTree(sirenField);

                generateFacetsForLeaves(sirenNode, fieldName, (ExtendedJsonField) ft, "", facets);

            } catch (JsonProcessingException e) {
                throw new FacetException("Could not parse siren field " + fieldName + ": " + e.getMessage(), e);
            } catch (IOException e) {
                throw new FacetException(
                        "I/O problem while parsing siren field " + fieldName + ": " + e.getMessage(), e);
            }
        }
    }

    return facets;
}

From source file:com.thinkaurelius.titan.diskstorage.solr.Solr5Index.java

License:Apache License

private void commitDocumentChanges(String collectionName, Collection<SolrInputDocument> documents)
        throws SolrServerException, IOException {
    if (documents.size() == 0)
        return;/*from   w  ww . java 2  s  .com*/

    try {
        solrClient.request(newUpdateRequest().add(documents), collectionName);
    } catch (HttpSolrClient.RemoteSolrException rse) {
        logger.error(
                "Unable to save documents to Solr as one of the shape objects stored were not compatible with Solr.",
                rse);
        logger.error("Details in failed document batch: ");
        for (SolrInputDocument d : documents) {
            Collection<String> fieldNames = d.getFieldNames();
            for (String name : fieldNames) {
                logger.error(name + ":" + d.getFieldValue(name).toString());
            }
        }

        throw rse;
    }
}

From source file:connectors.reuters21578.ExtractReuters.java

License:Apache License

public ArrayList<String> getBodies() {
    ArrayList<String> textString = new ArrayList<String>();
    System.out.println(docs.size());
    //int docNo=0;
    for (SolrInputDocument solrInputDocument : docs) {
        if (solrInputDocument.getFieldNames().contains("BODY")) {
            textString.add(solrInputDocument.getField("BODY").toString().substring(5));
        }/*from ww  w.jav  a2  s . c om*/
        /*else if(solrInputDocument.getFieldNames().contains("TITLE")){
           docNo++;
            textString.add(solrInputDocument.getField("TITLE").toString().substring(6));
        System.out.println(solrInputDocument.getField("TITLE"));
           }*/
    }
    return textString;
}

From source file:de.hybris.platform.solrfacetsearch.indexer.impl.SolrDocumentFactoryTest.java

License:Open Source License

@Test
public void testCreateSolrInputDocument() throws Exception {
    final FacetSearchConfig config = facetSearchConfigService.getConfiguration("productSearch");
    assertNotNull("Facet Search Config must not be null", config);
    final ProductModel product = productService.getProduct("HW2310-1004");
    assertNotNull("Product must not be null", product);
    final IndexConfig indexConfig = config.getIndexConfig();
    assertNotNull("Index config must not be null", indexConfig);
    final Collection<IndexedType> indexedTypes = indexConfig.getIndexedTypes().values();
    assertNotNull("Collection of indexed types must not be null", indexedTypes);
    assertEquals("Size of collection of indexed types", 1, indexedTypes.size());
    final IndexedType indexedType = indexedTypes.iterator().next();
    final SolrInputDocument inputDocument = solrDocumentFactory.createInputDocument(product,
            config.getIndexConfig(), indexedType);
    assertNotNull("id must not be null", inputDocument.getField("id"));
    inputDocument.removeField("id");
    assertNotNull("pk must not be null", inputDocument.getField("pk"));
    inputDocument.removeField("pk");
    assertNotNull("catalogId must not be null", inputDocument.getField("catalogId"));
    assertEquals("Catalog ID", product.getCatalogVersion().getCatalog().getId(),
            inputDocument.getField("catalogId").getValue());
    inputDocument.removeField("catalogId");
    assertNotNull("catalogVersion must not be null", inputDocument.getField("catalogVersion"));
    assertEquals("Catalog Version", product.getCatalogVersion().getVersion(),
            inputDocument.getField("catalogVersion").getValue());
    inputDocument.removeField("catalogVersion");
    final Collection<IndexedProperty> indexedProperties = indexedType.getIndexedProperties().values();
    for (final IndexedProperty indexedProperty : indexedProperties) {
        final Collection<FieldValue> fieldValues = IndexedProperties.getFieldValueProvider(indexedProperty)
                .getFieldValues(indexConfig, indexedProperty, product);
        assertContainsValues(inputDocument, fieldValues);
    }/*from ww  w . j av a2  s.  c  o m*/
    assertTrue("Input document has too many fields", inputDocument.getFieldNames().isEmpty());
}

From source file:edu.unc.lib.dl.data.ingest.solr.indexing.SolrUpdateDriver.java

License:Apache License

/**
 * Perform a partial document update from a IndexDocumentBean. Null fields are considered to be unspecified and will
 * not be changed, except for the update timestamp field which is always set.
 * //from   ww w  .j  a  va  2s .c o  m
 * @param operation
 * @param idb
 * @throws IndexingException
 */
public void updateDocument(String operation, IndexDocumentBean idb) throws IndexingException {
    try {
        SolrInputDocument sid = solrServer.getBinder().toSolrInputDocument(idb);
        for (String fieldName : sid.getFieldNames()) {
            if (!ID_FIELD.equals(fieldName)) {
                SolrInputField inputField = sid.getField(fieldName);
                // Adding in each non-null field value, except the timestamp field which gets cleared if not specified so
                // that it always gets updated as part of a partial update
                // TODO enable timestamp updating when fix for SOLR-4133 is released, which enables setting null fields
                if (inputField != null
                        && (inputField.getValue() != null || UPDATE_TIMESTAMP.equals(fieldName))) {
                    Map<String, Object> partialUpdate = new HashMap<String, Object>();
                    partialUpdate.put(operation, inputField.getValue());
                    sid.setField(fieldName, partialUpdate);
                }
            }
        }
        log.debug("Performing partial update:\n" + ClientUtils.toXML(sid));
        solrServer.add(sid);
    } catch (IOException e) {
        throw new IndexingException("Failed to add document to solr", e);
    } catch (SolrServerException e) {
        throw new IndexingException("Failed to add document to solr", e);
    }
}

From source file:edu.unc.lib.dl.data.ingest.solr.test.AtomicUpdateTest.java

License:Apache License

@Test
public void atomicUpdate() throws IOException {
    IndexDocumentBean idb = new IndexDocumentBean();
    idb.setId("id");
    idb.setStatus(Arrays.asList("Unpublished", "Parent Unpublished"));

    DocumentObjectBinder binder = new DocumentObjectBinder();
    SolrInputDocument sid = binder.toSolrInputDocument(idb);

    String operation = "set";

    for (String fieldName : sid.getFieldNames()) {
        if (!ID_FIELD.equals(fieldName)) {
            SolrInputField inputField = sid.getField(fieldName);
            // Adding in each non-null field value, except the timestamp field which gets cleared if not specified so
            // that it always gets updated as part of a partial update
            // TODO enable timestamp updating when fix for SOLR-4133 is released, which enables setting null fields
            if (inputField != null && (inputField.getValue() != null || UPDATE_TIMESTAMP.equals(fieldName))) {
                Map<String, Object> partialUpdate = new HashMap<String, Object>();
                partialUpdate.put(operation, inputField.getValue());
                sid.setField(fieldName, partialUpdate);
            }//from   ww  w. j av a 2  s  . co  m
        }
    }

    /*   Map<String,String> mapField = new HashMap<String,String>();
       mapField.put("", arg1)
       Map<String, Object> partialUpdate = new HashMap<String, Object>();
       partialUpdate.put("set", inputField.getFirstValue());
       sid.setField(fieldName, partialUpdate);*/

    StringWriter writer = new StringWriter();
    ClientUtils.writeXML(sid, writer);

    System.out.println(writer.toString());

    System.out.println(sid.toString());
}

From source file:eu.annocultor.converters.solr.SolrDocumentTagger.java

License:Apache License

static int countWords(SolrInputDocument document, String fieldNamePrefix) {
    int count = 0;
    for (String fieldName : document.getFieldNames()) {
        if (fieldName.startsWith(fieldNamePrefix)) {
            for (Object fieldValue : document.getFieldValues(fieldName)) {
                if (fieldValue != null) {
                    String delims = "[\\W]+";
                    String[] words = fieldValue.toString().split(delims);
                    for (String word : words) {
                        if (!StringUtils.isBlank(word)
                                && word.length() >= MIN_WORD_LENGTH_TO_INCLUDE_IN_WORD_COUNT) {
                            count++;//from w ww . j a  v a 2  s .c o m
                        }
                    }
                }
            }
        }
    }
    return count;
}

From source file:eu.annocultor.data.destinations.SolrServer.java

License:Apache License

private void flush() throws Exception {

    int docs = documents.size();
    System.out.println("Start with " + docs);
    while (!documents.isEmpty()) {
        final SolrInputDocument document = documents.pop();
        if (document == null || document.getFieldNames().isEmpty()) {
            docs--;//w ww .ja  v a2 s  .  co m
        } else {
            log.info("Document to server: " + document.toString());
            try {
                server.add(document);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    server.commit();
    log.info("Committed " + docs + " documents");
}