Changeset 30050 for gs3-extensions/solr/trunk/src/src/java/org/greenstone
- Timestamp:
- 2015-07-21T05:35:34+12:00 (9 years ago)
- Location:
- gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/service/GS2SolrSearch.java
r29711 r30050 65 65 paramDefaults.put(SORT_ORDER_PARAM, SORT_ORDER_DESCENDING); 66 66 does_faceting = true; 67 does_highlight_snippets = true; 68 does_full_field_highlighting = true; 67 69 // Used to store the solr cores that match the required 'level' 68 70 // of search (e.g. either document-level=>didx, or … … 341 343 try 342 344 { 345 //if it is a Highlighting Query - execute it 346 this.solr_src.setHighlightField(indexField); 347 if(hldocOID != null) 348 { 349 String rslt = this.solr_src.runHighlightingQuery(query,hldocOID); 350 return rslt; 351 } 343 352 SharedSoleneQueryResult sqr = this.solr_src.runQuery(query); 344 353 … … 352 361 return null; 353 362 } 354 363 364 355 365 /** get the total number of docs that match */ 356 366 protected long numDocsMatched(Object query_result) … … 445 455 446 456 return newFacetList; 457 } 458 @Override 459 protected Map<String, Map<String, List<String>>> getHighlightSnippets(Object query_result) 460 { 461 if (!(query_result instanceof SolrQueryResult)) 462 { 463 return null; 464 } 465 466 SolrQueryResult result = (SolrQueryResult) query_result; 467 468 return result.getHighlightResults(); 447 469 } 448 470 -
gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/util/SolrQueryResult.java
r29142 r30050 28 28 29 29 import java.util.List; 30 import java.util.Map; 30 31 31 32 import org.apache.solr.client.solrj.response.FacetField; … … 41 42 { 42 43 protected List<FacetField> _facetResults = null; 44 protected Map<String,Map<String,List<String>>> _highlightResults = null; 43 45 SolrQueryResult() 44 46 { 45 47 super(); 46 48 } 47 48 49 public void setFacetResults(List<FacetField> facetResults) 49 50 { … … 55 56 return _facetResults; 56 57 } 58 //Save highlighting snippets 59 public void setHighlightResults(Map<String,Map<String,List<String>>> highlightResults){ 60 _highlightResults = highlightResults; 61 } 62 //Extract highlighting snippets 63 public Map<String,Map<String,List<String>>> getHighlightResults(){ 64 return _highlightResults; 65 } 66 57 67 } -
gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/util/SolrQueryWrapper.java
r29987 r30050 33 33 import java.util.Iterator; 34 34 import java.util.List; 35 import java.util.Map; 35 36 import java.util.Set; 36 37 import java.util.HashSet; 37 38 38 import java.util.regex.Pattern; 39 39 import java.util.regex.Matcher; … … 46 46 import org.apache.solr.client.solrj.response.QueryResponse; 47 47 import org.apache.solr.client.solrj.response.TermsResponse; 48 49 48 import org.apache.solr.core.CoreContainer; 50 49 import org.apache.solr.core.SolrCore; 51 52 50 import org.apache.solr.common.SolrDocument; 53 51 import org.apache.solr.common.SolrDocumentList; … … 55 53 import org.greenstone.LuceneWrapper4.SharedSoleneQuery; 56 54 import org.greenstone.LuceneWrapper4.SharedSoleneQueryResult; 57 58 55 import org.apache.lucene.search.Query; // Query, TermQuery, BooleanQuery, BooleanClause and more 59 56 import org.apache.lucene.index.IndexReader; … … 81 78 SolrServer solr_core = null; 82 79 80 protected String highlight_field = null; 81 83 82 String collection_core_name_prefix = null; 84 83 … … 109 108 } 110 109 } 111 110 public void setHighlightField(String hl_field) 111 { 112 this.highlight_field = hl_field; 113 } 112 114 public void setSortOrder(String order) 113 115 { … … 360 362 solrQuery.setFields("docOID", "score"); //solrParams.set("fl", "docOID score totaltermfreq(field,'queryterm')"); 361 363 364 //Turn on highlighting 365 solrQuery.setHighlight(true); 366 //Return 3 snippets for each document 367 solrQuery.setParam("hl.snippets", "3"); 368 solrQuery.setParam("hl.fl", highlight_field); 369 solrQuery.setHighlightSimplePre("<span class=\"snippetText\">"); 370 371 //Set text which appears after highlighted term 372 solrQuery.setHighlightSimplePost("</span>"); 373 362 374 //solrQuery.setTerms(true); // turn on the termsComponent 363 375 //solrQuery.set("terms.fl", "ZZ"); // which field to get the terms from. ModifiableSolrParams method … … 392 404 QueryResponse solrResponse = solr_core.query(solrQuery); //solr_core.query(solrParams); 393 405 SolrDocumentList hits = solrResponse.getResults(); 406 Map<String, Map<String, List<String>>> hlResponse = solrResponse.getHighlighting(); 407 solr_query_result.setHighlightResults(hlResponse); 394 408 //TermsResponse termResponse = solrResponse.getTermsResponse(); // null unless termvectors=true in schema.xml 395 409 … … 410 424 solr_query_result.setStartResults(start_results); 411 425 solr_query_result.setEndResults(start_results + hits.size()); 412 413 426 414 427 // get the first field we're searching in, this will be the fallback field 415 428 int sepIndex = query_string.indexOf(":"); … … 505 518 return solr_query_result; 506 519 } 520 // Highlighting query. Returns full highlighted text for document 521 public String runHighlightingQuery(String query,String hldocOID) 522 { 523 524 SolrQueryResult solr_query_result = new SolrQueryResult(); 525 solr_query_result.clear(); 526 527 528 /* Create Query*/ 529 530 SolrQuery solrQuery = new SolrQuery(query); 531 532 /* Set Query Parameters*/ 533 534 //Turn on highlighting 535 solrQuery.setHighlight(true); 536 //Extract default field from query 537 538 //Set field for highlighting 539 solrQuery.setParam("hl.fl", highlight_field); 540 541 //Get whole highlighted field 542 solrQuery.setHighlightFragsize(0); 543 544 //Return only required document by docOID 545 solrQuery.setFilterQueries("docOID:"+ hldocOID); 546 547 //Set text which appears before highlighted term 548 //solrQuery.setHighlightSimplePre("<annotation type=\"query_term\">"); 549 solrQuery.setHighlightSimplePre("<span class=\"termHighlight\">"); 550 //Set text which appears after highlighted term 551 //solrQuery.setHighlightSimplePost("</annotation>"); 552 solrQuery.setHighlightSimplePost("</span>"); 553 //Prepare results 554 String text = null; 555 // do the query 556 try 557 { 558 QueryResponse solrResponse = solr_core.query(solrQuery); //solr_core.query(solrParams); 559 //Get highliting results 560 Map<String,Map<String,List<String>>> highlightingResults = solrResponse.getHighlighting(); 561 //Get highlited document text 562 text = highlightingResults.get(hldocOID).get(highlight_field).get(0); 563 564 565 } 566 catch (SolrServerException server_exception) 567 { 568 server_exception.printStackTrace(); 569 570 } 571 return text; 572 } 507 573 508 574 //Greenstone universe operates with a base of 1 for "start_results"
Note:
See TracChangeset
for help on using the changeset viewer.