Sumit Arora
Sumit Arora

Reputation: 5241

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 ?

Upvotes: 0

Views: 1447

Answers (2)

freedev
freedev

Reputation: 30067

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

Upvotes: 2

Sumit Arora
Sumit Arora

Reputation: 5241

I have used following approach to execute the query and it worked:

    SolrQuery query = new SolrQuery();
    query.setQuery(queryString);
    query.setFacet(true);
    query.set("wt", "json");
    query.set("indent",true);
    query.setRows(0);
    query.addFacetField("outcome_type");
    QueryResponse resp = server.query(query);

enter image description here

Upvotes: 0

Related Questions