在Ajax-Solr中进行拼写检查

时间:2013-07-04 11:49:13

标签: jquery ajax solr

我正在使用Ajax-Solr框架来查询Solr。 Ajax-Solr框架:https://github.com/evolvingweb/ajax-solr

我想使用solr的“拼写检查”功能。

伙计们,请帮我实施。

此致 耆那

1 个答案:

答案 0 :(得分:1)

我在ajax-solr google群组中找到了这个例子。

Manager.addWidget(new AjaxSolr.SpellcheckWidget({
   })); 

SpellcheckWidget.js文件

(function ($) {

// For an AutocompleteWidget that uses the q parameter, see:
// https://github.com/evolvingweb/ajax-solr/blob/gh-pages/examples/reuters/widgets/AutocompleteWidget.q.js
AjaxSolr.SpellcheckWidget = AjaxSolr.AbstractSpellcheckWidget.extend({


     suggestion: function () {
        var replacePairs = {};
        for (var word in this.suggestions) {
          replacePairs[word] = this.suggestions[word][0];
        }
        return this.manager.response.responseHeader.params['spellcheck.q'].strtr(replacePairs);
      },


      beforeRequest: function () {
            if (this.manager.store.get('spellcheck').val() && this.manager.store.get('q').val()) {
               this.manager.store.get('spellcheck.q').val(this.manager.store.get('q').val());
            }
            else {
              this.manager.store.remove('spellcheck.q');
            }
          },

          afterRequest: function () {
                this.suggestions = {};
                alert(this.manager.response.spellcheck);
                if (this.manager.response.spellcheck && this.manager.response.spellcheck.suggestions) {
                  var suggestions = this.manager.response.spellcheck.suggestions;

                  for (var word in suggestions) {
                    if (word == 'collation' || word == 'correctlySpelled') continue;

                    this.suggestions[word] = [];
                    for (var i = 0, l = suggestions[word].suggestion.length; i < l; i++) {
                      if (this.manager.response.responseHeader.params['spellcheck.extendedResults']) {
                        this.suggestions[word].push(suggestions[word].suggestion[i].word);
                      }
                      else {
                        this.suggestions[word].push(suggestions[word].suggestion[i]);
                      }
                    }
                  }

                  if (AjaxSolr.size(this.suggestions)) {
                    this.handleSuggestions(this.manager.response);
                  }
                }
              },
              /**
               * An abstract hook for child implementations.
               *
               * <p>Allow the child to handle the suggestions without parsing the response.</p>
               */
              handleSuggestions: function () {}
            });


})(jQuery);