问题
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 ?
回答1:
I see few problems in your tentative.
You should not pass entire query string with the
setQuery
method. For almost each parameter available in query string there is a corresponding method inSolrQuery
class.SolrQuery
does not supportjson
format, SolrJ only supports thejavabin
andxml
formats, I suggest to not specify anywt
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
回答2:
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);
来源:https://stackoverflow.com/questions/34972147/correct-use-case-of-string-parameter-in-setquery-function-of-solrquery