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

While using Solr field highlighted by Solr Servlet. Also added snippets to search results while using Solr.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/AbstractGS2FieldSearch.java

    r29731 r30049  
    2222import java.util.ArrayList;
    2323import java.util.HashMap;
     24import java.util.List;
     25import java.util.Map;
    2426
    2527import org.apache.log4j.Logger;
     
    2729import org.greenstone.gsdl3.util.GSXML;
    2830import org.greenstone.gsdl3.util.XMLConverter;
    29 
    3031import org.w3c.dom.Document;
    3132import org.w3c.dom.Element;
     
    9293    protected boolean advanced_form_search = false;
    9394    protected boolean raw_search = false;
     95    //Parameters to get full field with highlighted terms
     96    protected String hldocOID = null;
     97    //index Field for highlighting
     98    protected String indexField = null;
    9499
    95100    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2FieldSearch.class.getName());
     
    591596    protected Element processAnyQuery(Element request, int query_type)
    592597    {
     598               
    593599        String service_name = null;
    594600        String empty_query_test_param = null;
     
    633639
    634640        // Process the request parameters
     641       
    635642        HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
    636 
     643        //Save as variable to make it accessable from GS2SolrSearch
     644        hldocOID = (String) params.get("hldocOID");
     645        //System.out.println("@@@@@Extracted hldocOID is " + hldocOID);
    637646        // Make sure a query has been specified
    638647        String query = (String) params.get(empty_query_test_param);
     
    644653        // If a field hasn't been specified, use the default - for textQuery
    645654        String field = (String) params.get(INDEX_PARAM);
     655        //Save as variable to make it accessable from GS2SolrSearch
     656       
    646657        if (field == null)
    647658        {
    648659            field = default_index;
    649660        }
    650 
     661        indexField = field;
    651662        // set up the appropriate query system
    652663        if (!setUpQueryer(params))
     
    668679            break;
    669680        }
    670 
     681       
    671682        // run the query
    672683        Object query_result = runQuery(query);
    673 
     684       
     685        // We want highlighted text to be returned right now!
     686        if (hldocOID != null && does_full_field_highlighting)
     687        {
     688            Element nodeContentElement = result_doc.createElement(GSXML.NODE_CONTENT_ELEM);
     689            nodeContentElement.setTextContent((String) query_result);
     690            result.appendChild(nodeContentElement);
     691            return result;
     692        }
     693       
     694           
    674695        // build up the response
    675696
     
    704725        }
    705726       
    706 
     727        Map<String, Map<String, List<String>>> hlResults = null;
     728        if (does_highlight_snippets)
     729        {
     730            hlResults = getHighlightSnippets(query_result);
     731        }
     732       
    707733        // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
    708734        GSXML.addMetadata(metadata_list, "query", query);
     
    711737            Element document_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
    712738            result.appendChild(document_list);
     739            Element snippet_list = result_doc.createElement(GSXML.HL_SNIPPET_ELEM + GSXML.LIST_MODIFIER);
     740            result.appendChild(snippet_list);
    713741            for (int d = 0; d < docs.length; d++)
    714742            {
    715743                String doc_id = internalNum2OID(docs[d]);
    716744                Element doc_node = createDocNode(result_doc, doc_id, doc_ranks[d]);
     745                if (hlResults != null && hlResults.get(docs[d]) != null && hlResults.get(docs[d]).get(indexField) != null) {
     746                    for (String snippet : hlResults.get(docs[d]).get(indexField)){
     747                        //remove html tags
     748                        snippet = snippet.replaceAll("\\<.*?>", "");
     749                        //remove truncated tags
     750                        snippet = snippet.replaceAll(".*>", "");
     751                        snippet = snippet.replaceAll("\\<.*", "");
     752                        //remove unwanted symbols at start of line
     753                        snippet = snippet.replaceAll("^[ .,»):;-–]+", "");
     754                        //highlighting tags transformation
     755                        snippet = snippet.replaceAll("&lt;", "<");
     756                        snippet = snippet.replaceAll("&gt;", ">");
     757                        Element snippet_node = result_doc.createElement(GSXML.HL_SNIPPET_ELEM);
     758                        snippet_node.setAttribute(GSXML.NODE_ID_ATT, doc_id);
     759                        snippet_node.setTextContent(snippet);
     760                        snippet_list.appendChild(snippet_node);
     761                    }
     762                }
    717763                document_list.appendChild(doc_node);
    718764            }
     
    756802        }
    757803
     804       
    758805        return result;
    759806
     
    778825    /** get the list of facets */
    779826    abstract protected ArrayList<FacetWrapper> getFacets(Object query_result);
     827   
     828    /** get the map of highlighting snippets */
     829    abstract protected Map<String, Map<String, List<String>>> getHighlightSnippets(Object query_result);
    780830
    781831    /** add in term info if available */
Note: See TracChangeset for help on using the changeset viewer.