Permalink
Browse files

enhance executeRequest and tidy AutocompleteTermWidget

  • Loading branch information...
jpmckinney committed Sep 8, 2011
1 parent 8430e04 commit bd9a1d4819de87684ec04757832b08524ab49916
Showing with 76 additions and 90 deletions.
  1. +7 −3 managers/Manager.jquery.js
  2. +69 −87 widgets/jquery/AutocompleteTermWidget.js
View
@@ -8,13 +8,17 @@
AjaxSolr.Manager = AjaxSolr.AbstractManager.extend(
/** @lends AjaxSolr.Manager.prototype */
{
- executeRequest: function (servlet) {
+ executeRequest: function (servlet, string, handler) {
var self = this;
+ string = string || this.store.string();
+ handler = handler || function (data) {
+ self.handleResponse(data);
+ };
if (this.proxyUrl) {
- jQuery.post(this.proxyUrl, { query: this.store.string() }, function (data) { self.handleResponse(data); }, 'json');
+ jQuery.post(this.proxyUrl, { query: string }, handler, 'json');
}
else {
- jQuery.getJSON(this.solrUrl + servlet + '?' + this.store.string() + '&wt=json&json.wrf=?', {}, function (data) { self.handleResponse(data); });
+ jQuery.getJSON(this.solrUrl + servlet + '?' + string + '&wt=json&json.wrf=?', {}, handler);
}
}
});
@@ -10,28 +10,28 @@
* relevant completion suggestions.
*
* Index instructions:
- * Put a facet warming query into Solr's "firstSearcher" in solrconfig.xml, for
- * the target field.
- * Use appropriate text analysis to include a tokenizer (not keyword) and do
- * <i>not</i> do stemming or else you will see stems suggested. A 'light'
+ * 1. Put a facet warming query into Solr's "firstSearcher" in solrconfig.xml,
+ * for the target field.
+ * 2. Use appropriate text analysis to include a tokenizer (not keyword) and do
+ * <i>not</i> do stemming or else you will see stems suggested. A 'light'
* stemmer may produce acceptable stems.
- * If you are auto-completing in a search box that would normally be using
+ * 3. If you are auto-completing in a search box that would normally be using
* the dismax query parser AND your qf parameter references more than one field,
* then you might want to use a catch-all search field to autocomplete on.
*
- * For large indexes, another implementation approach like
- * the Suggester feature or TermsComponent might be better than a faceting
- * approach.
+ * For large indexes, another implementation approach like the Suggester feature
+ * or TermsComponent might be better than a faceting approach.
*
- * FYI: Other types of autocomplete (AKA suggest) are search-results, query-log,
- * and facet-value. Again, this one is term autocompletion.
+ * Other types of autocomplete (a.k.a. suggest) are "search-results",
+ * "query-log", and "facet-value". This widget does term autocompletion.
*
- * @author David Smiley -- david.w.smiley at gmail.com
+ * @author David Smiley <david.w.smiley at gmail.com>
*/
-AjaxSolr.AutocompleteTermWidget = AjaxSolr.AbstractTextWidget.extend({
-
+AjaxSolr.AutocompleteTermWidget = AjaxSolr.AbstractTextWidget.extend(
+ /** @lends AjaxSolr.AutocompleteTermWidget.prototype */
+ {
/**
- * The Solr field to auto-complete indexed terms from.
+ * The Solr field to autocomplete indexed terms from.
*
* @field
* @public
@@ -86,11 +86,12 @@ AjaxSolr.AutocompleteTermWidget = AjaxSolr.AbstractTextWidget.extend({
/**
* The URL path that follows the solr webapp, for use in auto-complete
- * queries.
- * If not specified, the manager's servlet property will be used.
- * You may prepend the servlet with a core if using multiple cores.
- * It is a good idea to use a non-default one to differentiate these requests
- * in server logs and Solr statistics.
+ * queries.
+ *
+ * If not specified, the manager's servlet property will be used. You may
+ * prepend the servlet with a core if using multiple cores. It is a good idea
+ * to use a non-default one to differentiate these requests in server logs and
+ * Solr statistics.
*
* @field
* @public
@@ -101,90 +102,71 @@ AjaxSolr.AutocompleteTermWidget = AjaxSolr.AbstractTextWidget.extend({
init: function () {
var self = this;
-
- if (! self.field)
+
+ if (!this.field) {
throw '"field" must be set on AutocompleteTermWidget.';
- self.servlet = (self.servlet || self.manager.servlet);
+ }
+ this.servlet = this.servlet || this.manager.servlet;
- $(this.target).find('input').bind('keydown', function(e) {
+ $(this.target).find('input').bind('keydown', function (e) {
if (e.which == 13) {
var q = $(this).val();
if (self.set(q)) {
self.manager.doRequest(0);
}
}
});
-
- //as suggested by jpmckinney
- function executeRequest_jquery_enhanced (servlet, string, handler) {
- var self = this;
- string = string || self.store.string();
- handler = handler || function (data) { self.handleResponse(data); };
- if (self.proxyUrl) {
- jQuery.post(this.proxyUrl, { query: string }, handler, 'json');
- }
- else {
- jQuery.getJSON(this.solrUrl + servlet + '?' + string + '&wt=json&json.wrf=?', {}, handler);
- }
- };
-
- function autocomplete_source ( request, response ) {
- //Note: must always call response()
- var qInput = request.term;
- if (qInput.charAt(qInput.length-1).trim() == '') {//ends with a space
- response();
- return;
- }
- var qPrefix = qInput;//facet.prefix value
- var qFilter = "";//before the last word (if we tokenize)
- var store = new AjaxSolr.ParameterStore();
- store.addByValue('fq',self.manager.store.values('fq'));
-
- if (self.tokenized) {
- //-- take the query string and split out the last word from the words
- // before it.
- var lastSpace = qInput.lastIndexOf(' ');
- if (lastSpace > -1) {
- qFilter = qInput.substring(0,lastSpace);
- qPrefix = qInput.substring(lastSpace+1);
- store.addByValue('fq','{!dismax qf='+self.field+'}'+qFilter);
+
+ $(this.target).find('input').autocomplete({
+ source: function (request, response) { // note: must always call response()
+ // If term ends with a space:
+ if (request.term.charAt(request.term.length - 1).trim() == '') {
+ response();
+ return;
}
- }
- if (self.lowercase)
- qPrefix = qPrefix.toLowerCase();
-
- store.addByValue('facet.field',self.field);
- store.addByValue('facet.prefix',qPrefix);
- store.addByValue('facet.limit',self.limit);
- var urlParams = 'json.nl=arrarr&q=*:*&rows=0&facet=true&facet.mincount=1&' + store.string();
-
- function ajaxhandler ( data ) {
- var rspData = $.map( data.facet_counts.facet_fields[self.field], function( term ) {
- var q = (qFilter + " " + term[0]).trim();
- return {
- label: q + " (" + term[1] + ")",
- value: q,
+
+ var term = request.term,
+ facetPrefix = term, // before the last word (if we tokenize)
+ fq = '',
+ store = new AjaxSolr.ParameterStore();
+
+ store.addByValue('fq', self.manager.store.values('fq'));
+
+ if (self.tokenized) {
+ // Split out the last word of the term from the words before it.
+ var lastSpace = term.lastIndexOf(' ');
+ if (lastSpace > -1) {
+ fq = term.substring(0, lastSpace);
+ facetPrefix = term.substring(lastSpace + 1);
+ store.addByValue('fq', '{!dismax qf=' + self.field + '}' + fq);
}
- });
- response( rspData );
- };
+ }
+ if (self.lowercase) {
+ facetPrefix = facetPrefix.toLowerCase();
+ }
- executeRequest_jquery_enhanced.apply(self.manager,
- [self.servlet, urlParams, ajaxhandler ]);
- };//autocomplete_source
-
- $(this.target).find('input').autocomplete({
- source: autocomplete_source,
- minLength: self.minLength,
- select: function( event, ui ) {
+ store.addByValue('facet.field', self.field);
+ store.addByValue('facet.limit', self.limit);
+ store.addByValue('facet.prefix', facetPrefix);
+
+ self.manager.executeRequest(self.servlet, 'json.nl=arrarr&q=*:*&rows=0&facet=true&facet.mincount=1&' + store.string(), function (data) {
+ response($.map(data.facet_counts.facet_fields[self.field], function (term) {
+ var q = (fq + ' ' + term[0]).trim();
+ return {
+ label: q + ' (' + term[1] + ')',
+ value: q
+ }
+ }));
+ });
+ },
+ minLength: this.minLength,
+ select: function(event, ui) {
if (self.set(ui.item.value)) {
self.manager.doRequest(0);
}
- },
+ }
});
-
- },//init()
-
+ }
});
})(jQuery);

0 comments on commit bd9a1d4

Please sign in to comment.