Example usage for org.apache.solr.common SolrInputField getValueCount

List of usage examples for org.apache.solr.common SolrInputField getValueCount

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputField getValueCount.

Prototype

public int getValueCount() 

Source Link

Usage

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

License:Apache License

protected void appendField(SolrInputDocument doc, String f, String pfx, List fields) {
    SolrInputField field = doc.getField(f);
    int vc = field.getValueCount();
    if (vc <= 0)
        return; // no values to add for this field

    if (vc == 1) {
        Map<String, Object> fieldMap = mapField(f, pfx, field.getFirstValue());
        if (fieldMap != null)
            fields.add(fieldMap);//from  w  w w .j av  a 2  s  . c  o  m
    } else {
        for (Object val : field.getValues()) {
            Map<String, Object> fieldMap = mapField(f, pfx, val);
            if (fieldMap != null)
                fields.add(fieldMap);
        }
    }
}

From source file:com.talis.rdf.solr.DefaultDocumentBuilderTest.java

License:Apache License

@Test
public void getDocumentAddsLiteralFieldsForMultipleValuesOfSamePredicate() {

    ArrayList<Quad> quads = new ArrayList<Quad>();
    int NUMBER_OF_QUADS = 10;
    for (int i = 0; i < NUMBER_OF_QUADS; i++) {
        quads.add(new Quad(Node.createURI(GRAPH_URI), Node.createURI(SUBJECT_URI),
                Node.createURI(PREDICATE_BASE), Node.createLiteral(OBJECT_BASE + i)));
    }/*from www .j  a v a  2 s .c  o m*/

    SolrInputDocument doc = quadsToDoc.getDocument(DOCUMENT_KEY, quads);
    assertNotNull(doc);

    SolrInputField field = doc.getField(PREDICATE_BASE);
    assertEquals(NUMBER_OF_QUADS, field.getValueCount());
    Collection<Object> values = field.getValues();
    for (int i = 0; i < NUMBER_OF_QUADS; i++) {
        String expected = OBJECT_BASE + i;
        assertTrue(values.contains(expected));
    }

}

From source file:edu.cornell.mannlib.vitro.webapp.search.solr.ThumbnailImageURLTest.java

License:Open Source License

/**
 * Test method for {@link edu.cornell.mannlib.vitro.webapp.search.solr.documentBuilding.ThumbnailImageURL#modifyDocument(edu.cornell.mannlib.vitro.webapp.beans.Individual, org.apache.solr.common.SolrInputDocument, java.lang.StringBuffer)}.
 *///ww w.  j av a 2 s. c  om
@Test
public void testModifyDocument() {
    SolrInputDocument doc = new SolrInputDocument();
    ThumbnailImageURL testMe = new ThumbnailImageURL(testModel);
    Individual ind = new IndividualImpl();
    ind.setURI(personsURI);
    try {
        testMe.modifyDocument(ind, doc, null);
    } catch (SkipIndividualException e) {
        Assert.fail("person was skipped: " + e.getMessage());
    }

    SolrInputField thumbnailField = doc.getField(fieldForThumbnailURL);
    Assert.assertNotNull(thumbnailField);

    Assert.assertNotNull(thumbnailField.getValues());
    Assert.assertEquals(1, thumbnailField.getValueCount());

    Assert.assertEquals("http://vivo.cornell.edu/individual/n54945", thumbnailField.getFirstValue());
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

public static Document toDocument(SolrInputDocument doc, IndexSchema schema, AlfrescoSolrDataModel model) {
    Document out = new Document();
    out.setBoost(doc.getDocumentBoost());

    // Load fields from SolrDocument to Document
    for (SolrInputField field : doc) {
        String name = field.getName();
        SchemaField sfield = schema.getFieldOrNull(name);
        boolean used = false;
        float boost = field.getBoost();

        // Make sure it has the correct number
        if (sfield != null && !sfield.multiValued() && field.getValueCount() > 1) {
            String id = "";
            SchemaField sf = schema.getUniqueKeyField();
            if (sf != null) {
                id = "[" + doc.getFieldValue(sf.getName()) + "] ";
            }//from   w w w . j a v a  2 s  . c  o  m
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "ERROR: " + id + "multiple values encountered for non multiValued field " + sfield.getName()
                            + ": " + field.getValue());
        }

        // load each field value
        boolean hasField = false;
        for (Object v : field) {
            // TODO: Sort out null
            if (v == null) {
                continue;
            }
            String val = null;
            hasField = true;
            boolean isBinaryField = false;
            if (sfield != null && sfield.getType() instanceof BinaryField) {
                isBinaryField = true;
                BinaryField binaryField = (BinaryField) sfield.getType();
                Field f = binaryField.createField(sfield, v, boost);
                if (f != null)
                    out.add(f);
                used = true;
            } else {
                // TODO!!! HACK -- date conversion
                if (sfield != null && v instanceof Date && sfield.getType() instanceof DateField) {
                    DateField df = (DateField) sfield.getType();
                    val = df.toInternal((Date) v) + 'Z';
                } else if (v != null) {
                    val = v.toString();
                }

                if (sfield != null) {
                    if (v instanceof Reader) {
                        used = true;
                        Field f = new Field(field.getName(), (Reader) v, model.getFieldTermVec(sfield));
                        f.setOmitNorms(model.getOmitNorms(sfield));
                        f.setOmitTermFreqAndPositions(sfield.omitTf());

                        if (f != null) { // null fields are not added
                            out.add(f);
                        }
                    } else {
                        used = true;
                        Field f = sfield.createField(val, boost);
                        if (f != null) { // null fields are not added
                            out.add(f);
                        }
                    }
                }
            }

            // Check if we should copy this field to any other fields.
            // This could happen whether it is explicit or not.
            List<CopyField> copyFields = schema.getCopyFieldsList(name);
            for (CopyField cf : copyFields) {
                SchemaField destinationField = cf.getDestination();
                // check if the copy field is a multivalued or not
                if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "ERROR: multiple values encountered for non multiValued copy field "
                                    + destinationField.getName() + ": " + val);
                }

                used = true;
                Field f = null;
                if (isBinaryField) {
                    if (destinationField.getType() instanceof BinaryField) {
                        BinaryField binaryField = (BinaryField) destinationField.getType();
                        f = binaryField.createField(destinationField, v, boost);
                    }
                } else {
                    f = destinationField.createField(cf.getLimitedValue(val), boost);
                }
                if (f != null) { // null fields are not added
                    out.add(f);
                }
            }

            // In lucene, the boost for a given field is the product of the
            // document boost and *all* boosts on values of that field.
            // For multi-valued fields, we only want to set the boost on the
            // first field.
            boost = 1.0f;
        }

        // make sure the field was used somehow...
        if (!used && hasField) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR:unknown field '" + name + "'");
        }
    }

    // Now validate required fields or add default values
    // fields with default values are defacto 'required'
    for (SchemaField field : schema.getRequiredFields()) {
        if (out.getField(field.getName()) == null) {
            if (field.getDefaultValue() != null) {
                out.add(field.createField(field.getDefaultValue(), 1.0f));
            } else {
                String id = schema.printableUniqueKey(out);
                String msg = "Document [" + id + "] missing required field: " + field.getName();
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
            }
        }
    }
    return out;
}

From source file:org.apache.blur.slur.RowMutationHelper.java

License:Apache License

private static RecordMutation createRecordMutation(SolrInputDocument doc, String id) {
    RecordMutation recordMutation = new RecordMutation();
    // TODO: what's solr default behavior?
    recordMutation.setRecordMutationType(RecordMutationType.REPLACE_ENTIRE_RECORD);
    Record record = new Record();
    record.setFamily(findFamily(doc));// ww  w.ja  v  a  2 s.  co  m
    record.setRecordId(id);

    for (String fieldName : doc.getFieldNames()) {
        if (!fieldName.contains(".")) {
            continue;
        }
        SolrInputField field = doc.getField(fieldName);
        String rawColumnName = fieldName.substring(fieldName.indexOf(".") + 1, fieldName.length());

        if (field.getValueCount() > 1) {
            for (Object fieldVal : field.getValues()) {
                record.addToColumns(new Column(rawColumnName, fieldVal.toString()));
            }
        } else {
            record.addToColumns(new Column(rawColumnName, field.getFirstValue().toString()));
        }
    }
    recordMutation.setRecord(record);
    return recordMutation;
}

From source file:org.obm.service.solr.jms.EventUpdateCommand.java

License:Open Source License

private void appendField(SolrInputDocument solrInputDocument, String field, Object... values) {
    SolrInputField solrInputField = new SolrInputField(field);
    for (Object value : values) {
        if (value != null) {
            solrInputField.addValue(value, 1);
        }//from   w  ww.j a  va  2s  .  c  o  m
    }
    if (solrInputField.getValueCount() >= 1) {
        solrInputDocument.put(field, solrInputField);
    }
}

From source file:org.springframework.data.solr.core.convert.MappingSolrConvertDocumentObjectBinderCompatibilityTests.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/* w  w w  .j  av  a2 s .co m*/
public void testSimple() throws Exception {
    XMLResponseParser parser = new XMLResponseParser();
    NamedList<Object> nl = parser.processResponse(new StringReader(xml));
    QueryResponse res = new QueryResponse(nl, null);

    SolrDocumentList solDocList = res.getResults();
    List<Item> l = getBeans(solDocList);
    Assert.assertEquals(solDocList.size(), l.size());
    Assert.assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);

    Item item = new Item();
    item.id = "aaa";
    item.categories = new String[] { "aaa", "bbb", "ccc" };
    SolrInputDocument out = new SolrInputDocument();
    converter.write(item, out);

    Assert.assertEquals(item.id, out.getFieldValue("id"));
    SolrInputField catfield = out.getField("cat");
    Assert.assertEquals(3, catfield.getValueCount());

    List<String> catValues = (List<String>) catfield.getValue();
    Assert.assertEquals("aaa", catValues.get(0));
    Assert.assertEquals("bbb", catValues.get(1));
    Assert.assertEquals("ccc", catValues.get(2));
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.converter.SolrDocumentConverterTest.java

License:Apache License

@Test
public void testTypeAndAc() throws Exception {
    String psiMiTabLine = "intact:EBI-12345\tintact:EBI-1443\tuniprotkb:Nefh(gene name)\tuniprotkb:Dst(gene name)"
            + "\tintact:Nfh\tintact:Bpag1\tMI:0018(2 hybrid)\tLeung et al. (1999)\tpubmed:9971739"
            + "\ttaxid:10116(rat)\ttaxid:10090(mouse)\tMI:0218(physical interaction)\tMI:0469(intact)"
            + "\tintact:EBI-446356|irefindex:arigidblabla(rigid)\t-\t-\tMI:0499(unspecified role)"
            + "\tMI:0499(unspecified role)\tMI:0498(prey)\tMI:0496(bait)\tMI:0326(protein)\tMI:0326(protein)\tinterpro:IPR004829|\tgo:\"GO:0030246\"\t-\t-\t-\t-\tyeast:4932\t-\t-\t-\t-"
            + "\t-\t-\t-\t-\t-\t-\t-\t-\t-";

    SolrDocumentConverter converter = new SolrDocumentConverter(getSolrServer());
    SolrInputDocument doc = converter.toSolrDocument(psiMiTabLine);

    final SolrInputField field = doc.getField("intact_byInteractorType_mi0326");
    Assert.assertEquals(2, field.getValueCount());
    Assert.assertTrue(field.getValues().contains("EBI-12345"));
    Assert.assertTrue(field.getValues().contains("EBI-1443"));

}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.converter.SolrDocumentConverterTest.java

License:Apache License

@Test
public void idSelectiveAdder() throws Exception {
    String psiMiTabLine = "intact:EBI-12345\tintact:EBI-54321|uniprotkb:P12345\tuniprotkb:Nefh(gene name)\tuniprotkb:Dst(gene name)"
            + "\tintact:Nfh\tintact:Bpag1\tMI:0018(2 hybrid)\tLeung et al. (1999)\tpubmed:9971739"
            + "\ttaxid:10116(rat)\ttaxid:10090(mouse)\tMI:0218(physical interaction)\tMI:0469(intact)"
            + "\tintact:EBI-446356|irefindex:arigidblabla(rigid)\t-\t-\tMI:0499(unspecified role)"
            + "\tMI:0499(unspecified role)\tMI:0498(prey)\tMI:0496(bait)\tMI:0326(protein)\tMI:0326(protein)\tinterpro:IPR004829|\tgo:\"GO:0030246\"\t-\t-\t-\t-\tyeast:4932\t-\t-\t-\t-"
            + "\t-\t-\t-\t-\t-\t-\t-\t-\t-";

    SolrDocumentConverter converter = new SolrDocumentConverter(getSolrServer());
    SolrInputDocument doc = converter.toSolrDocument(psiMiTabLine);

    final SolrInputField intactIdField = doc.getField("idA");
    Assert.assertEquals(3, intactIdField.getValueCount());
    Assert.assertTrue(intactIdField.getValues().contains("EBI-12345"));
    final SolrInputField intactIdField2 = doc.getField("idB");
    Assert.assertEquals(6, intactIdField2.getValueCount());
    Assert.assertTrue(intactIdField2.getValues().contains("EBI-54321"));
    Assert.assertTrue(intactIdField2.getValues().contains("P12345"));

    /*final SolrInputField uniprotIdField = doc.getField("uniprotkb_id");
    Assert.assertEquals(1, uniprotIdField.getValueCount());
    Assert.assertTrue(uniprotIdField.getValues().contains("P12345"));*/

}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.converter.SolrDocumentConverterTest.java

License:Apache License

@Test
public void geneName() throws Exception {
    String psiMiTabLine = "intact:EBI-12345\tintact:EBI-54321|uniprotkb:P12345\tuniprotkb:Nefh(gene name synonym)\tuniprotkb:Dst(gene name synonym)"
            + "\tintact:Nfh(gene name)\tintact:Bpag1(gene name)\tMI:0018(2 hybrid)\tLeung et al. (1999)\tpubmed:9971739"
            + "\ttaxid:10116(rat)\ttaxid:10090(mouse)\tMI:0218(physical interaction)\tMI:0469(intact)"
            + "\tintact:EBI-446356|irefindex:arigidblabla(rigid)\t-\t-\tMI:0499(unspecified role)"
            + "\tMI:0499(unspecified role)\tMI:0498(prey)\tMI:0496(bait)\tMI:0326(protein)\tMI:0326(protein)\tinterpro:IPR004829|\t-\t-\t-\t-\tgo:\"GO:0030246\"\tyeast:4932\t-\t-\t-\t-"
            + "\t-\t-\t-\t-\t-\t-\t-\t-\t-";

    SolrDocumentConverter converter = new SolrDocumentConverter(getSolrServer());
    SolrInputDocument doc = converter.toSolrDocument(psiMiTabLine);

    final SolrInputField field = doc.getField("geneName");
    Assert.assertEquals(2, field.getValueCount());
    Assert.assertTrue(field.getValues().contains("Nfh"));
    Assert.assertTrue(field.getValues().contains("Bpag1"));

}