Search code examples
javasolrsolrj

Correct use case of String parameter in SetQuery function of SolrQuery?


I have q

queryString = "select?wt=json&rows=0&indent=true&facet=true&q=*:*&facet=true&facet.field=outcome_type"

If queried like :

http://x.x.x.x:8983/solr/abc/queryString 

it works. here abc is a core.

Now I would like to execute it programmatically, and using the following approach :

    SolrQuery query = new SolrQuery();
    query.setQuery(queryString);
    QueryResponse resp = server.query(query);

here queryString as defined above, but it return the following error :

Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: undefined field text

What I am missing here ? Or I need to build the query by set functions ?


Solution

  • I see few problems in your tentative.

    1. You should not pass entire query string with the setQuery method. For almost each parameter available in query string there is a corresponding method in SolrQuery class.

    2. SolrQuery does not support json format, SolrJ only supports the javabin and xml formats, I suggest to not specify any wt parameter.

    So, you should use setQuery method only for q parameter:

    query.setQuery("*:*");
    

    For remaining parameters, the easiest way is use add method:

    query.add("rows", "0");  // instead of setRows(0)
    query.add("indent", "true"); 
    query.add("facet", "true"); // ... setFacet(true)
    query.add("facet.field", "outcome_type"); // ... addFacetField("outcome_type")
    

    Hope this helps