Changeset 25866
- Timestamp:
- 2012-06-28T13:17:17+12:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/util/SolrQueryWrapper.java
r25865 r25866 25 25 *********************************************************************/ 26 26 package org.greenstone.gsdl3.util; 27 28 import java.lang.reflect.Type; 29 import java.net.URLDecoder; 30 import java.util.ArrayList; 31 import java.util.HashMap; 32 import java.util.List; 27 33 28 34 import org.apache.log4j.Logger; … … 36 42 import org.greenstone.LuceneWrapper3.SharedSoleneQueryResult; 37 43 44 import com.google.gson.Gson; 45 import com.google.gson.reflect.TypeToken; 46 38 47 public class SolrQueryWrapper extends SharedSoleneQuery 39 48 { 40 41 49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.SolrQueryWrapper.class.getName()); 42 43 50 protected int max_docs = 100; 44 51 protected ArrayList<String> _facets = new ArrayList<String>(); 52 protected ArrayList<String> _facetQueries = new ArrayList<String>(); 45 53 SolrServer solr_core = null; 46 54 … … 48 56 { 49 57 super(); 58 start_results = 0; 50 59 } 51 60 … … 60 69 } 61 70 71 public void addFacet(String facet) 72 { 73 if (!_facets.contains(facet)) 74 { 75 _facets.add(facet); 76 } 77 } 78 79 public void clearFacets() 80 { 81 _facets.clear(); 82 } 83 84 public void addFacetQuery(String facetQuery) 85 { 86 if (!_facetQueries.contains(facetQuery)) 87 { 88 _facetQueries.add(facetQuery); 89 } 90 } 91 92 public void clearFacetQueries() 93 { 94 _facetQueries.clear(); 95 } 96 62 97 public boolean initialise() 63 98 { 64 65 99 if (solr_core == null) 66 100 { … … 70 104 } 71 105 return true; 72 73 106 } 74 107 75 108 public SharedSoleneQueryResult runQuery(String query_string) 76 109 { 77 78 110 if (query_string == null || query_string.equals("")) 79 111 { … … 85 117 SolrQueryResult solr_query_result = new SolrQueryResult(); 86 118 solr_query_result.clear(); 119 120 if (_facetQueries.size() > 0) 121 { 122 HashMap<String, ArrayList<String>> grouping = new HashMap<String, ArrayList<String>>(); 123 for (String currentQuery : _facetQueries) 124 { 125 //Facet queries are stored in JSON, so we have to decode it 126 Gson gson = new Gson(); 127 Type type = new TypeToken<List<String>>() 128 { 129 }.getType(); 130 List<String> queryElems = gson.fromJson(currentQuery, type); 131 132 //Group each query segment by the index it uses 133 for (String currentQueryElement : queryElems) 134 { 135 String decodedQueryElement = null; 136 try 137 { 138 decodedQueryElement = URLDecoder.decode(currentQueryElement, "UTF-8"); 139 } 140 catch (Exception ex) 141 { 142 continue; 143 } 144 145 int colonIndex = currentQueryElement.indexOf(":"); 146 String indexShortName = currentQueryElement.substring(0, colonIndex); 147 148 if(grouping.get(indexShortName) == null) 149 { 150 grouping.put(indexShortName, new ArrayList<String>()); 151 } 152 grouping.get(indexShortName).add(decodedQueryElement); 153 } 154 } 155 156 //Construct the facet query string to add to the regular query string 157 StringBuilder facetQueryString = new StringBuilder(); 158 int keysetCounter = 0; 159 for (String key : grouping.keySet()) 160 { 161 StringBuilder currentFacetString = new StringBuilder("("); 162 int groupCounter = 0; 163 for(String queryElem : grouping.get(key)) 164 { 165 currentFacetString.append(queryElem); 166 167 groupCounter++; 168 if(groupCounter < grouping.get(key).size()) 169 { 170 currentFacetString.append(" OR "); 171 } 172 } 173 currentFacetString.append(")"); 174 175 facetQueryString.append(currentFacetString); 176 177 keysetCounter++; 178 if(keysetCounter < grouping.keySet().size()) 179 { 180 facetQueryString.append(" AND "); 181 } 182 } 183 184 if(facetQueryString.length() > 0) 185 { 186 query_string += " AND " + facetQueryString; 187 } 188 } 87 189 88 190 ModifiableSolrParams solrParams = new ModifiableSolrParams(); … … 91 193 solrParams.set("rows", (end_results - start_results) + 1); 92 194 solrParams.set("fl", "docOID score"); 195 196 if (_facets.size() > 0) 197 { 198 solrParams.set("facet", "true"); 199 for (int i = 0; i < _facets.size(); i++) 200 { 201 solrParams.add("facet.field", _facets.get(i)); 202 } 203 } 93 204 94 205 try 95 206 { 96 207 QueryResponse solrResponse = solr_core.query(solrParams); 97 98 208 SolrDocumentList hits = solrResponse.getResults(); 99 209 100 210 if (hits != null) 101 211 { 102 103 212 logger.info("*** hits size = " + hits.size()); 104 213 logger.info("*** num docs found = " + hits.getNumFound()); … … 108 217 logger.info("*** max docs = " + max_docs); 109 218 110 // numDocsFound is the total number of ma ctching docs in the collection219 // numDocsFound is the total number of matching docs in the collection 111 220 // as opposed to the number of documents returned in the hits list 112 221 … … 139 248 solr_query_result.setEndResults(0); 140 249 } 250 251 solr_query_result.setFacetResults(solrResponse.getFacetFields()); 141 252 } 142 253 catch (SolrServerException server_exception) 143 254 { 255 server_exception.printStackTrace(); 144 256 solr_query_result.setError(SolrQueryResult.SERVER_ERROR); 145 257 } … … 148 260 } 149 261 262 //Greenstone universe operates with a base of 1 for "start_results" 263 //But Solr operates from 0 264 public void setStartResults(int start_results) 265 { 266 if (start_results < 0) 267 { 268 start_results = 0; 269 } 270 this.start_results = start_results - 1; 271 } 272 150 273 public void cleanUp() 151 274 {
Note:
See TracChangeset
for help on using the changeset viewer.