private static SolrDocument toSolrDocument(SolrInputDocument d) {
   SolrDocument doc = new SolrDocument();
   for (String name : d.getFieldNames()) {
     doc.addField(name, d.getFieldValue(name));
   }
   return doc;
 }
  public void testSolrDocumentEquals() {

    String randomString = TestUtil.randomSimpleString(random());

    SolrDocument doc1 = new SolrDocument();
    doc1.addField("foo", randomString);

    SolrDocument doc2 = new SolrDocument();
    doc2.addField("foo", randomString);

    assertTrue(assertSolrDocumentEquals(doc1, doc2));

    doc1.addField("foo", "bar");

    assertFalse(assertSolrDocumentEquals(doc1, doc2));

    doc1 = new SolrDocument();
    doc1.addField("bar", randomString);

    assertFalse(assertSolrDocumentEquals(doc1, doc2));

    int randomInt = random().nextInt();
    doc1 = new SolrDocument();
    doc1.addField("foo", randomInt);
    doc2 = new SolrDocument();
    doc2.addField("foo", randomInt);

    assertTrue(assertSolrDocumentEquals(doc1, doc2));

    doc2 = new SolrDocument();
    doc2.addField("bar", randomInt);

    assertFalse(assertSolrDocumentEquals(doc1, doc2));
  }
  public SolrDocument doc2SolrDoc(Document doc) {
    SolrDocument solrDoc = new SolrDocument();
    for (IndexableField field : doc) {
      String fieldName = field.name();
      SchemaField sf =
          getSchemaField(
              fieldName); // hack-patch of this.core.getLatestSchema().getFieldOrNull(fieldName);
                          // makes it a lot faster!!
      Object val = null;
      try {
        FieldType ft = null;
        if (sf != null) ft = sf.getType();
        if (ft == null) {
          BytesRef bytesRef = field.binaryValue();
          if (bytesRef != null) {
            if (bytesRef.offset == 0 && bytesRef.length == bytesRef.bytes.length) {
              val = bytesRef.bytes;
            } else {
              final byte[] bytes = new byte[bytesRef.length];
              System.arraycopy(bytesRef.bytes, bytesRef.offset, bytes, 0, bytesRef.length);
              val = bytes;
            }
          } else {
            val = field.stringValue();
          }
        } else {
          val = ft.toObject(field);
        }
      } catch (Throwable e) {
        continue;
      }

      if (sf != null && sf.multiValued() && !solrDoc.containsKey(fieldName)) {
        ArrayList<Object> l = new ArrayList<Object>();
        l.add(val);
        solrDoc.addField(fieldName, l);
      } else {
        solrDoc.addField(fieldName, val);
      }
    }
    return solrDoc;
  }
Exemple #4
0
 public static final SolrDocument toSolrDocument(Document doc, final IndexSchema schema) {
   SolrDocument out = new SolrDocument();
   for (IndexableField f : doc.getFields()) {
     // Make sure multivalued fields are represented as lists
     Object existing = out.get(f.name());
     if (existing == null) {
       SchemaField sf = schema.getFieldOrNull(f.name());
       if (sf != null && sf.multiValued()) {
         List<Object> vals = new ArrayList<>();
         vals.add(f);
         out.setField(f.name(), vals);
       } else {
         out.setField(f.name(), f);
       }
     } else {
       out.addField(f.name(), f);
     }
   }
   return out;
 }
  private static SolrDocument toSolrDoc(StoredDocument doc, IndexSchema schema) {
    SolrDocument out = new SolrDocument();
    for (StorableField f : doc.getFields()) {
      // Make sure multivalued fields are represented as lists
      Object existing = out.get(f.name());
      if (existing == null) {
        SchemaField sf = schema.getFieldOrNull(f.name());

        // don't return copyField targets
        if (sf != null && schema.isCopyFieldTarget(sf)) continue;

        if (sf != null && sf.multiValued()) {
          List<Object> vals = new ArrayList<Object>();
          vals.add(f);
          out.setField(f.name(), vals);
        } else {
          out.setField(f.name(), f);
        }
      } else {
        out.addField(f.name(), f);
      }
    }
    return out;
  }
  protected SolrDocument readDocument(XMLStreamReader parser) throws XMLStreamException {
    if (XMLStreamConstants.START_ELEMENT != parser.getEventType()) {
      throw new RuntimeException("must be start element, not: " + parser.getEventType());
    }
    if (!"doc".equals(parser.getLocalName().toLowerCase(Locale.ENGLISH))) {
      throw new RuntimeException("must be 'lst', not: " + parser.getLocalName());
    }

    SolrDocument doc = new SolrDocument();
    StringBuilder builder = new StringBuilder();
    KnownType type = null;
    String name = null;

    // just eat up the events...
    int depth = 0;
    while (true) {
      switch (parser.next()) {
        case XMLStreamConstants.START_ELEMENT:
          depth++;
          builder.setLength(0); // reset the text
          type = KnownType.get(parser.getLocalName());
          if (type == null) {
            throw new RuntimeException("this must be known type! not: " + parser.getLocalName());
          }

          name = null;
          int cnt = parser.getAttributeCount();
          for (int i = 0; i < cnt; i++) {
            if ("name".equals(parser.getAttributeLocalName(i))) {
              name = parser.getAttributeValue(i);
              break;
            }
          }

          if (name == null) {
            throw new XMLStreamException(
                "requires 'name' attribute: " + parser.getLocalName(), parser.getLocation());
          }

          // Handle multi-valued fields
          if (type == KnownType.ARR) {
            for (Object val : readArray(parser)) {
              doc.addField(name, val);
            }
            depth--; // the array reading clears out the 'endElement'
          } else if (!type.isLeaf) {
            throw new XMLStreamException("must be value or array", parser.getLocation());
          }
          break;

        case XMLStreamConstants.END_ELEMENT:
          if (--depth < 0) {
            return doc;
          }
          // System.out.println( "FIELD:"+type+"::"+name+"::"+builder );
          Object val = type.read(builder.toString().trim());
          if (val == null) {
            throw new XMLStreamException("error reading value:" + type, parser.getLocation());
          }
          doc.addField(name, val);
          break;

        case XMLStreamConstants
            .SPACE: // TODO?  should this be trimmed? make sure it only gets one/two space?
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
          builder.append(parser.getText());
          break;
      }
    }
  }
 protected void addHoldingsInfoToBib(
     SolrInputDocument solrInputDocument, SolrDocument solrBibDocument) {
   solrBibDocument.addField(URI_SEARCH, solrInputDocument.getFieldValue(URI_SEARCH));
 }