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

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  
    6565                paramDefaults.put(SORT_ORDER_PARAM, SORT_ORDER_DESCENDING);
    6666        does_faceting = true;
     67        does_highlight_snippets = true;
     68        does_full_field_highlighting = true;
    6769        // Used to store the solr cores that match the required 'level'
    6870        // of search (e.g. either document-level=>didx, or
     
    341343        try
    342344        {
     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            }
    343352            SharedSoleneQueryResult sqr = this.solr_src.runQuery(query);
    344353
     
    352361        return null;
    353362    }
    354 
     363   
     364   
    355365    /** get the total number of docs that match */
    356366    protected long numDocsMatched(Object query_result)
     
    445455
    446456        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();
    447469    }
    448470
  • gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/util/SolrQueryResult.java

    r29142 r30050  
    2828
    2929import java.util.List;
     30import java.util.Map;
    3031
    3132import org.apache.solr.client.solrj.response.FacetField;
     
    4142{
    4243    protected List<FacetField> _facetResults = null;
     44    protected Map<String,Map<String,List<String>>> _highlightResults = null;
    4345    SolrQueryResult()
    4446    {
    4547        super();
    4648    }
    47    
    4849    public void setFacetResults(List<FacetField> facetResults)
    4950    {
     
    5556        return _facetResults;
    5657    }
     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   
    5767}
  • 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("&lt;span class=\"snippetText\"&gt;");
     370       
     371        //Set text which appears after highlighted term
     372        solrQuery.setHighlightSimplePost("&lt;/span&gt;");
     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.