Search code examples
solrsolrj

Prompt Solr Query using SolrJ


I am trying to structure a solr query using solrj. From my understanding of Solr-7.5.0 all libraries and dependencies for solrj should be included in my solr installation. Below is my /dist folder followed by my /dist/solrj-lib folder

dist

dist/solrj-lib

Now, my query will be tied to an html post form but I want to get the solrj working first. Here is the entirety of my solrj

    import org.apache.solr.client.solrj.SolrClient;
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.HttpSolrClient;

    String queryInput = q;
    SolrQuery query = new SolrQuery(queryInput);
    SolrClient client = new HttpSolrClient.Builder (
        "http://localhost:8983/solr/sqltest")
        .build();
    QueryResponse response = client.query(query);
    SolrDocumentList list = response.getResults();

This will not compile because it does not recognize my classes (SolrClient, SolrQuery, etc). I am sure there are countless issues right now but Im not sure where to start. As a side question, I was looking into other APIs that are not in compiled code. Would one of these be better used for a web application or is it safe to use solrs native solrj?


Solution

  • You can safely use SolrJ - in fact, if you're writing a Java application, it's the recommended way of interacting with Solr. See Using SolrJ - Building and Running SolrJ Applications for further details than what I've given below.

    I've assumed you're on Windows from your screenshots - if you're on Linux or OS X, replace ; with : as the path separator below.

    To include the SolrJ jar, you need to include a -classpath ".;path-to-solr-solrj-7.5.0.jar argument when running javac. This is the only jar required to compile your application.

    When running it you'll have to include the jars found in the solrj-lib path. You can do this by including a class path argument to java with your files. Use java -classpath ".;path-to-solrj-lib\* to include all the jar files in the directory.