1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.List;
import org.apache.lucene.index.CorruptIndexException;
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.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
public class Test {
private static final String ServeurConstel = "http://127.0.0.1:8083/constellio/app";
private String Collection = "Test";
private static final String facet = "on";
private String recherche = "test";
private int debut = 0;
private int nbDocuments = 20;
public Test(PrintWriter x) throws MalformedURLException,SolrServerException, CorruptIndexException, IOException {
CommonsHttpSolrServer server = new CommonsHttpSolrServer(ServeurConstel);
server.setParser(new XMLResponseParser());
x.print(" = = = = = = Exécution de la requête = = = = = = <br>");
print(doQuery(server),x);
}
private QueryResponse doQuery(SolrServer server) throws SolrServerException {
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery(recherche);
solrQuery.set("collectionName", Collection);
solrQuery.set("facet", facet);
solrQuery.setStart(debut);
solrQuery.setRows(nbDocuments);
solrQuery.setHighlightFragsize(0);
solrQuery.setHighlightSnippets(1);
solrQuery.setHighlightSimplePre("<b>");
solrQuery.setHighlightSimplePost("</b>");
return server.query(solrQuery);
}
@SuppressWarnings("unchecked")
private void print(QueryResponse response, PrintWriter x) throws CorruptIndexException, IOException {
SolrDocumentList docs = response.getResults();
if (docs != null) {
x.print(docs.getNumFound() + " documents trouvés, " + docs.size() + " affichés : <br>");
for (int i = 0; i < docs.size(); i++) {
SolrDocument doc = docs.get(i);
String content = (String) doc.get("doc_parsedContent");
String id = (String) doc.get("doc_uniqueKey");
if (response.getHighlighting().get(id) != null) {
List<String> highlightSnippets = response.getHighlighting().get(id).get(content);
for( int j = 0; j< highlightSnippets.size();j++ ){
x.print(highlightSnippets.get(j));
}
}
else{
x.print("<br><br> highlightSnippets est égal à NULL");
}
}
}
}
} |