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

青春壹個敷衍的年華 提交于 2019-12-25 11:52:55

问题


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.

  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




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!