List of usage examples for org.apache.solr.client.solrj.request UpdateRequest getDocuments
public List<SolrInputDocument> getDocuments()
From source file:org.codelibs.elasticsearch.solr.solr.JavaBinUpdateRequestCodec.java
License:Apache License
/** * Converts an UpdateRequest to a NamedList which can be serialized to the * given OutputStream in the javabin format * * @param updateRequest//from www. j a va 2 s . c o m * the UpdateRequest to be written out * @param os * the OutputStream to which the request is to be written * * @throws IOException * in case of an exception during marshalling or writing to the * stream */ public void marshal(final UpdateRequest updateRequest, final OutputStream os) throws IOException { final NamedList nl = new NamedList(); final NamedList params = solrParamsToNamedList(updateRequest.getParams()); if (updateRequest.getCommitWithin() != -1) { params.add("commitWithin", updateRequest.getCommitWithin()); } Iterator<SolrInputDocument> docIter = null; if (updateRequest.getDocuments() != null) { docIter = updateRequest.getDocuments().iterator(); } if (updateRequest.getDocIterator() != null) { docIter = updateRequest.getDocIterator(); } nl.add("params", params);// 0: params nl.add("delById", updateRequest.getDeleteById()); nl.add("delByQ", updateRequest.getDeleteQuery()); nl.add("docs", docIter); final JavaBinCodec codec = new JavaBinCodec(); codec.marshal(nl, os); }
From source file:org.teiid.translator.solr.SolrUpdateExecution.java
License:Open Source License
/** * Did not find any other suitable way to pass the query through solrj otherthan walking the documents, * all the examples were at the passing xml based query. so that would be a good update if the below does * not performs or gets into OOM/*from w w w.j a v a2 s.co m*/ * @param obj * @throws TranslatorException */ private void performUpdate(final Update obj) throws TranslatorException { if (obj.getParameterValues() != null) { throw new TranslatorException(SolrPlugin.Event.TEIID20009, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20009)); } SolrQueryExecution query = new SolrQueryExecution(ef, obj, this.executionContext, this.metadata, this.connection); query.execute(); final UpdateRequest request = new UpdateRequest(); query.walkDocuments(new SolrDocumentCallback() { @Override public void walk(SolrDocument doc) { SolrUpdateExecution.this.updateCount++; Table table = obj.getTable().getMetadataObject(); SolrInputDocument updateDoc = new SolrInputDocument(); for (String name : doc.getFieldNames()) { if (table.getColumnByName(name) != null) { updateDoc.setField(name, doc.getFieldValue(name)); } } int elementCount = obj.getChanges().size(); for (int i = 0; i < elementCount; i++) { String columnName = SolrSQLHierarchyVistor.getColumnName(obj.getChanges().get(i).getSymbol()); Literal value = (Literal) obj.getChanges().get(i).getValue(); updateDoc.setField(columnName, value.getValue()); } request.add(updateDoc); } }); if (request.getDocuments() != null && !request.getDocuments().isEmpty()) { UpdateResponse response = this.connection.update(request); if (response.getStatus() != 0) { throw new TranslatorException(SolrPlugin.Event.TEIID20004, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20004, response.getStatus())); } } }
From source file:org.teiid.translator.solr.SolrUpdateExecution.java
License:Open Source License
private void performInsert(Insert insert) throws TranslatorException { // build insert List<ColumnReference> columns = insert.getColumns(); if (insert.getParameterValues() == null) { final UpdateRequest request = new UpdateRequest(); SolrInputDocument doc = new SolrInputDocument(); List<Expression> values = ((ExpressionValueSource) insert.getValueSource()).getValues(); for (int i = 0; i < columns.size(); i++) { String columnName = SolrSQLHierarchyVistor.getColumnName(columns.get(i)); Object value = values.get(i); if (value instanceof Literal) { doc.addField(columnName, ((Literal) value).getValue()); } else { throw new TranslatorException(SolrPlugin.Event.TEIID20002, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20002)); }/* w w w. j a v a 2 s .co m*/ } this.updateCount++; request.add(doc); // check if the row already exists Select q = buildSelectQuery(insert); SolrQueryExecution query = new SolrQueryExecution(ef, q, this.executionContext, this.metadata, this.connection); query.execute(); query.walkDocuments(new SolrDocumentCallback() { @Override public void walk(SolrDocument doc) { request.clear(); } }); if (request.getDocuments().isEmpty()) { throw new TranslatorException(SolrPlugin.Event.TEIID20007, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20007)); } // write the mutation UpdateResponse response = this.connection.update(request); if (response.getStatus() != 0) { throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus())); } } else { UpdateRequest request = new UpdateRequest(); int batchSize = 1024; // bulk insert; should help Iterator<? extends List<?>> args = insert.getParameterValues(); while (args.hasNext()) { List<?> arg = args.next(); SolrInputDocument doc = new SolrInputDocument(); for (int i = 0; i < columns.size(); i++) { String columnName = SolrSQLHierarchyVistor.getColumnName(columns.get(i)); doc.addField(columnName, arg.get(i)); } this.updateCount++; request.add(doc); if ((this.updateCount % batchSize) == 0) { UpdateResponse response = this.connection.update(request); if (response.getStatus() != 0) { throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus())); } request = new UpdateRequest(); } } if (request.getDocuments() != null && !request.getDocuments().isEmpty()) { // write the mutation UpdateResponse response = this.connection.update(request); if (response.getStatus() != 0) { throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus())); } } } }
From source file:org.teiid.translator.solr.TestSolrUpdateExecution.java
License:Open Source License
@Test public void testSimpleInsert() throws Exception { String query = "insert into example (price, weight, popularity, name, field) " + "VALUES ('1.10', '2.23', 5, 'teiid', 'any')"; SolrInputDocument insert = new SolrInputDocument(); insert.addField("price", 1.10f); insert.addField("weight", 2.23f); insert.addField("popularity", 5); insert.addField("name", "teiid"); insert.addField("nis", "any"); QueryResponse queryResponse = Mockito.mock(QueryResponse.class); Mockito.stub(queryResponse.getResults()).toReturn(new SolrDocumentList()); QueryResponse queryResponse2 = Mockito.mock(QueryResponse.class); Mockito.stub(queryResponse2.getResults()).toReturn(new SolrDocumentList()); UpdateRequest request = helpUpdate(query, queryResponse, queryResponse2); List<SolrInputDocument> docs = request.getDocuments(); assertEquals(1, docs.size());/*from w ww. j av a2s . c om*/ assertEquals(insert.toString(), docs.get(0).toString()); }
From source file:org.teiid.translator.solr.TestSolrUpdateExecution.java
License:Open Source License
@Test public void testSimpleUpdate() throws Exception { String query = "Update example set field = 'some' where name = 'teiid'"; SolrDocument doc = new SolrDocument(); doc.addField("price", 1.10f); doc.addField("weight", 2.23f); doc.addField("popularity", 5); doc.addField("name", "teiid"); doc.addField("nis", "any"); SolrDocumentList list = new SolrDocumentList(); list.add(doc);/*from w ww . ja v a2 s. c o m*/ QueryResponse queryResponse = Mockito.mock(QueryResponse.class); Mockito.stub(queryResponse.getResults()).toReturn(list); QueryResponse queryResponse2 = Mockito.mock(QueryResponse.class); Mockito.stub(queryResponse2.getResults()).toReturn(new SolrDocumentList()); UpdateRequest request = helpUpdate(query, queryResponse, queryResponse2); List<SolrInputDocument> docs = request.getDocuments(); assertEquals(1, docs.size()); SolrInputDocument update = new SolrInputDocument(); update.addField("price", 1.10f); update.addField("weight", 2.23f); update.addField("popularity", 5); update.addField("name", "teiid"); update.addField("nis", "some"); assertEquals(update.toString(), docs.get(0).toString()); }
From source file:org.teiid.translator.solr.TestSolrUpdateExecution.java
License:Open Source License
@Test public void testSimpleDelete() throws Exception { String query = "Delete from example where name = 'teiid'"; SolrDocument doc = new SolrDocument(); doc.addField("price", 1.10f); doc.addField("weight", 2.23f); doc.addField("popularity", 5); doc.addField("name", "teiid"); doc.addField("nis", "any"); SolrDocumentList list = new SolrDocumentList(); list.add(doc);/*from w ww . j a va 2s.com*/ QueryResponse queryResponse = Mockito.mock(QueryResponse.class); Mockito.stub(queryResponse.getResults()).toReturn(list); QueryResponse queryResponse2 = Mockito.mock(QueryResponse.class); Mockito.stub(queryResponse2.getResults()).toReturn(new SolrDocumentList()); UpdateRequest request = helpUpdate(query, queryResponse, queryResponse2); List<SolrInputDocument> docs = request.getDocuments(); UpdateRequest expected = new UpdateRequest(); expected.deleteById("teiid"); assertEquals(expected.getDeleteById().toString(), request.getDeleteById().toString()); }
From source file:ru.org.linux.search.SearchQueueListener.java
License:Apache License
public void handleMessage(UpdateComments msgUpdate) throws MessageNotFoundException, IOException, SolrServerException { logger.info("Indexing comments " + msgUpdate.getMsgids()); UpdateRequest rq = new UpdateRequest(); rq.setCommitWithin(10000);//from w ww . j a v a 2s . com boolean delete = false; for (Integer msgid : msgUpdate.getMsgids()) { if (msgid == 0) { logger.warn("Skipping MSGID=0!!!"); continue; } Comment comment = commentService.getById(msgid); if (comment.isDeleted()) { logger.info("Deleting comment " + comment.getId() + " from solr"); solrServer.deleteById(Integer.toString(comment.getId())); delete = true; } else { // ? ?? ? // ? ?? - , .. ? // Topic topic = topicDao.getById(comment.getTopicId()); String message = msgbaseDao.getMessageText(comment.getId()).getText(); rq.add(processComment(topic, comment, message)); } } if (rq.getDocuments() != null && !rq.getDocuments().isEmpty()) { rq.process(solrServer); } if (delete) { solrServer.commit(); } }
From source file:sk.datalan.solr.impl.ConcurrentUpdateSolrServer.java
License:Apache License
@Override public NamedList<Object> request(final SolrRequest request) throws SolrServerException, IOException { if (!(request instanceof UpdateRequest)) { return server.request(request); }/* ww w. j av a 2 s.c o m*/ UpdateRequest req = (UpdateRequest) request; // this happens for commit... if (streamDeletes) { if ((req.getDocuments() == null || req.getDocuments().isEmpty()) && (req.getDeleteById() == null || req.getDeleteById().isEmpty()) && (req.getDeleteByIdMap() == null || req.getDeleteByIdMap().isEmpty())) { if (req.getDeleteQuery() == null) { blockUntilFinished(); return server.request(request); } } } else { if ((req.getDocuments() == null || req.getDocuments().isEmpty())) { blockUntilFinished(); return server.request(request); } } SolrParams params = req.getParams(); if (params != null) { // check if it is waiting for the searcher if (params.getBool(UpdateParams.WAIT_SEARCHER, false)) { log.info("blocking for commit/optimize"); blockUntilFinished(); // empty the queue return server.request(request); } } try { CountDownLatch tmpLock = lock; if (tmpLock != null) { tmpLock.await(); } boolean success = queue.offer(req); for (;;) { synchronized (runners) { if (runners.isEmpty() || (queue.remainingCapacity() < queue.size() // queue // is // half // full // and // we // can // add // more // runners && runners.size() < threadCount)) { // We need more runners, so start a new one. Runner r = new Runner(); runners.add(r); scheduler.execute(r); } else { // break out of the retry loop if we added the element to the queue // successfully, *and* // while we are still holding the runners lock to prevent race // conditions. if (success) break; } } // Retry to add to the queue w/o the runners lock held (else we risk // temporary deadlock) // This retry could also fail because // 1) existing runners were not able to take off any new elements in the // queue // 2) the queue was filled back up since our last try // If we succeed, the queue may have been completely emptied, and all // runners stopped. // In all cases, we should loop back to the top to see if we need to // start more runners. // if (!success) { success = queue.offer(req, 100, TimeUnit.MILLISECONDS); } } } catch (InterruptedException e) { log.error("interrupted", e); throw new IOException(e.getLocalizedMessage()); } // RETURN A DUMMY result NamedList<Object> dummy = new NamedList<>(); dummy.add("NOTE", "the request is processed in a background stream"); return dummy; }