From cceca1659bd40916f06394548940f100dcf03596 Mon Sep 17 00:00:00 2001 From: Jim Blake Date: Wed, 23 Apr 2014 14:15:12 -0400 Subject: [PATCH] VIVO-742 Refine the search interface --- .../webapp/dao/jena/VClassGroupCache.java | 3 +- .../modules/searchEngine/SearchQuery.java | 71 ++++++++++--------- .../controller/PagedSearchController.java | 9 +-- .../searchengine/base/BaseSearchQuery.java | 45 +++--------- .../solr/SolrConversionUtils.java | 18 ++--- .../searchindex/SearchIndexerSetup.java | 16 ++--- 6 files changed, 64 insertions(+), 98 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java index bdb2cb884..9491a1f14 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java @@ -327,8 +327,7 @@ public class VClassGroupCache implements IndexingEventListener { SearchQuery query = searchEngine.createQuery(). setRows(0). setQuery(VitroSearchTermNames.CLASSGROUP_URI + ":" + groupUri ). - setFaceting(true). //facet on type to get counts for classes in classgroup - addFacetFields( facetOnField ). + addFacetFields( facetOnField ). //facet on type to get counts for classes in classgroup setFacetMinCount(0); log.debug("query: " + query); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/modules/searchEngine/SearchQuery.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/modules/searchEngine/SearchQuery.java index 10977367a..f15d7b076 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/modules/searchEngine/SearchQuery.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/modules/searchEngine/SearchQuery.java @@ -3,7 +3,6 @@ package edu.cornell.mannlib.vitro.webapp.modules.searchEngine; import java.util.Collection; -import java.util.List; import java.util.Map; import java.util.Set; @@ -20,118 +19,124 @@ public interface SearchQuery { /** * Set the text of the query. This will be parsed using Lucene query syntax. + * + * @return this query */ SearchQuery setQuery(String query); /** * Where in the ordered list of result documents should the response begin? * That is, how many of the results should be skipped? (allows paging of - * results) + * results). The default is 0. + * + * @return this query */ SearchQuery setStart(int start); /** * What is the maximum number of documents that will be returned from the - * query? + * query? A negative value means no limit. The default is -1. + * + * @return this query */ SearchQuery setRows(int rows); /** * Which fields should be returned from the query? + * + * @return this query */ SearchQuery addFields(String... names); /** * Which fields should be returned from the query? + * + * @return this query */ SearchQuery addFields(Collection names); /** * What field should be used to sort the results, and in what order? + * + * @return this query */ SearchQuery addSortField(String name, Order order); /** - * Restrict the results by thisw query. + * Restrict the results by this query. + * + * @return this query */ SearchQuery addFilterQuery(String filterQuery); /** * Restrict the results by these queries. + * + * @return this query */ SearchQuery addFilterQueries(String... filterQueries); - /** - * Should the results be faceted? - */ - SearchQuery setFaceting(boolean b); - /** * What fields should be used to facet the results? + * + * @return this query */ SearchQuery addFacetFields(String... fields); /** - * Add queries that can be used to facet the results. + * The maximum number of facet counts that will be returned from the query. + * The default is 100. A negative value means no limit. + * + * @return this query */ - SearchQuery addFacetQueries(String... queries); + SearchQuery setFacetLimit(int cnt); /** - * Facet having fewer hits will be excluded from the list. + * Facet having fewer hits will be excluded from the list. The default is 0. + * + * @return this query */ SearchQuery setFacetMinCount(int cnt); /** - * Add a system-dependent parameter to the query. - */ - SearchQuery addParameter(String name, String... values); - - /** - * Get the text of the query. + * @return The text of the query. May be empty, but never null. */ String getQuery(); int getStart(); /** - * A value of -1 means that no limit has been specified. + * @return A negative value means that no limit has been specified. */ int getRows(); /** - * May return an empty set, but never null. + * @return May return an empty set, but never null. */ Set getFieldsToReturn(); /** - * May return an empty map, but never null. + * @return May return an empty map, but never null. */ Map getSortFields(); /** - * May return an empty set, but never null. + * @return May return an empty set, but never null. */ Set getFilters(); - boolean isFaceting(); - /** - * May return an empty set, but never null. + * @return May return an empty set, but never null. */ Set getFacetFields(); /** - * May return an empty set, but never null. + * @return A negative value means that no limit has been specified. */ - Set getFacetQueries(); + int getFacetLimit(); /** - * A value of -1 means that no limit has been specified. + * @return A negative value means that no limit has been specified. */ int getFacetMinCount(); - - /** - * May return an empty map, but never null. - */ - Map> getParameterMap(); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java index cadbf36dc..c968390d6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java @@ -484,22 +484,17 @@ public class PagedSearchController extends FreemarkerHttpServlet { query.addFilterQuery(VitroSearchTermNames.CLASSGROUP_URI + ":\"" + classgroupParam + "\""); //with ClassGroup filtering we want type facets - query.addParameter("facet","true"); - query.addParameter("facet.limit","-1"); - query.addParameter("facet.field",VitroSearchTermNames.RDFTYPE); + query.addFacetFields(VitroSearchTermNames.RDFTYPE).setFacetLimit(-1); }else if ( ! StringUtils.isBlank(typeParam) ) { // rdf:type filtering log.debug("Firing type query "); log.debug("request.getParameter(type) is "+ typeParam); query.addFilterQuery(VitroSearchTermNames.RDFTYPE + ":\"" + typeParam + "\""); - //with type filtering we don't have facets. }else{ //When no filtering is set, we want ClassGroup facets - query.addParameter("facet","true"); - query.addParameter("facet.limit","-1"); - query.addParameter("facet.field",VitroSearchTermNames.CLASSGROUP_URI); + query.addFacetFields(VitroSearchTermNames.CLASSGROUP_URI).setFacetLimit(-1); } log.debug("Query = " + query.toString()); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/base/BaseSearchQuery.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/base/BaseSearchQuery.java index eccc6b243..7b001cc2e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/base/BaseSearchQuery.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/base/BaseSearchQuery.java @@ -2,13 +2,11 @@ package edu.cornell.mannlib.vitro.webapp.searchengine.base; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -26,13 +24,10 @@ public class BaseSearchQuery implements SearchQuery { private final Map sortFields = new HashMap<>(); private final Set filters = new HashSet<>(); - private boolean faceting; private final Set facetFields = new HashSet<>(); - private final Set facetQueries = new HashSet<>(); + private int facetLimit = 100; private int facetMinCount = -1; - private final Map> parameterMap = new HashMap<>(); - @Override public SearchQuery setQuery(String query) { this.queryText = query; @@ -80,12 +75,6 @@ public class BaseSearchQuery implements SearchQuery { return this; } - @Override - public SearchQuery setFaceting(boolean b) { - this.faceting = b; - return this; - } - @Override public SearchQuery addFacetFields(String... fields) { facetFields.addAll(Arrays.asList(fields)); @@ -93,8 +82,8 @@ public class BaseSearchQuery implements SearchQuery { } @Override - public SearchQuery addFacetQueries(String... queries) { - facetQueries.addAll(Arrays.asList(queries)); + public SearchQuery setFacetLimit(int cnt) { + facetLimit = cnt; return this; } @@ -104,13 +93,6 @@ public class BaseSearchQuery implements SearchQuery { return this; } - @Override - public SearchQuery addParameter(String name, String... values) { - parameterMap.put(name, Collections.unmodifiableList(new ArrayList<>( - Arrays.asList(values)))); - return this; - } - @Override public String getQuery() { return queryText; @@ -141,19 +123,14 @@ public class BaseSearchQuery implements SearchQuery { return Collections.unmodifiableSet(filters); } - @Override - public boolean isFaceting() { - return faceting; - } - @Override public Set getFacetFields() { return Collections.unmodifiableSet(facetFields); } @Override - public Set getFacetQueries() { - return Collections.unmodifiableSet(facetQueries); + public int getFacetLimit() { + return facetLimit; } @Override @@ -161,19 +138,13 @@ public class BaseSearchQuery implements SearchQuery { return facetMinCount; } - @Override - public Map> getParameterMap() { - return Collections.unmodifiableMap(parameterMap); - } - @Override public String toString() { - return "BaseSearchQuery [queryText=" + queryText + ", start=" + start + return "BaseSearchQuery[queryText=" + queryText + ", start=" + start + ", rows=" + rows + ", fieldsToReturn=" + fieldsToReturn + ", sortFields=" + sortFields + ", filters=" + filters - + ", faceting=" + faceting + ", facetFields=" + facetFields - + ", facetQueries=" + facetQueries + ", facetMinCount=" - + facetMinCount + ", parameterMap=" + parameterMap + "]"; + + ", facetFields=" + facetFields + ", facetLimit=" + facetLimit + + ", facetMinCount=" + facetMinCount + "]"; } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/solr/SolrConversionUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/solr/SolrConversionUtils.java index 98f9f3f2d..a9849491c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/solr/SolrConversionUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/searchengine/solr/SolrConversionUtils.java @@ -102,28 +102,24 @@ public class SolrConversionUtils { solrQuery.addFilterQuery(filter); } - solrQuery.setFacet(query.isFaceting()); + if (!query.getFacetFields().isEmpty()) { + solrQuery.setFacet(true); + } for (String facetField : query.getFacetFields()) { solrQuery.addFacetField(facetField); } - for (String facetQuery : query.getFacetQueries()) { - solrQuery.addFacetQuery(facetQuery); + int facetLimit = query.getFacetLimit(); + if (facetLimit >= 0) { + solrQuery.setFacetLimit(facetLimit); } - + int minCount = query.getFacetMinCount(); if (minCount >= 0) { solrQuery.setFacetMinCount(minCount); } - Map> parameterMap = query.getParameterMap(); - for (String parameterName : parameterMap.keySet()) { - String[] values = parameterMap.get(parameterName).toArray( - new String[0]); - solrQuery.add(parameterName, values); - } - return solrQuery; } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/searchindex/SearchIndexerSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/searchindex/SearchIndexerSetup.java index de51e80f1..f55acad33 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/searchindex/SearchIndexerSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/searchindex/SearchIndexerSetup.java @@ -77,7 +77,7 @@ public class SearchIndexerSetup implements ServletContextListener { SearchEngine searchEngine = ApplicationUtils.instance().getSearchEngine(); try { - /* set up the individual to solr doc translation */ + /* set up the individual to search doc translation */ OntModel jenaOntModel = ModelAccess.on(context).getJenaOntModel(); OntModel displayModel = ModelAccess.on(context).getDisplayModel(); @@ -100,13 +100,13 @@ public class SearchIndexerSetup implements ServletContextListener { List searchIndexExcludesFromContext = (List) context .getAttribute("SearchIndexExcludes"); - IndividualToSearchDocument indToSolrDoc = setupTransltion( + IndividualToSearchDocument indToSearchDoc = setupTranslation( jenaOntModel, displayModel, RDFServiceUtils.getRDFServiceFactory(context), modifiersFromContext, searchIndexExcludesFromContext); - /* setup solr indexer */ - SearchIndexer solrIndexer = new SearchIndexer(searchEngine, indToSolrDoc); + /* setup search indexer */ + SearchIndexer searchIndexer = new SearchIndexer(searchEngine, indToSearchDoc); // This is where the builder gets the list of places to try to // get objects to index. It is filtered so that non-public text @@ -123,7 +123,7 @@ public class SearchIndexerSetup implements ServletContextListener { .getList(rdfService, wadf.getIndividualDao()); // Make the IndexBuilder - IndexBuilder builder = new IndexBuilder(solrIndexer, wadf, + IndexBuilder builder = new IndexBuilder(searchIndexer, wadf, uriFinders); // Save it to the servlet context so we can access it later in the // webapp. @@ -135,9 +135,9 @@ public class SearchIndexerSetup implements ServletContextListener { SearchReindexingListener srl = new SearchReindexingListener(builder); ModelContext.registerListenerForChanges(ctx, srl); - ss.info(this, "Setup of Solr index completed."); + ss.info(this, "Setup of search indexer completed."); } catch (Throwable e) { - ss.fatal(this, "could not setup local solr server", e); + ss.fatal(this, "could not setup search engine", e); } } @@ -151,7 +151,7 @@ public class SearchIndexerSetup implements ServletContextListener { } - public static IndividualToSearchDocument setupTransltion( + public static IndividualToSearchDocument setupTranslation( OntModel jenaOntModel, Model displayModel, RDFServiceFactory rdfServiceFactory, List modifiersFromContext,