0
votes

I am using httpclient to query solr server from java code. Now I just want to convert all of my code using solrj library. I am not able to find any proper documentation for it. My existing code is like this

HttpURLConnection conn = (HttpURLConnection) new URL(url.toString()).openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "*/*");
conn.setInstanceFollowRedirects(false);
OutputStream os = conn.getOutputStream();
os.write(inputJson.getBytes());
os.flush();
os.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//read resp from rd

How to do the same using solrj? Here is documentation for the same to do by using curl JSON Request API

I want to send inputJson String as request body. This Json String may contain whatever dynamic key values. It is doable as I have shown using http rest call. But how to do same using solrj?

1

1 Answers

0
votes

Here is a simple query example via SolrJ:

package eric.search.solr.solrj;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

/**
 * SolrJ test - solr5
 * 
 * @author eric
 * @date Aug 7, 2015 3:56:08 PM
 */
public class Solrj5Test {
    // query via edismax - test
    public static void queryEdismaxTest() {
        String coreUrl = "http://localhost:8983/solr/search_cn";
        SolrClient client;
        try {
            // create client
            client = new HttpSolrClient(coreUrl);

            // create query,
            SolrQuery query = new SolrQuery();

            query.setQuery("java"); // q
            query.addFilterQuery("authors:布鲁斯"); // fq
            // query.setFields("id", "cn_name", "en_name", "authors"); // fl
            query.setStart(0); // start
            query.setRows(10); // rows
            query.set("defType", "edismax"); // defType
            query.set("qf", "cn_name"); // qf

            // get response
            QueryResponse response = client.query(query);

            // get documents
            SolrDocumentList docList = response.getResults();

            System.out.printf("doc count: %d\n", docList.size());
            client.close();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        queryEdismaxTest();
    }
}

SolrJ will wrap result into Java objects, if u want to get the raw output as json. Then I suggest u to use the tools like HttpClient to retrieve the result via url of solr web, then parse the result via Jackson into json tree JsonNode.