I want my servlet to establish a connection with solr so as to perform query. using eclipse I have created a simple JSP file for the user to input their query, then it will call the doGet() method from the servlet. From the servlet i try to use solrj to connect with solr
my solrj code is as below.
HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
SolrQuery query = new SolrQuery();
query.setQuery("sony digital camera");
query.addFilterQuery("cat:electronics","store:amazon.com");
query.setFields("id","price","merchant","cat","store");
query.setStart(0);
query.set("defType", "edismax");
QueryResponse response = solr.query(query);
SolrDocumentList results = response.getResults();
for (int i = 0; i < results.size(); ++i) {
System.out.println(results.get(i));
}
}
Its basically just simple code available online, there is no syntax error which means that the program detects the library files. However on run time the program returns classNotFoundException error.
This prompt me thinking whether i can implement solrj on servlet. Anyone can advise me where i can exactly use the solrj code? I think that the code is right but just that i implement solrj wrongly.
Thanks in advance!
The exception is saying that your runtime classpath is wrong.
Assuming you're running this class in a servlet container, most probably what you're missing is not a Java EE jar but instead has something to do with SolrJ.
Solrj has few dependencies: you can find them in the Solr distribution or if you have confidence with Apache Maven, you can use it for individuating those jars.
In any case, once you identified those libraries, you have to put them under WEB-INF/lib of your webapp
For a list of dependencies you can read here: http://mvnrepository.com/artifact/org.apache.solr/solr-solrj/4.10.3