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