Author: ssmiweve
Date: 2009-03-03 12:11:09 +0100 (Tue, 03 Mar 2009)
New Revision: 7214

Added:
   
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSimpleFacetToolkitImpl.java
Modified:
   
branches/2.18/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/SolrCommandConfig.java
   
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSearchCommand.java
Log:
Additions to Solr searching functionality:
 - possible to configure the query type (solr's qt parameter) so to delegate to 
non-default request handlers
 - make the FacetToolkit configuration work (was no FacetToolkitFactory before 
loading from the correct Spi classloader)
 - make result-fields an optional configuration. when blank all fields defined 
by the request handler are imported into the ResultItem
 - append any existing filter to the query string, solr does not any a separate 
parameter for general field filters
 - extract the SolrSimpleFacetToolkitImpl to a separate class in same package


Modified: 
branches/2.18/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/SolrCommandConfig.java
===================================================================
--- 
branches/2.18/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/SolrCommandConfig.java
  2009-03-03 10:03:12 UTC (rev 7213)
+++ 
branches/2.18/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/SolrCommandConfig.java
  2009-03-03 11:11:09 UTC (rev 7214)
@@ -52,6 +52,8 @@
 
     private String facetToolkit;
 
+    private String queryType = null;
+
     // Static --------------------------------------------------------
 
     // Constructors --------------------------------------------------
@@ -97,6 +99,23 @@
         this.filteringQuery = filteringQuery;
     }
 
+    /** Sets the qt parameter in turn choosing a query handler.
+     * {...@link http://wiki.apache.org/solr/CoreQueryParameters}
+     *
+     * @return
+     */
+    public String getQueryType(){
+        return queryType;
+    }
+
+    /** @see #getQueryType()
+     *
+     * @param filteringQuery
+     */
+    public void setQueryType(final String queryType){
+        this.queryType = queryType;
+    }
+
     /** @see #setFieldFilters(java.lang.String[])
      *
      * @return Value of map property sort.

Modified: 
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSearchCommand.java
===================================================================
--- 
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSearchCommand.java
        2009-03-03 10:03:12 UTC (rev 7213)
+++ 
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSearchCommand.java
        2009-03-03 11:11:09 UTC (rev 7214)
@@ -17,23 +17,24 @@
  */
 package no.sesat.search.mode.command;
 
+import java.io.Serializable;
 import java.lang.ref.Reference;
 import java.net.MalformedURLException;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.locks.ReentrantLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
+import no.sesat.commons.ioc.BaseContext;
+import no.sesat.commons.ioc.ContextWrapper;
 import no.sesat.commons.ref.ReferenceMap;
-import no.sesat.search.datamodel.generic.StringDataObject;
-import no.sesat.search.datamodel.request.ParametersDataObject;
-import no.sesat.search.mode.config.FacetedCommandConfig;
 import no.sesat.search.mode.config.SolrCommandConfig;
 import no.sesat.search.result.BasicResultItem;
 import no.sesat.search.result.BasicResultList;
-import no.sesat.search.result.Navigator;
 import no.sesat.search.result.ResultItem;
 import no.sesat.search.result.ResultList;
+import no.sesat.search.site.Site;
+import no.sesat.search.site.config.SiteClassLoaderFactory;
 import no.sesat.search.site.config.SiteConfiguration;
+import no.sesat.search.site.config.Spi;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.solr.client.solrj.SolrQuery;
@@ -91,7 +92,7 @@
             LOG.error(ex.getMessage(), ex);
         }
 
-        facetToolkit = new SimpleFacetToolkitImpl();
+        facetToolkit = createFacetToolkit();
     }
 
     // Public --------------------------------------------------------
@@ -101,15 +102,28 @@
 
         final ResultList<ResultItem> searchResult = new 
BasicResultList<ResultItem>();
 
+        // @todo make the join between query and filter configurable
+        final String q = getTransformedQuery()
+                + (0 < getFilter().length() ? " (" + getFilter() + ')' : "");
+
         try {
             // set up query
             final SolrQuery query = new SolrQuery()
-                    .setQuery(getTransformedQuery())
+                    .setQuery(q)
                     
.setFilterQueries(getSearchConfiguration().getFilteringQuery())
                     .setStart(getOffset())
-                    .setRows(getSearchConfiguration().getResultsToReturn())
-                    
.setFields(getSearchConfiguration().getResultFieldMap().keySet().toArray(new 
String[]{}));
+                    .setRows(getSearchConfiguration().getResultsToReturn());
 
+            // custom query type
+            if(null != getSearchConfiguration().getQueryType() && 0 < 
getSearchConfiguration().getQueryType().length()){
+                query.setQueryType(getSearchConfiguration().getQueryType());
+            }
+
+            // The request handler may be configured in the index which fields 
to return in the results
+            if(0 < getSearchConfiguration().getResultFieldMap().size()){
+                
query.setFields(getSearchConfiguration().getResultFieldMap().keySet().toArray(new
 String[]{}));
+            }
+
             createFacets(query);
 
             // when the root logger is set to DEBUG do not limit connection 
times
@@ -156,17 +170,8 @@
 
         FacetToolkit toolkit = null;
         final String toolkitName = getSearchConfiguration().getFacetToolkit();
-        if(null != toolkit){
-            try{
-                final Class<FacetToolkit> cls = (Class<FacetToolkit>) 
Class.forName(toolkitName);
-                toolkit = cls.newInstance();
-            }catch(ClassNotFoundException cnfe){
-                LOG.error(cnfe.getMessage());
-            }catch(InstantiationException ie){
-                LOG.error(ie.getMessage());
-            }catch(IllegalAccessException iae){
-                LOG.error(iae.getMessage());
-            }
+        if(null != toolkitName && 0 < toolkitName.length()){
+            toolkit = FacetToolkitFactory.getInstance(context, toolkitName);
         }
         return toolkit;
     }
@@ -179,11 +184,29 @@
 
     protected BasicResultItem createItem(final SolrDocument doc) {
 
+        Map<String,String> fieldNames;
+        if(0 < getSearchConfiguration().getResultFieldMap().size()){
+            fieldNames = getSearchConfiguration().getResultFieldMap();
+        }else{
+            // The request handler must be configured in the index as to which 
fields to return in the results
+            fieldNames = new HashMap<String,String>();
+            for(String fieldName : doc.getFieldNames()){
+                fieldNames.put(fieldName, fieldName);
+            }
+        }
+
         BasicResultItem item = new BasicResultItem();
 
-        for (final Map.Entry<String,String> entry : 
getSearchConfiguration().getResultFieldMap().entrySet()){
+        for (final Map.Entry<String,String> entry : fieldNames.entrySet()){
 
-            item = item.addField(entry.getValue(), 
(String)doc.getFieldValue(entry.getKey()));
+            final Object value = doc.getFieldValue(entry.getKey());
+            if(value instanceof String){
+                item = item.addField(entry.getValue(), 
(String)doc.getFieldValue(entry.getKey()));
+            }else if(value instanceof Serializable){
+                item = item.addObjectField(entry.getValue(), 
(Serializable)doc.getFieldValue(entry.getKey()));
+            }else{
+                LOG.warn("Unable to add to ResultItem this non Serializable 
object: " + value);
+            }
 
         }
 
@@ -201,47 +224,57 @@
         void createFacets(SearchCommand.Context context, SolrQuery query);
     }
 
-    /**
-     * Solr's Simple Faceting toolkit.
-     *
-     * {...@link http://wiki.apache.org/solr/SolrFacetingOverview}
-     * {...@link http://wiki.apache.org/solr/SimpleFacetParameters}
-     */
-    public static class SimpleFacetToolkitImpl implements FacetToolkit{
+    protected static final class FacetToolkitFactory {
 
-        public void createFacets(final SearchCommand.Context context, final 
SolrQuery query) {
+        // Constructors --------------------------------------------------
 
-            final Map<String,Navigator> facets = 
getSearchConfiguration(context).getFacets();
+        /** Not possible to create a new instance of FacetToolkitFactory */
+        private FacetToolkitFactory() {
+        }
 
-            query.setFacet(0 < facets.size());
+        // Public --------------------------------------------------------
 
-            // facet counters
-            for(Navigator facet : facets.values()){
-                query.addFacetField(facet.getField());
-            }
+        /** Factory call to instiantate a FacetToolkit.
+         *
+         * @param context context providing Resource
+         * @param name the name of the class implementing FacetToolkit
+         * @return
+         */
+        public static FacetToolkit getInstance(
+                final Context context,
+                final String name){
 
-            // facet selection
-            for (final Navigator facet : facets.values()) {
+            try{
+                final Site site = context.getDataModel().getSite().getSite();
 
-                final StringDataObject facetValue = 
context.getDataModel().getParameters().getValue(facet.getId());
+                final SiteClassLoaderFactory.Context ctlContext = 
ContextWrapper.wrap(
+                        SiteClassLoaderFactory.Context.class,
+                        new BaseContext() {
+                            public Spi getSpi() {
+                                return Spi.SEARCH_COMMAND_CONTROL;
+                            }
+                            public Site getSite(){
+                                return site;
+                            }
+                        },
+                        context
+                    );
 
-                if (null != facetValue) {
+                final ClassLoader ctlLoader = 
SiteClassLoaderFactory.instanceOf(ctlContext).getClassLoader();
 
-                    // splitting here allows for multiple navigation 
selections within the one navigation level.
-                    for(String navSingleValue : 
facetValue.getString().split(",")){
+                @SuppressWarnings("unchecked")
+                final Class<? extends FacetToolkit> cls = (Class<? extends 
FacetToolkit>)ctlLoader.loadClass(name);
 
-                        final String value =  facet.isBoundaryMatch()
-                                ? "^\"" + navSingleValue + "\"$"
-                                : "\"" + navSingleValue + "\"";
+                return cls.newInstance();
 
-                        query.addFacetQuery(facet.getField() + ':' + value);
-                    }
-                }
+            } catch (ClassNotFoundException ex) {
+                throw new IllegalArgumentException(ex);
+            } catch (InstantiationException ex) {
+                throw new IllegalArgumentException(ex);
+            } catch (IllegalAccessException ex) {
+                throw new IllegalArgumentException(ex);
             }
         }
 
-        private FacetedCommandConfig getSearchConfiguration(final 
SearchCommand.Context context){
-            return (FacetedCommandConfig) context.getSearchConfiguration();
-        }
     }
 }

Added: 
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSimpleFacetToolkitImpl.java
===================================================================
--- 
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSimpleFacetToolkitImpl.java
                               (rev 0)
+++ 
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSimpleFacetToolkitImpl.java
       2009-03-03 11:11:09 UTC (rev 7214)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (2009) Schibsted Søk AS
+ * This file is part of SESAT.
+ *
+ *   SESAT is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Affero General Public License as published 
by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   SESAT is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Affero General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public License
+ *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package no.sesat.search.mode.command;
+
+import java.util.Map;
+import no.sesat.search.datamodel.generic.StringDataObject;
+import no.sesat.search.mode.config.FacetedCommandConfig;
+import no.sesat.search.result.Navigator;
+import org.apache.solr.client.solrj.SolrQuery;
+
+/**
+ * Solr's Simple Faceting toolkit.
+ *
+ * {...@link http://wiki.apache.org/solr/SolrFacetingOverview}
+ * {...@link http://wiki.apache.org/solr/SimpleFacetParameters}
+ */
+public class SolrSimpleFacetToolkitImpl implements 
SolrSearchCommand.FacetToolkit {
+
+    public void createFacets(final SearchCommand.Context context, final 
SolrQuery query) {
+
+        final Map<String, Navigator> facets = 
getSearchConfiguration(context).getFacets();
+        query.setFacet(0 < facets.size());
+
+        // facet counters
+        for (Navigator facet : facets.values()) {
+            query.addFacetField(facet.getField());
+        }
+
+        // facet selection
+        for (final Navigator facet : facets.values()) {
+            final StringDataObject facetValue = 
context.getDataModel().getParameters().getValue(facet.getId());
+
+            if (null != facetValue) {
+                // splitting here allows for multiple navigation selections 
within the one navigation level.
+                for (String navSingleValue : 
facetValue.getString().split(",")) {
+
+                    final String value = facet.isBoundaryMatch()
+                            ? "^\"" + navSingleValue + "\"$"
+                            : "\"" + navSingleValue + "\"";
+
+                    query.addFacetQuery(facet.getField() + ':' + value);
+                }
+            }
+        }
+    }
+
+    private FacetedCommandConfig getSearchConfiguration(final 
SearchCommand.Context context) {
+        return (FacetedCommandConfig) context.getSearchConfiguration();
+    }
+}


Property changes on: 
branches/2.18/generic.sesam/search-command-control/default/src/main/java/no/sesat/search/mode/command/SolrSimpleFacetToolkitImpl.java
___________________________________________________________________
Added: svn:keywords
   + Id

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to