Permalink
Browse files

adding sorting. version bump to 1.2

  • Loading branch information...
dbuthay committed Aug 17, 2011
1 parent bd2c1e3 commit db23a08544dec1a70add1c9707c15aeb4b809c57
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Author:: Diego Buthay (<dbuthay@gmail.com>)
+ * Copyright:: Copyright (c) 2011 indextank.com
+ * License:: Apache License, Version 2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+class Indextank_Exception_IndexDoesNotExist extends Indextank_Exception
+{
+
+}
View
@@ -3,18 +3,18 @@
/**
* @package Indextank Search
* @author Diego Buthay
- * @version 1.1.7
+ * @version 1.2
*/
/*
Plugin Name: IndexTank Search
Plugin URI: http://github.com/flaptor/indextank-wordpress/
Description: IndexTank makes search easy, scalable, reliable .. and makes you happy :)
Author: Diego Buthay
- Version: 1.1.7
+ Version: 1.2
Author URI: http://twitter.com/dbuthay
*/
-$indextank_plugin_version = '1.1.7';
+$indextank_plugin_version = '1.2';
require_once("indextank.php");
// the indextank index format version.
@@ -725,6 +725,15 @@ function inject_indextank_head_script(){
<style>
+ #sorting span.selected {
+ font-weight: bolder;
+ }
+
+ #sorting span {
+ cursor: pointer;
+ }
+
+
#paginator {
font-size: 1.4em;
}
@@ -779,6 +788,7 @@ function indextank_include_js_css(){
wp_enqueue_script("autocomplete", plugins_url( "js/jquery.indextank.autocomplete.js", __FILE__), array("ize"));
wp_enqueue_script("statsrenderer", plugins_url( "js/jquery.indextank.statsrenderer.js", __FILE__), array("ize"));
wp_enqueue_script("renderer", plugins_url( "js/jquery.indextank.renderer.js", __FILE__), array("ize"));
+ wp_enqueue_script("sorting", plugins_url( "js/jquery.indextank.sorting.js", __FILE__), array("ize"));
wp_enqueue_script("pagination", plugins_url( "js/jquery.indextank.pagination.js", __FILE__), array("ize"));
wp_enqueue_script("ajaxsearch", plugins_url( "js/jquery.indextank.ajaxsearch.js", __FILE__), array("ize"));
wp_enqueue_script("querybuilder", plugins_url( "js/querybuilder.js", __FILE__), array("ize"));
View
@@ -65,25 +65,27 @@
}
var afterRender = function($el) {
- // mave paginator to the bottom
+ // move paginator to the bottom
var p = jQuery("#paginator").detach();
$el.append(p);
}
// create some placeholders
var stContainer = jQuery("<div/>").attr("id", "stats").hide();
stContainer.append( jQuery("<span/>") );
+ var sortingContainer = jQuery("<div/>").attr("id","sorting").hide();
var pContainer = jQuery("<div/>").attr("id", "paginator").hide();
- jQuery("#content").prepend(pContainer).prepend(stContainer);
+ jQuery("#content").prepend(pContainer).prepend(stContainer).prepend(sortingContainer);
var rw = function(q) { return 'post_content:(' + q + ') OR post_title:(' + q + ') OR post_author:(' + q + ')';}
var r = jQuery('#content').indextank_Renderer({format: fmt, setupContainer: setupContainer, afterRender:afterRender});
//var st = jQuery('#stats').indextank_StatsRenderer();
var st = stContainer.indextank_StatsRenderer();
var p = pContainer.indextank_Pagination({maxPages:5});
+ var so = sortingContainer.indextank_Sorting({labels: {"relevance":0, "newest":1}});
jQuery('#s').parents('form').indextank_Ize(INDEXTANK_PUBLIC_URL, INDEXTANK_INDEX_NAME);
- jQuery('#s').indextank_Autocomplete().indextank_AjaxSearch({ listeners: r.add(p).add(st),
+ jQuery('#s').indextank_Autocomplete().indextank_AjaxSearch({ listeners: [r,p,st,so],
fields: 'post_title,post_author,timestamp,url,thumbnail,post_content',
snippets:'post_content',
rewriteQuery: rw }).indextank_InstantSearch();
@@ -31,7 +31,17 @@
base.options = $.extend({},$.Indextank.AjaxSearch.defaultOptions, options);
base.xhr = undefined;
-
+
+ // base.options.listeners is ASSUMED to be a jQuery set ..
+ // if we got an Array, we need to convert it
+ if (base.options.listeners instanceof Array) {
+ var listeners = $();
+ $.map(base.options.listeners, function(e, i) {
+ listeners = listeners.add(e);
+ });
+
+ base.options.listeners = listeners;
+ }
// TODO: make sure ize is an Indextank.Ize element somehow
base.ize = $(base.el.form).data("Indextank.Ize");
@@ -122,6 +132,7 @@
// unbind everything
base.destroy = function() {
base.$el.unbind("Indextank.AjaxSearch.runQuery", base.runQuery);
+ base.$el.unbind("Indextank.AjaxSearch.displayNoResults", base.displayNoResults );
base.ize.$el.unbind("submit", base.hijackFormSubmit);
base.$el.removeData("Indextank.AjaxSearch");
};
@@ -74,8 +74,8 @@
bolds = $.map(text.match(rx) || [], function(i) { return "<span class='highlighted'>"+i+"</span>";});
regulars = $( $.map(text.split(rx), function(i){ return $("<span></span>").addClass("regular").text(i).get(0);}));
- return regulars.append(function(i, h) {
- return bolds[i] || "";
+ return $.each(regulars, function(i, v) {
+ $(v).append(bolds[i] || "");
});
};
@@ -26,7 +26,7 @@
query.withQueryString(suggestions[0]);
// run it
base.$el.trigger( "Indextank.AjaxSearch.runQuery", query );
- }
+ } else { base.$el.trigger( "Indextank.AjaxSearch.displayNoResults" ); }
});
// make autocomplete focus trigger an AjaxSearch, only if requested
@@ -13,7 +13,7 @@
base.el = el;
// some parameter validation
- var urlrx = /http(s)?:\/\/[a-z0-9]+.api.indextank.com/
+ var urlrx = /http(s)?:\/\/[a-z0-9]+.api.indextank.com/;
if (!urlrx.test(apiurl)) throw("invalid api url!");
if (indexName == undefined) throw("index name is not defined!");
@@ -31,7 +31,6 @@
// nothing to see here .. go on!
if (totalPages < 2 ) return;
- base.$el.append( $("<div/>").css({'float':'left'}).text('More results'));
var ul = $("<ul/>").addClass("pagination");
// first, put selected page
@@ -76,6 +75,8 @@
}
+ // fix for zero-size, floating non-clickable list items
+ ul.append( $("<div/>").css({ clear: "both" }) );
// make pages clickable
$("li", ul).click(function(e){
@@ -94,12 +95,8 @@
d.searcher.trigger("Indextank.AjaxSearch.runQuery", q);
});
- ul.appendTo(base.$el);
-
- // fix for zero-size, floating non-clickable list items
- ul.append( $("<div/>").css({ clear: "both" }) );
- base.$el.append( $("<div/>").css({ clear: "both" }) );
+ ul.appendTo(base.$el);
});
};
@@ -0,0 +1,82 @@
+(function($){
+ if(!$.Indextank){
+ $.Indextank = new Object();
+ };
+
+ $.Indextank.Sorting = function(el, options){
+ // To avoid scope issues, use 'base' instead of 'this'
+ // to reference this class from internal events and functions.
+ var base = this;
+
+ // Access to jQuery and DOM versions of element
+ base.$el = $(el);
+ base.el = el;
+
+ // Add a reverse reference to the DOM object
+ base.$el.data("Indextank.Sorting", base);
+
+ base.init = function(){
+ base.options = $.extend({},$.Indextank.Sorting.defaultOptions, options);
+
+ // react to result sets ..
+ base.$el.bind( "Indextank.AjaxSearch.success", base.trackQuery);
+
+ // create the control
+ var control = $("<div/>").append("Sort by ");
+ $.each( base.options.labels, function (name, fn){
+ var btn = $("<span/>").text(name);
+ btn.click( function(event){
+ control.children().removeClass("selected");
+ $(this).addClass("selected");
+ if (base.query) {
+ query = base.query.clone().withScoringFunction(fn);
+ // remember to start from scratch
+ query.withStart(0);
+ base.searcher.trigger("Indextank.AjaxSearch.runQuery", [query]);
+ }
+ });
+
+ control.append(btn);
+ });
+
+ // add separator
+ control.children(":not(:first)").prepend(" ", base.options.separator, " ");
+
+ // and make it appear
+ base.$el.append(control);
+ };
+
+ // tracks the latest query, so sorting buttons can run it again, changing the scoring function
+ base.trackQuery = function(event, data) {
+ // keep a copy of the query, so we can re-run it.
+ base.query = data.query;
+ // keep a pointer to the event trigger .. HACKY way
+ base.searcher = data.searcher;
+ };
+
+
+ // clean up
+ base.destroy = function(){
+ base.$el.unbind( "Indextank.AjaxSearch.success", base.trackQuery);
+ base.$el.removeData("Indextank.Sorting");
+ };
+
+ // Run initializer
+ base.init();
+ };
+
+ $.Indextank.Sorting.defaultOptions = {
+ // you should REALLY provide it, as the default is useless
+ labels: { 'newest' : 0 },
+
+ // the separator for possible sorting names. just for formatting
+ separator : "|"
+ };
+
+ $.fn.indextank_Sorting = function(options){
+ return this.each(function(){
+ (new $.Indextank.Sorting(this, options));
+ });
+ };
+
+})(jQuery);
View
@@ -82,6 +82,18 @@ Query.prototype.withCategoryFilters = function (categoryFilters) {
return this;
};
+Query.prototype.withoutCategories = function (categories) {
+ if (this.categoryFilters == null) {
+ this.categoryFilters = {};
+ } else {
+ for (idx in categories) {
+ delete this.categoryFilters[categories[idx]];
+ }
+ }
+
+ return this;
+};
+
Query.prototype.withQueryVariables = function (queryVariables) {
if (this.queryVariables == null) {
this.queryVariables = {};
@@ -120,7 +132,7 @@ Query.prototype.clone = function() {
if (this.scoringFunction != null) q.scoringFunction = this.scoringFunction;
if (this.snippetFields != null) q.snippetFields = this.snippetFields;
if (this.fetchFields != null) q.fetchFields = this.fetchFields;
- if (this.category_filters != null) q.category_filters = this.category_filters;
+ if (this.categoryFilters != null) q.categoryFilters = this.categoryFilters;
if (this.documentVariableFilters != null) q.documentVariableFilters = this.documentVariableFilters;
if (this.functionFilters != null) q.functionFilters = this.functionFilters;
if (this.queryVariables != null) q.queryVariables = this.queryVariables;
View
@@ -3,7 +3,7 @@ Contributors: dbuthay, flaptor
Tags: search, better search, search replacement, autocomplete, ajax, cloud, instantsearch, suggestions, autocorrect
Requires at least: 2.8
Tested up to: 3.2.1
-Stable tag: 1.1.7
+Stable tag: 1.2
IndexTank hosted, realtime search (FTW!)
@@ -56,6 +56,10 @@ You'll get a message on your dashboard, in case you need to upgrade your index.
== Changelog ==
+= 1.2 =
+
+* Add sorting controls to search results. You can now sort results by *relevance*, *newest* and *comments*
+
= 1.1.7 =
* Trim posts longer than indextank's API limit. Only the first 100kb will be indexed for each post.

0 comments on commit db23a08

Please sign in to comment.