Last active
July 13, 2016 15:13
-
-
Save mumrah/4627896 to your computer and use it in GitHub Desktop.
Example of batching and multi-threading Solr updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SolrUpdater implements Runnable { | |
private final UpdateRequest req = new UpdateRequest(); | |
private final SolrServer solr; | |
private final BlockingQueue<String> strings; | |
private final AtomicLong id; | |
private final int batchSize = 100; | |
private volatile int batchedUpdates = 0; | |
public SolrUpdater(SolrServer solr, BlockingQueue<String> strings, | |
AtomicLong id) { | |
this.solr = solr; | |
this.strings = strings; | |
this.id = id; | |
} | |
@Override | |
public void run() { | |
while(true) { | |
String text = strings.take(); | |
SolrInputDocument doc = new SolrInputDocument(); | |
doc.setField("id", "doc-" + id.getAndIncrement()); | |
doc.setField("text", text); | |
req.add(doc); | |
batchedUpdates++; | |
if(batchedUpdates >= batchSize) { | |
req.process(solr); | |
req.clear(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment