use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class XMLLoader method readDoc.
/**
* Given the input stream, read a document
*
* @since solr 1.3
*/
public SolrInputDocument readDoc(XMLStreamReader parser) throws XMLStreamException {
SolrInputDocument doc = new SolrInputDocument();
String attrName = "";
for (int i = 0; i < parser.getAttributeCount(); i++) {
attrName = parser.getAttributeLocalName(i);
if ("boost".equals(attrName)) {
String message = "Ignoring document boost: " + parser.getAttributeValue(i) + " as index-time boosts are not supported anymore";
if (WARNED_ABOUT_INDEX_TIME_BOOSTS.compareAndSet(false, true)) {
log.warn(message);
} else {
log.debug(message);
}
} else {
log.warn("XML element <doc> has invalid XML attr:" + attrName);
}
}
StringBuilder text = new StringBuilder();
String name = null;
boolean isNull = false;
String update = null;
Collection<SolrInputDocument> subDocs = null;
Map<String, Map<String, Object>> updateMap = null;
boolean complete = false;
while (!complete) {
int event = parser.next();
switch(event) {
// Add everything to the text
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
case XMLStreamConstants.CHARACTERS:
text.append(parser.getText());
break;
case XMLStreamConstants.END_ELEMENT:
if ("doc".equals(parser.getLocalName())) {
if (subDocs != null && !subDocs.isEmpty()) {
doc.addChildDocuments(subDocs);
subDocs = null;
}
complete = true;
break;
} else if ("field".equals(parser.getLocalName())) {
// should I warn in some text has been found too
Object v = isNull ? null : text.toString();
if (update != null) {
if (updateMap == null)
updateMap = new HashMap<>();
Map<String, Object> extendedValues = updateMap.get(name);
if (extendedValues == null) {
extendedValues = new HashMap<>(1);
updateMap.put(name, extendedValues);
}
Object val = extendedValues.get(update);
if (val == null) {
extendedValues.put(update, v);
} else {
// multiple val are present
if (val instanceof List) {
List list = (List) val;
list.add(v);
} else {
List<Object> values = new ArrayList<>();
values.add(val);
values.add(v);
extendedValues.put(update, values);
}
}
break;
}
doc.addField(name, v);
// field is over
name = null;
}
break;
case XMLStreamConstants.START_ELEMENT:
text.setLength(0);
String localName = parser.getLocalName();
if ("doc".equals(localName)) {
if (subDocs == null)
subDocs = Lists.newArrayList();
subDocs.add(readDoc(parser));
} else {
if (!"field".equals(localName)) {
String msg = "XML element <doc> has invalid XML child element: " + localName;
log.warn(msg);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
}
update = null;
isNull = false;
String attrVal = "";
for (int i = 0; i < parser.getAttributeCount(); i++) {
attrName = parser.getAttributeLocalName(i);
attrVal = parser.getAttributeValue(i);
if (NAME.equals(attrName)) {
name = attrVal;
} else if ("boost".equals(attrName)) {
String message = "Ignoring field boost: " + attrVal + " as index-time boosts are not supported anymore";
if (WARNED_ABOUT_INDEX_TIME_BOOSTS.compareAndSet(false, true)) {
log.warn(message);
} else {
log.debug(message);
}
} else if ("null".equals(attrName)) {
isNull = StrUtils.parseBoolean(attrVal);
} else if ("update".equals(attrName)) {
update = attrVal;
} else {
log.warn("XML element <field> has invalid XML attr: " + attrName);
}
}
}
break;
}
}
if (updateMap != null) {
for (Map.Entry<String, Map<String, Object>> entry : updateMap.entrySet()) {
name = entry.getKey();
Map<String, Object> value = entry.getValue();
doc.addField(name, value);
}
}
return doc;
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class SingleThreadedCSVLoader method addDoc.
@Override
void addDoc(int line, String[] vals) throws IOException {
templateAdd.clear();
SolrInputDocument doc = new SolrInputDocument();
doAdd(line, vals, doc, templateAdd);
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class JavabinLoader method parseAndLoadDocs.
private void parseAndLoadDocs(final SolrQueryRequest req, SolrQueryResponse rsp, InputStream stream, final UpdateRequestProcessor processor) throws IOException {
UpdateRequest update = null;
JavaBinUpdateRequestCodec.StreamingUpdateHandler handler = new JavaBinUpdateRequestCodec.StreamingUpdateHandler() {
private AddUpdateCommand addCmd = null;
@Override
public void update(SolrInputDocument document, UpdateRequest updateRequest, Integer commitWithin, Boolean overwrite) {
if (document == null) {
// Perhaps commit from the parameters
try {
RequestHandlerUtils.handleCommit(req, processor, updateRequest.getParams(), false);
RequestHandlerUtils.handleRollback(req, processor, updateRequest.getParams(), false);
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR handling commit/rollback");
}
return;
}
if (addCmd == null) {
addCmd = getAddCommand(req, updateRequest.getParams());
}
addCmd.solrDoc = document;
if (commitWithin != null) {
addCmd.commitWithin = commitWithin;
}
if (overwrite != null) {
addCmd.overwrite = overwrite;
}
if (updateRequest.isLastDocInBatch()) {
// this is a hint to downstream code that indicates we've sent the last doc in a batch
addCmd.isLastDocInBatch = true;
}
try {
processor.processAdd(addCmd);
addCmd.clear();
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR adding document " + document, e);
}
}
};
FastInputStream in = FastInputStream.wrap(stream);
for (; ; ) {
try {
update = new JavaBinUpdateRequestCodec().unmarshal(in, handler);
} catch (EOFException e) {
// this is expected
break;
}
if (update.getDeleteByIdMap() != null || update.getDeleteQuery() != null) {
delete(req, update, processor);
}
}
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class TestSolrEntityProcessorEndToEnd method addDocumentsToSolr.
private void addDocumentsToSolr(List<Map<String, Object>> docs) throws SolrServerException, IOException {
List<SolrInputDocument> sidl = new ArrayList<>();
for (Map<String, Object> doc : docs) {
SolrInputDocument sd = new SolrInputDocument();
for (Entry<String, Object> entry : doc.entrySet()) {
sd.addField(entry.getKey(), entry.getValue());
}
sidl.add(sd);
}
try (HttpSolrClient solrServer = getHttpSolrClient(getSourceUrl())) {
solrServer.setConnectionTimeout(15000);
solrServer.setSoTimeout(30000);
solrServer.add(sidl);
solrServer.commit(true, true);
}
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class TestDocumentBuilder method testDeepCopy.
@Test
public void testDeepCopy() throws IOException {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("field1", "value1");
doc.addField("field2", "value1");
doc.addField("field3", "value2");
doc.addField("field4", 15);
List<Integer> list = new ArrayList<>();
list.add(45);
list.add(33);
list.add(20);
doc.addField("field5", list);
SolrInputDocument clone = doc.deepCopy();
System.out.println("doc1: " + doc);
System.out.println("clone: " + clone);
assertNotSame(doc, clone);
Collection<String> fieldNames = doc.getFieldNames();
for (String name : fieldNames) {
Collection<Object> values = doc.getFieldValues(name);
Collection<Object> cloneValues = clone.getFieldValues(name);
assertEquals(values.size(), cloneValues.size());
assertNotSame(values, cloneValues);
Iterator<Object> cloneIt = cloneValues.iterator();
for (Object value : values) {
Object cloneValue = cloneIt.next();
assertSame(value, cloneValue);
}
}
}
Aggregations