Changeset 25866


Ignore:
Timestamp:
2012-06-28T13:17:17+12:00 (12 years ago)
Author:
sjm84
Message:

Some major upgrades to SolrQueryWrapper to enable faceting

File:
1 edited

Legend:

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

    r25865 r25866  
    2525 *********************************************************************/
    2626package org.greenstone.gsdl3.util;
     27
     28import java.lang.reflect.Type;
     29import java.net.URLDecoder;
     30import java.util.ArrayList;
     31import java.util.HashMap;
     32import java.util.List;
    2733
    2834import org.apache.log4j.Logger;
     
    3642import org.greenstone.LuceneWrapper3.SharedSoleneQueryResult;
    3743
     44import com.google.gson.Gson;
     45import com.google.gson.reflect.TypeToken;
     46
    3847public class SolrQueryWrapper extends SharedSoleneQuery
    3948{
    40 
    4149    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.SolrQueryWrapper.class.getName());
    42 
    4350    protected int max_docs = 100;
    44 
     51    protected ArrayList<String> _facets = new ArrayList<String>();
     52    protected ArrayList<String> _facetQueries = new ArrayList<String>();
    4553    SolrServer solr_core = null;
    4654
     
    4856    {
    4957        super();
     58        start_results = 0;
    5059    }
    5160
     
    6069    }
    6170
     71    public void addFacet(String facet)
     72    {
     73        if (!_facets.contains(facet))
     74        {
     75            _facets.add(facet);
     76        }
     77    }
     78
     79    public void clearFacets()
     80    {
     81        _facets.clear();
     82    }
     83
     84    public void addFacetQuery(String facetQuery)
     85    {
     86        if (!_facetQueries.contains(facetQuery))
     87        {
     88            _facetQueries.add(facetQuery);
     89        }
     90    }
     91
     92    public void clearFacetQueries()
     93    {
     94        _facetQueries.clear();
     95    }
     96
    6297    public boolean initialise()
    6398    {
    64 
    6599        if (solr_core == null)
    66100        {
     
    70104        }
    71105        return true;
    72 
    73106    }
    74107
    75108    public SharedSoleneQueryResult runQuery(String query_string)
    76109    {
    77 
    78110        if (query_string == null || query_string.equals(""))
    79111        {
     
    85117        SolrQueryResult solr_query_result = new SolrQueryResult();
    86118        solr_query_result.clear();
     119
     120        if (_facetQueries.size() > 0)
     121        {
     122            HashMap<String, ArrayList<String>> grouping = new HashMap<String, ArrayList<String>>();
     123            for (String currentQuery : _facetQueries)
     124            {
     125                //Facet queries are stored in JSON, so we have to decode it
     126                Gson gson = new Gson();
     127                Type type = new TypeToken<List<String>>()
     128                {
     129                }.getType();
     130                List<String> queryElems = gson.fromJson(currentQuery, type);
     131
     132                //Group each query segment by the index it uses
     133                for (String currentQueryElement : queryElems)
     134                {
     135                    String decodedQueryElement = null;
     136                    try
     137                    {
     138                        decodedQueryElement = URLDecoder.decode(currentQueryElement, "UTF-8");
     139                    }
     140                    catch (Exception ex)
     141                    {
     142                        continue;
     143                    }
     144
     145                    int colonIndex = currentQueryElement.indexOf(":");
     146                    String indexShortName = currentQueryElement.substring(0, colonIndex);
     147                   
     148                    if(grouping.get(indexShortName) == null)
     149                    {
     150                        grouping.put(indexShortName, new ArrayList<String>());
     151                    }
     152                    grouping.get(indexShortName).add(decodedQueryElement);
     153                }
     154            }
     155
     156            //Construct the facet query string to add to the regular query string
     157            StringBuilder facetQueryString = new StringBuilder();
     158            int keysetCounter = 0;
     159            for (String key : grouping.keySet())
     160            {
     161                StringBuilder currentFacetString = new StringBuilder("(");
     162                int groupCounter = 0;
     163                for(String queryElem : grouping.get(key))
     164                {
     165                    currentFacetString.append(queryElem);
     166                   
     167                    groupCounter++;
     168                    if(groupCounter < grouping.get(key).size())
     169                    {
     170                        currentFacetString.append(" OR ");
     171                    }
     172                }
     173                currentFacetString.append(")");
     174               
     175                facetQueryString.append(currentFacetString);
     176               
     177                keysetCounter++;
     178                if(keysetCounter < grouping.keySet().size())
     179                {
     180                    facetQueryString.append(" AND ");
     181                }
     182            }
     183           
     184            if(facetQueryString.length() > 0)
     185            {
     186                query_string += " AND " + facetQueryString;
     187            }
     188        }
    87189
    88190        ModifiableSolrParams solrParams = new ModifiableSolrParams();
     
    91193        solrParams.set("rows", (end_results - start_results) + 1);
    92194        solrParams.set("fl", "docOID score");
     195       
     196        if (_facets.size() > 0)
     197        {
     198            solrParams.set("facet", "true");
     199            for (int i = 0; i < _facets.size(); i++)
     200            {
     201                solrParams.add("facet.field", _facets.get(i));
     202            }
     203        }
    93204
    94205        try
    95206        {
    96207            QueryResponse solrResponse = solr_core.query(solrParams);
    97 
    98208            SolrDocumentList hits = solrResponse.getResults();
    99209
    100210            if (hits != null)
    101211            {
    102 
    103212                logger.info("*** hits size = " + hits.size());
    104213                logger.info("*** num docs found = " + hits.getNumFound());
     
    108217                logger.info("*** max docs = " + max_docs);
    109218
    110                 // numDocsFound is the total number of mactching docs in the collection
     219                // numDocsFound is the total number of matching docs in the collection
    111220                // as opposed to the number of documents returned in the hits list
    112221
     
    139248                solr_query_result.setEndResults(0);
    140249            }
     250
     251            solr_query_result.setFacetResults(solrResponse.getFacetFields());
    141252        }
    142253        catch (SolrServerException server_exception)
    143254        {
     255            server_exception.printStackTrace();
    144256            solr_query_result.setError(SolrQueryResult.SERVER_ERROR);
    145257        }
     
    148260    }
    149261
     262    //Greenstone universe operates with a base of 1 for "start_results"
     263    //But Solr operates from 0
     264    public void setStartResults(int start_results)
     265    {
     266        if (start_results < 0)
     267        {
     268            start_results = 0;
     269        }
     270        this.start_results = start_results - 1;
     271    }
     272
    150273    public void cleanUp()
    151274    {
Note: See TracChangeset for help on using the changeset viewer.