Shubhra Sinha Shubhra Sinha - 26 days ago 14
Java Question

How to write solr query in java for multiple sub-queries?

Could someone help in converting the following solr query into java?

http://<server>/solr/<core>/select?debugQuery=on&indent=on&q=(_query_:"{!edismax mm='100%25' qf='manu^2.0' v=$q1}")
AND (_query_:"{!edismax mm='100%25' qf='category^2.0' v=$q1}")&q1=galaxy&wt=csv


I need to know how I can set the multiple sub-queries into query.

Edit 2: In order to get accurate response, I queried 3 times until response is received.

query.setQuery("manu:"+prefix+" AND category:"+prefix);
QueryResponse response = server.query(query);
if (response.getResults().getNumFound() == 0) {
query.setQuery("manu:"+prefix+" OR category:"+prefix);
response = server.query(query);
if (response.getResults().getNumFound() == 0) {
query.setQuery("text:"+prefix);
response = server.query(query);
}
}


But it is causing too much delay? Is there any better approach?

Answer

I just made little modifications in your query to make it more simpler

q=(category:$q1^2.0 AND manu:$q1^2.0)&defType=edismax&q1=galaxy&wt=csv&fl=manu,category

Create Solrclient object

String urlString = "http://server:port/solr/core";

SolrClient solrClient = new HttpSolrClient.Builder(urlString).build();

Create SolrQuery object and on that object execute the query method like this and also set various parameters

query.set("q", "category:galaxy AND manu:galaxy");

query.set("qt", "/search");

query.set("mm", "100%");

query.setQueryType("edismax");

query.set("qf", "category^2.0 manu^2.0");

QueryResponse response = solrClient.query(query);

you will get the list of SolrDocumentList, on which you could iterate and got the results back.

SolrDocumentList list = response.getResults();

Please refer to SolrJ Documentation as per the solr version you are using.

Comments