Created
June 4, 2013 22:11
-
-
Save fforbeck/5710060 to your computer and use it in GitHub Desktop.
SolrConnector using solrj
This file contains hidden or 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
| import java.io.IOException; | |
| import java.util.List; | |
| import org.apache.solr.client.solrj.SolrQuery; | |
| import org.apache.solr.client.solrj.SolrServer; | |
| import org.apache.solr.client.solrj.SolrServerException; | |
| import org.apache.solr.client.solrj.impl.HttpSolrServer; | |
| import org.apache.solr.client.solrj.response.QueryResponse; | |
| import org.apache.solr.common.SolrInputDocument; | |
| public enum SolrConnector { | |
| INSTANCE; | |
| private final SolrServer solrServer; | |
| private SolrConnector() { | |
| solrServer = new HttpSolrServer("http://localhost:8983/solr"); | |
| } | |
| public long count(String query) { | |
| long count; | |
| try { | |
| final QueryResponse response = solrServer.query(new SolrQuery(query)); | |
| count = response.getResults().getNumFound(); | |
| } catch (SolrServerException e) { | |
| e.printStackTrace(); | |
| count = 0; | |
| } | |
| return count; | |
| } | |
| public void addDocument(SolrInputDocument doc) { | |
| try { | |
| solrServer.add(doc, 50); | |
| } catch (SolrServerException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public void addDocuments(List<SolrInputDocument> docs) { | |
| try { | |
| solrServer.add(docs, 50); | |
| } catch (SolrServerException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependencies:
org.apache.solr
solr-core
4.3.0
org.apache.solr
solr-solrj
4.3.0