Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented text-based search with categories, although the "popular"

category is not currently supported.
  • Loading branch information...
commit 6cd84fcf998493ed08a7e1c6717d4d3316ba7eba 1 parent d222059
@pledbrook pledbrook authored
View
9 grails.org/grails-app/conf/UrlMappings.groovy
@@ -30,10 +30,11 @@ class UrlMappings {
"/plugin/showTag"(controller: 'plugin', action:'showTag')
"/plugin/postComment/$id"(controller: "plugin", action:"postComment")
"/plugin/latest"(controller: "plugin", action: "latest")
- "/plugin/category/all"(controller:"plugin", action:"browseByName")
- "/plugin/category/$category"(controller:"plugin", action:"home")
- "/plugin/showComment/$id"(controller: 'plugin', action:'showComment')
- "/plugins/tag/$tagName?"(controller: 'plugin', action: 'browseByTag')
+ "/plugin/category/all"(controller: "plugin", action: "browseByName")
+ "/plugin/category/$category"(controller: "plugin", action: "home")
+ "/plugin/showComment/$id"(controller: "plugin", action: "showComment")
+ "/plugins/category/$category"(controller: "plugin", action: "home")
+ "/plugins/tag/$tagName"(controller: "plugin", action: "browseByTag")
"/content/postComment/$id"(controller: "content", action:"postComment")
View
9 grails.org/grails-app/controllers/org/grails/plugin/PluginController.groovy
@@ -52,7 +52,14 @@ class PluginController extends BaseWikiController {
try {
if (params.q) {
- (currentPlugins, totalPlugins) = pluginService.searchWithTotal(params.q, params)
+ // Build the arguments for the search, starting with the query
+ // string. We add the category if it's defined. Finally we add
+ // the search options.
+ def args = [params.q]
+ if (category) args << category
+ args << queryParams
+
+ (currentPlugins, totalPlugins) = pluginService.searchWithTotal(*args)
}
else {
(currentPlugins, totalPlugins) = pluginService."list${category.capitalize()}PluginsWithTotal"(queryParams)
View
107 grails.org/grails-app/services/org/grails/plugin/PluginService.groovy
@@ -281,27 +281,132 @@ class PluginService {
pluginXml.@grailsVersion.toString()
}
+ /**
+ * Text-based search using the given Lucene-compatible query string.
+ * Returns a list of Plugin instances, although they may not be fully
+ * hydrated, i.e. any non-searchable properties will not be populated.
+ * @param query The Lucene-compatible query string.
+ * @param options A map of search modifiers, such as 'sort', 'offset'
+ * and 'max'.
+ */
protected final search(String query, Map options) {
return searchWithResults(query, options).results
}
+ /**
+ * Text-based search using the given Lucene-compatible query string.
+ * Returns a list of Plugin instances, although they may not be fully
+ * hydrated, i.e. any non-searchable properties will not be populated.
+ * @param query The Lucene-compatible query string.
+ * @param category The category of plugin to constrain the search to:
+ * 'featured', 'newest', 'recentlyUpdated', 'supported'. Note that
+ * 'popular' is not currently supported by text-based search.
+ * @param options A map of search modifiers, such as 'sort', 'offset'
+ * and 'max'.
+ */
+ protected final search(String query, String category, Map options) {
+ query = categoryToSearchConstraint(category) + " " + query
+ options << optionsForCategory(category)
+
+ return searchWithResults(query, options).results
+ }
+
+ /**
+ * Same as {@link #search(String, Map)} except it supports the options
+ * as named arguments.
+ */
protected final search(Map options, String query) {
return searchWithResults(query, options).results
}
+ /**
+ * Text-based search using the given Lucene-compatible query string.
+ * Returns a tuple containing the list of Plugin instances matching
+ * the query and the total number of results. The plugins objects
+ * may not be fully hydrated, i.e. any non-searchable properties will
+ * not be populated.
+ * @param query The Lucene-compatible query string.
+ * @param options A map of search modifiers, such as 'sort', 'offset'
+ * and 'max'.
+ */
protected final searchWithTotal(String query, Map options) {
def results = searchWithResults(query, options)
return [results.results, results.total]
}
+ /**
+ * Text-based search using the given Lucene-compatible query string.
+ * Returns a tuple containing the list of Plugin instances matching
+ * the query and the total number of results. The plugins objects
+ * may not be fully hydrated, i.e. any non-searchable properties will
+ * not be populated.
+ * @param query The Lucene-compatible query string.
+ * @param category The category of plugin to constrain the search to:
+ * 'featured', 'newest', 'recentlyUpdated', 'supported'. Note that
+ * 'popular' is not currently supported by text-based search.
+ * @param options A map of search modifiers, such as 'sort', 'offset'
+ * and 'max'.
+ */
+ protected final searchWithTotal(String query, String category, Map options) {
+ query = categoryToSearchConstraint(category) + " " + query
+ options << optionsForCategory(category)
+
+ def results = searchWithResults(query, options)
+ return [results.results, results.total]
+ }
+
+ /**
+ * Same as {@link #searchWithTotal(String, Map)} except it supports the
+ * options as named arguments.
+ */
protected final searchWithTotal(Map options, String query) {
def results = searchWithResults(query, options)
return [results.results, results.total]
}
- private final searchWithResults(String query, Map options = [:]) {
+ /**
+ * Executes a Searchable search and returns the results object.
+ */
+ private searchWithResults(String query, Map options = [:]) {
return Plugin.search(query, options)
}
+
+ /**
+ * Returns a map of search options based on the given category. These
+ * search options can be used to override those provided in a normal
+ * search. If the category has no requirements on the search options,
+ * this method returns an empty map.
+ */
+ private Map optionsForCategory(String category) {
+ switch(category.toLowerCase()) {
+ case "newest":
+ return [sort: "dateCreated", order: "desc"]
+
+ case "recentlyUpdate":
+ return [sort: "lastReleased", order: "desc"]
+
+ default:
+ return ""
+ }
+ }
+
+ /**
+ * Given a category, this method returns a query fragment that can be
+ * attached to an existing Lucence-compatible query string to constrain
+ * the results to plugins within that category.
+ */
+ private String categoryToSearchConstraint(String category) {
+ switch(category.toLowerCase()) {
+ case "featured":
+ return "+featured:true"
+
+ case "supported":
+ return "+official:true"
+
+ default:
+ return ""
+ }
+ }
}
class PluginVersion implements Comparable {
Please sign in to comment.
Something went wrong with that request. Please try again.