Search in sources :

Example 31 with SolrInputDocument

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;
}
Also used : HashMap(java.util.HashMap) SolrInputDocument(org.apache.solr.common.SolrInputDocument) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) SolrException(org.apache.solr.common.SolrException)

Example 32 with SolrInputDocument

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);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument)

Example 33 with SolrInputDocument

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);
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) EOFException(java.io.EOFException) IOException(java.io.IOException) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec) SolrException(org.apache.solr.common.SolrException) FastInputStream(org.apache.solr.common.util.FastInputStream)

Example 34 with SolrInputDocument

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);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ArrayList(java.util.ArrayList)

Example 35 with SolrInputDocument

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);
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

SolrInputDocument (org.apache.solr.common.SolrInputDocument)520 Test (org.junit.Test)166 ArrayList (java.util.ArrayList)98 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)76 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)69 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)63 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)55 SolrQuery (org.apache.solr.client.solrj.SolrQuery)54 IOException (java.io.IOException)47 SolrException (org.apache.solr.common.SolrException)47 IndexSchema (org.apache.solr.schema.IndexSchema)41 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)41 HashMap (java.util.HashMap)39 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)34 List (java.util.List)33 SolrServerException (org.apache.solr.client.solrj.SolrServerException)32 SolrDocument (org.apache.solr.common.SolrDocument)31 NamedList (org.apache.solr.common.util.NamedList)31 Map (java.util.Map)30 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)29