Changeset 30050 for gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/util/SolrQueryWrapper.java
- Timestamp:
- 2015-07-21T05:35:34+12:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.