Search in sources :

Example 51 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class SolrClient method addBeans.

/**
   * Adds the beans supplied by the given iterator.
   *
   * @param collection the Solr collection to add the documents to
   * @param beanIterator
   *          the iterator which returns Beans
   *
   * @return an {@link org.apache.solr.client.solrj.response.UpdateResponse} from the server
   *
   * @throws IOException         if there is a communication error with the server
   * @throws SolrServerException if there is an error on the server
   */
public UpdateResponse addBeans(String collection, final Iterator<?> beanIterator) throws SolrServerException, IOException {
    UpdateRequest req = new UpdateRequest();
    req.setDocIterator(new Iterator<SolrInputDocument>() {

        @Override
        public boolean hasNext() {
            return beanIterator.hasNext();
        }

        @Override
        public SolrInputDocument next() {
            Object o = beanIterator.next();
            if (o == null)
                return null;
            return getBinder().toSolrInputDocument(o);
        }

        @Override
        public void remove() {
            beanIterator.remove();
        }
    });
    return req.process(this, collection);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest)

Example 52 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class DocumentObjectBinder method toSolrInputDocument.

public SolrInputDocument toSolrInputDocument(Object obj) {
    List<DocField> fields = getDocFields(obj.getClass());
    if (fields.isEmpty()) {
        throw new BindingException("class: " + obj.getClass() + " does not define any fields.");
    }
    SolrInputDocument doc = new SolrInputDocument();
    for (DocField field : fields) {
        if (field.dynamicFieldNamePatternMatcher != null && field.get(obj) != null && field.isContainedInMap) {
            Map<String, Object> mapValue = (Map<String, Object>) field.get(obj);
            for (Map.Entry<String, Object> e : mapValue.entrySet()) {
                doc.setField(e.getKey(), e.getValue());
            }
        } else {
            if (field.child != null) {
                addChild(obj, field, doc);
            } else {
                doc.setField(field.name, field.get(obj));
            }
        }
    }
    return doc;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 53 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class CloudSolrClient method hasInfoToFindLeaders.

private static boolean hasInfoToFindLeaders(UpdateRequest updateRequest, String idField) {
    final Map<SolrInputDocument, Map<String, Object>> documents = updateRequest.getDocumentsMap();
    final Map<String, Map<String, Object>> deleteById = updateRequest.getDeleteByIdMap();
    final boolean hasNoDocuments = (documents == null || documents.isEmpty());
    final boolean hasNoDeleteById = (deleteById == null || deleteById.isEmpty());
    if (hasNoDocuments && hasNoDeleteById) {
        // no documents and no delete-by-id, so no info to find leader(s)
        return false;
    }
    if (documents != null) {
        for (final Map.Entry<SolrInputDocument, Map<String, Object>> entry : documents.entrySet()) {
            final SolrInputDocument doc = entry.getKey();
            final Object fieldValue = doc.getFieldValue(idField);
            if (fieldValue == null) {
                // a document with no id field value, so can't find leader for it
                return false;
            }
        }
    }
    return true;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 54 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    DebugServlet.clear();
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
        UpdateRequest req = new UpdateRequest();
        req.add(new SolrInputDocument());
        req.setParam("a", "ሴ");
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        //default method
        assertEquals("post", DebugServlet.lastMethod);
        //agent
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        //default wt
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        //default version
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        //content type
        assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
        //parameter encoding
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        //XML response and writer
        client.setParser(new XMLResponseParser());
        client.setRequestWriter(new RequestWriter());
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals("application/xml; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        //javabin request
        client.setParser(new BinaryResponseParser());
        client.setRequestWriter(new BinaryRequestWriter());
        DebugServlet.clear();
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ParseException(org.apache.http.ParseException) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) Test(org.junit.Test)

Example 55 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class CloudSolrClientTest method testHandlingOfStaleAlias.

@Test
public void testHandlingOfStaleAlias() throws Exception {
    CloudSolrClient client = getRandomClient();
    CollectionAdminRequest.createCollection("nemesis", "conf", 2, 1).process(client);
    CollectionAdminRequest.createAlias("misconfigured-alias", "nemesis").process(client);
    CollectionAdminRequest.deleteCollection("nemesis").process(client);
    List<SolrInputDocument> docs = new ArrayList<>();
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField(id, Integer.toString(1));
    docs.add(doc);
    try {
        client.add("misconfigured-alias", docs);
        fail("Alias points to non-existing collection, add should fail");
    } catch (SolrException e) {
        assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, e.code());
        assertTrue("Unexpected exception", e.getMessage().contains("Collection not found"));
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ArrayList(java.util.ArrayList) SolrException(org.apache.solr.common.SolrException) 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