Skip to content

Instantly share code, notes, and snippets.

@johnmiedema
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmiedema/104b606b0b184cbcb702 to your computer and use it in GitHub Desktop.
Save johnmiedema/104b606b0b184cbcb702 to your computer and use it in GitHub Desktop.
Extract SolrQuery Response Data
//Extract SolrQuery response data
//johnmiedema.com
package demoCrawlIndexQuery;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
public class Query {
public static void main(String[] args) {
//assumption: documents have already been indexed
//see: https://gist.github.com/johnmiedema/11224886
SolrServer solr = new HttpSolrServer("http://localhost:8983/solr/"); //create solr connection
//----------------------------------------------------------
//create query
SolrQuery query = new SolrQuery();
query.setQuery("\"Call me Ishmael\""); //use quotes for exact match on phrase
//turn on snippet highlighting
query.setHighlight(true);
query.setHighlight(true).setHighlightSnippets(1);
query.setParam("hl.fl", "content");
//include relevance score
query.addField("*");
query.addField("score");
System.out.println("Query: " + query);
//----------------------------------------------------------
//submit query
QueryResponse response = null;
try {
response = solr.query(query);
} catch (SolrServerException e) {
e.printStackTrace();
}
//----------------------------------------------------------
//extract response data
SolrDocumentList docList = (SolrDocumentList) response.getResults();
//document count
int docCount = docList.size();
System.out.println("Doc Count: " + docCount);
//document fields
SolrDocument doc = null;
for (int i = 0; i < docCount; i++) {
doc = docList.get(i);
Object id = doc.getFieldValue("id");
System.out.println("Doc Id: " + id);
Object title = doc.getFieldValue("title");
System.out.println("Title: " + title);
Object author = doc.getFieldValue("author");
System.out.println("Author: " + author);
Object score = doc.getFieldValue("score");
System.out.println("Score: " + score); //https://wiki.apache.org/solr/SolrRelevancyFAQ#How_are_documents_scored
//this is big, so left out
//Object content = doc.getFieldValue("content");
//System.out.println(content);
//first highlight
String firstHighlight = response.getHighlighting().get(id).get("content").get(0);
firstHighlight = firstHighlight.replaceAll("[\n\r]", ""); //remove line feeds
System.out.println("Highlight: " + firstHighlight);
}
//----------------------------------------------------------
//results
/*
Query: q=%22Call+me+Ishmael%22&hl=true&hl.snippets=1&hl.fl=content&fl=*%2Cscore
Doc Count: 1
Doc Id: 9664818f-3b88-4af1-91b3-6ee83abce813
Title: [Moby Dick; Or the Whale, by Herman Melville]
Author: null
Score: 0.006909426
Highlight: CHAPTER 1. Loomings. <em>Call</em> <em>me</em> <em>Ishmael</em>. Some years ago—never mind how long precisely—having
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment