Skip to content

Instantly share code, notes, and snippets.

@joel-bernstein
Last active June 9, 2020 10:54
Show Gist options
  • Save joel-bernstein/18e1e4173fb68dcc58ed0f259ed41bf5 to your computer and use it in GitHub Desktop.
Save joel-bernstein/18e1e4173fb68dcc58ed0f259ed41bf5 to your computer and use it in GitHub Desktop.
Harness to run collapse query
import java.util.*;
import java.io.*;
import org.apache.solr.client.solrj.impl.*;
import org.apache.solr.client.solrj.*;
import org.apache.solr.common.util.*;
import org.apache.solr.client.solrj.response.*;
import org.apache.solr.common.params.*;
public class CollapseQuery {
public static void main(String args[]) throws Exception {
String query = "*:*";
//String host = args[1];
int num = Integer.parseInt(args[0]);
String collection = args[1];
long max = Long.MIN_VALUE;
long min = Long.MAX_VALUE;
PrintWriter out = null;
HttpSolrClient client = null;
try {
long timestamp = System.currentTimeMillis();
out = new PrintWriter(new FileWriter("results-"+timestamp+".txt"));
out.println("query:"+query);
//out.println("host:"+host);
out.println("num:"+num);
String solrUrl = "http://localhost:8983/solr";
client = new HttpSolrClient.Builder(solrUrl)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
long begin = System.currentTimeMillis();
Random rand = new Random();
int numq = 0;
for(int i=0; i<num; i++) {
numq++;
//int t = rand.nextInt(3000);
//String query1 = query.replace("{term}", Integer.toString(t));
//System.out.println(query1);
Map<String, String> queryParamMap = new HashMap<String, String>();
queryParamMap.put("q", query);
queryParamMap.put("fq", "{!collapse tag=gc field=group_s}");
queryParamMap.put("fl", "id");
//queryParamMap.put("json.facet", "{ \"prod\": { \"type\": \"terms\", \"field\": \"prod_s\", \"domain\": {\"excludeTags\":\"gc\"}, \"facet\": { \"parent_count\": \"unique(group_s)\" } }, \"service\": { \"type\": \"terms\", \"field\": \"service_s\", \"domain\": { \"excludeTags\":\"gc\" }, \"facet\": { \"parent_count\": \"unique(group_s)\" } } }");
MapSolrParams queryParams = new MapSolrParams(queryParamMap);
long qbegin = System.currentTimeMillis();
QueryResponse response = client.query(collection, queryParams);
//System.out.println("NumFound:"+response.getResults().getNumFound());
//System.out.println("Response:"+response);
long qend = System.currentTimeMillis();
long qtime = qend-qbegin;
if(qtime < min) {
min = qtime;
}
if(qtime > max) {
max = qtime;
}
}
long end = System.currentTimeMillis();
long totalTime = end-begin;
out.println("Queries Run:"+numq);
out.println("Total time:"+ totalTime);
out.println("Min Time:"+min);
out.println("Max Time:"+max);
out.println("Avg time:"+totalTime/num);
out.println("QPS:"+((double)num)/((double)totalTime/(double)1000));
} catch(Exception e) {
e.printStackTrace();
} finally {
client.close();
out.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment