Ignore:
Timestamp:
2015-07-21T05:35:34+12:00 (9 years ago)
Author:
Georgiy Litvinov
Message:

Solr repo modifications for Solr side highlighing and snippets

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/util/SolrQueryWrapper.java

    r29987 r30050  
    3333import java.util.Iterator;
    3434import java.util.List;
     35import java.util.Map;
    3536import java.util.Set;
    3637import java.util.HashSet;
    37 
    3838import java.util.regex.Pattern;
    3939import java.util.regex.Matcher;
     
    4646import org.apache.solr.client.solrj.response.QueryResponse;
    4747import org.apache.solr.client.solrj.response.TermsResponse;
    48 
    4948import org.apache.solr.core.CoreContainer;
    5049import org.apache.solr.core.SolrCore;
    51 
    5250import org.apache.solr.common.SolrDocument;
    5351import org.apache.solr.common.SolrDocumentList;
     
    5553import org.greenstone.LuceneWrapper4.SharedSoleneQuery;
    5654import org.greenstone.LuceneWrapper4.SharedSoleneQueryResult;
    57 
    5855import org.apache.lucene.search.Query; // Query, TermQuery, BooleanQuery, BooleanClause and more
    5956import org.apache.lucene.index.IndexReader;
     
    8178    SolrServer solr_core = null;
    8279
     80    protected String highlight_field = null;
     81   
    8382    String collection_core_name_prefix = null;
    8483
     
    109108    }
    110109  }
    111 
     110  public void setHighlightField(String hl_field)
     111  {
     112    this.highlight_field = hl_field;
     113  }
    112114  public void setSortOrder(String order)
    113115  {
     
    360362        solrQuery.setFields("docOID", "score"); //solrParams.set("fl", "docOID score totaltermfreq(field,'queryterm')");
    361363       
     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       
    362374        //solrQuery.setTerms(true); // turn on the termsComponent       
    363375        //solrQuery.set("terms.fl", "ZZ"); // which field to get the terms from. ModifiableSolrParams method
     
    392404            QueryResponse solrResponse = solr_core.query(solrQuery); //solr_core.query(solrParams);
    393405            SolrDocumentList hits = solrResponse.getResults();
     406            Map<String, Map<String, List<String>>> hlResponse = solrResponse.getHighlighting();
     407            solr_query_result.setHighlightResults(hlResponse);
    394408            //TermsResponse termResponse = solrResponse.getTermsResponse(); // null unless termvectors=true in schema.xml
    395409
     
    410424                solr_query_result.setStartResults(start_results);
    411425                solr_query_result.setEndResults(start_results + hits.size());
    412 
    413                
     426                   
    414427                // get the first field we're searching in, this will be the fallback field
    415428                int sepIndex = query_string.indexOf(":");
     
    505518        return solr_query_result;
    506519    }
     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    }
    507573
    508574    //Greenstone universe operates with a base of 1 for "start_results"
Note: See TracChangeset for help on using the changeset viewer.