[24644] | 1 |
|
---|
| 2 | // Adapted from
|
---|
| 3 | // http://wiki.constellio.com/index.php/Solrj_example
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | import java.net.MalformedURLException;
|
---|
| 7 | import java.util.Iterator;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Map;
|
---|
| 10 | import java.util.Map.Entry;
|
---|
| 11 |
|
---|
| 12 | import org.apache.solr.client.solrj.SolrQuery;
|
---|
| 13 | import org.apache.solr.client.solrj.SolrServer;
|
---|
| 14 | import org.apache.solr.client.solrj.SolrServerException;
|
---|
| 15 | import org.apache.solr.client.solrj.response.FacetField;
|
---|
| 16 | import org.apache.solr.client.solrj.response.FacetField.Count;
|
---|
| 17 | import org.apache.solr.client.solrj.response.QueryResponse;
|
---|
| 18 | import org.apache.solr.common.SolrDocument;
|
---|
| 19 | import org.apache.solr.common.SolrDocumentList;
|
---|
| 20 | import org.apache.solr.common.params.ModifiableSolrParams;
|
---|
| 21 | import org.apache.solr.common.params.SolrParams;
|
---|
| 22 | import org.apache.solr.common.util.NamedList;
|
---|
| 23 | import org.apache.solr.servlet.SolrRequestParsers;
|
---|
| 24 |
|
---|
| 25 | import java.io.File;
|
---|
| 26 | import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
---|
| 27 | import org.apache.solr.core.CoreContainer;
|
---|
| 28 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 29 | import java.io.IOException;
|
---|
| 30 | import org.xml.sax.SAXException;
|
---|
| 31 |
|
---|
| 32 | import java.util.Collection;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * A simple main to illustrate how to execute a request using SolrJ
|
---|
| 36 | *
|
---|
| 37 | */
|
---|
| 38 | public class QueryTest
|
---|
| 39 | {
|
---|
| 40 | protected static String solr_home_str = "C:\\cygwin\\home\\davidb\\research\\code\\greenstone3-svn-full\\web\\ext\\solr";
|
---|
| 41 |
|
---|
| 42 | private static final String myCore = "localsite-solr-jdbm-demo-didx";
|
---|
| 43 |
|
---|
| 44 | private static final String facet_field = "DS";
|
---|
| 45 |
|
---|
| 46 | // q=...
|
---|
| 47 | //private static final String query = "TX:snails";
|
---|
| 48 | private static final String query = "TX:farming";
|
---|
| 49 |
|
---|
| 50 | private static final int start = 0;
|
---|
| 51 | private static final int nbDocuments = 11;
|
---|
| 52 |
|
---|
| 53 | public static void main(String[] args) throws IOException,
|
---|
| 54 | ParserConfigurationException,
|
---|
| 55 | SAXException,
|
---|
| 56 | SolrServerException {
|
---|
| 57 |
|
---|
| 58 | File solr_home = new File(solr_home_str);
|
---|
| 59 | File solr_xml = new File( solr_home,"solr.xml" );
|
---|
| 60 |
|
---|
| 61 | try {
|
---|
| 62 | CoreContainer cores = new CoreContainer(solr_home_str,solr_xml);
|
---|
| 63 |
|
---|
| 64 | Collection<String> core_names = cores.getCoreNames();
|
---|
| 65 |
|
---|
| 66 | for (String name : core_names) {
|
---|
| 67 | System.err.println("(**** core name: " + name);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | EmbeddedSolrServer server = new EmbeddedSolrServer(cores,myCore);
|
---|
| 71 |
|
---|
| 72 | // Do the same query three times using three different method
|
---|
| 73 | System.out.println("====== 1st way to execute a query ======");
|
---|
| 74 | print(doFirstQuery(server));
|
---|
| 75 | System.out.println("====== 2nd way to execute a query ======");
|
---|
| 76 | print(doSecondQuery(server));
|
---|
| 77 | System.out.println("====== 3rd way to execute query ======");
|
---|
| 78 | print(doThirdQuery(server));
|
---|
| 79 |
|
---|
| 80 | /*
|
---|
| 81 | System.out.println("======= Using SpellChecker ======");
|
---|
| 82 | print(spellCheck(server, "farmng snals"));
|
---|
| 83 |
|
---|
| 84 | */
|
---|
| 85 |
|
---|
| 86 | cores.shutdown();
|
---|
| 87 | }
|
---|
| 88 | catch (Exception e) {
|
---|
| 89 | e.printStackTrace();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Do the query using a StringBuffer
|
---|
| 96 | */
|
---|
| 97 | public static QueryResponse doFirstQuery(SolrServer server)
|
---|
| 98 | throws SolrServerException {
|
---|
| 99 | StringBuffer request = new StringBuffer();
|
---|
| 100 | //request.append("collectionName=" + myCore);
|
---|
| 101 | request.append("facet=" + true);
|
---|
| 102 | request.append("&facet.field=" + facet_field);
|
---|
| 103 | request.append("&q=" + query);
|
---|
| 104 | request.append("&start=" + start);
|
---|
| 105 | request.append("&rows=" + nbDocuments);
|
---|
| 106 | SolrParams solrParams = SolrRequestParsers.parseQueryString(request
|
---|
| 107 | .toString());
|
---|
| 108 |
|
---|
| 109 | System.err.println("*** URL request: " + solrParams);
|
---|
| 110 |
|
---|
| 111 | return server.query(solrParams);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Do the query using a ModifiableSolrParams
|
---|
| 116 | */
|
---|
| 117 | public static QueryResponse doSecondQuery(SolrServer server)
|
---|
| 118 | throws SolrServerException {
|
---|
| 119 | ModifiableSolrParams solrParams = new ModifiableSolrParams();
|
---|
| 120 | //solrParams.set("collectionName", myCore);
|
---|
| 121 | solrParams.set("facet", "true");
|
---|
| 122 | solrParams.set("facet.field", facet_field);
|
---|
| 123 | solrParams.set("q", query);
|
---|
| 124 | solrParams.set("start", start);
|
---|
| 125 | solrParams.set("rows", nbDocuments);
|
---|
| 126 | return server.query(solrParams);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
| 130 | * Do the query using a SolrQuery
|
---|
| 131 | */
|
---|
| 132 | public static QueryResponse doThirdQuery(SolrServer server)
|
---|
| 133 | throws SolrServerException {
|
---|
| 134 | SolrQuery solrQuery = new SolrQuery();
|
---|
| 135 | solrQuery.setQuery(query);
|
---|
| 136 | //solrQuery.set("collectionName", myCore);
|
---|
| 137 | solrQuery.set("facet", "true");
|
---|
| 138 | solrQuery.set("facet.field", facet_field);
|
---|
| 139 | solrQuery.setStart(start);
|
---|
| 140 | solrQuery.setRows(nbDocuments);
|
---|
| 141 | return server.query(solrQuery);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * Do the query using a SolrQuery
|
---|
| 146 | */
|
---|
| 147 | public static QueryResponse spellCheck(SolrServer server, String badQuery)
|
---|
| 148 | throws SolrServerException {
|
---|
| 149 | SolrQuery solrQuery = new SolrQuery();
|
---|
| 150 | solrQuery.setQuery(badQuery);
|
---|
| 151 | solrQuery.set("collectionName", myCore);
|
---|
| 152 |
|
---|
| 153 | // qt=spellcheck || qt=spellchecker
|
---|
| 154 | solrQuery.setQueryType("spellcheck");
|
---|
| 155 | return server.query(solrQuery);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * Print documents and facets
|
---|
| 160 | *
|
---|
| 161 | * @param response
|
---|
| 162 | */
|
---|
| 163 | @SuppressWarnings("unchecked")
|
---|
| 164 | public static void print(QueryResponse response) {
|
---|
| 165 | SolrDocumentList docs = response.getResults();
|
---|
| 166 | if (docs != null) {
|
---|
| 167 | System.out.println(docs.getNumFound() + " documents found, "
|
---|
| 168 | + docs.size() + " returned : ");
|
---|
| 169 | for (int i = 0; i < docs.size(); i++) {
|
---|
| 170 | SolrDocument doc = docs.get(i);
|
---|
| 171 | System.out.println("\t" + doc.toString());
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | List<FacetField> fieldFacets = response.getFacetFields();
|
---|
| 176 | if (fieldFacets != null && !fieldFacets.isEmpty()) {
|
---|
| 177 | System.out.println("\nField Facets : ");
|
---|
| 178 | for (FacetField fieldFacet : fieldFacets) {
|
---|
| 179 | System.out.print("\t" + fieldFacet.getName() + " :\t");
|
---|
| 180 | if (fieldFacet.getValueCount() > 0) {
|
---|
| 181 | for (Count count : fieldFacet.getValues()) {
|
---|
| 182 | System.out.print(count.getName() + "["
|
---|
| 183 | + count.getCount() + "]\t");
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | System.out.println("");
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | Map<String, Integer> queryFacets = response.getFacetQuery();
|
---|
| 191 | if (queryFacets != null && !queryFacets.isEmpty()) {
|
---|
| 192 | System.out.println("\nQuery facets : ");
|
---|
| 193 | for (String queryFacet : queryFacets.keySet()) {
|
---|
| 194 | System.out.println("\t" + queryFacet + "\t["
|
---|
| 195 | + queryFacets.get(queryFacet) + "]");
|
---|
| 196 | }
|
---|
| 197 | System.out.println("");
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | NamedList<NamedList<Object>> spellCheckResponse = (NamedList<NamedList<Object>>) response
|
---|
| 201 | .getResponse().get("spellcheck");
|
---|
| 202 |
|
---|
| 203 | if (spellCheckResponse != null) {
|
---|
| 204 | Iterator<Entry<String, NamedList<Object>>> wordsIterator = spellCheckResponse
|
---|
| 205 | .iterator();
|
---|
| 206 |
|
---|
| 207 | while (wordsIterator.hasNext()) {
|
---|
| 208 | Entry<String, NamedList<Object>> entry = wordsIterator.next();
|
---|
| 209 | String word = entry.getKey();
|
---|
| 210 | NamedList<Object> spellCheckWordResponse = entry.getValue();
|
---|
| 211 | boolean correct = spellCheckWordResponse.get("frequency")
|
---|
| 212 | .equals(1);
|
---|
| 213 | System.out.println("Word: " + word + ",\tCorrect?: " + correct);
|
---|
| 214 | NamedList<Integer> suggestions = (NamedList<Integer>) spellCheckWordResponse
|
---|
| 215 | .get("suggestions");
|
---|
| 216 | if (suggestions != null && suggestions.size() > 0) {
|
---|
| 217 | System.out.println("Suggestions : ");
|
---|
| 218 | Iterator<Entry<String, Integer>> suggestionsIterator = suggestions
|
---|
| 219 | .iterator();
|
---|
| 220 | while (suggestionsIterator.hasNext()) {
|
---|
| 221 | System.out.println("\t"
|
---|
| 222 | + suggestionsIterator.next().getKey());
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | }
|
---|
| 226 | System.out.println("");
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | }
|
---|