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);
}
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;
}
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;
}
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]);
}
}
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"));
}
}
Aggregations