1 of 1 found this page helpful

Search suggestion tool using the MindTouch API

     Table of Contents

    This Page was viewed: 6038 Times

     

    Introduction

     This template will allow you to insert a search suggestion box on any page or your MindTouch Wiki.  As input is typed it will perform a search and return matching results of Pages, Files, and Comments.  All questions and comments should be directed to this forum thread.

    History

    Version Date Author Description
    1.6 15/11/2010 carles.coll Added Parameters(inputid,constrained)
    1.5 10/22/2009 rberinger Added additional parameters (see Arguments Section Below).  Added the ability to perform a default site search when the ENTER key is pressed, in the event that the max parameter hides our desired content.
    1.1 10/20/2009 rberinger Made site in-specific and converted from HTML Source to DekiBlock added and max results parameter
    1.0 10/20/2009 Howleyda Initial Release (here)

    Requirements

    • This template requires MindTouch version: MindTouch 9.02 or greater
    • DekiApi extension
    • jQuery Extension

    How do I install it?

    These instructions will help you install the template'SearchSuggestion' on your wiki quickly and correctly.

    Perform the following steps:

    1. Copy the template source code:
      1. If the code in the "HTML Source" area below is not already visible and selected, click the "View" button to display and select it.
      2. Copy it to your clipboard (CTRL-C).
    2. Create a new template on your wiki:
      1. Select the Tools->Templates menu item on your wiki
      2. Click "New Page" button. This will open the editor on a new page with the title "Template:Page Title".
      3. Replace "Page Title" with the name of the template.
    3. Paste in the source code:
      1. In the editor, click the "View->Source" menu item to view the HTML source of the page.
      2. Select all text (CTRL-A).
      3. Paste in the template source code from the clipboard (CTRL-V).
    4. Click "Save" button.

    HTML Source for Template:SearchSuggestion

    A quick note about the examples on this page

    For all the examples on this page, the code is shown before the working example.  The code is shown with the syntax extension, and looks like this:

    SearchSuggestion()

    This means that the actual code on the page should be enclosed in a DekiScript block.  If you want to copy the code from this page, then use the same procedure as described in steps 2-4 above. 

    How do I use it?

     Where ever you would like to add some search functionality just insert this template as described in the example below.  As the user starts typing and after a slight pause the suggested results will show.  If you do not find your desired results just hit the ENTER key and a default search will be performed.  NOTICE:  After the ENTER key is pressed the search will default to a all inclusive search and will still respect the path parameter if one was passed to the template.

    PLEASE NOTE: Only 1 search box can be used on any single page at one time.

    Arguments

    <meta charset="utf-8"/> (optional) Contraint to apply on the search suggestion
    Name
    Type
    Default
    Description
     max  num  10  (optional) Max number of results to return with each search
     path str  N/A  (optional) Limits the search to a particular path (To search the subpages of desired path end with /*)
    example: path:user:Anonymous/* will search all the pages under and including your user page.
     sortby str score (optional) specify which field to use to sort results by (one of "score", "title", "date", "size", "wordcount"; use "-title" for reverse order; default: "score") NOTE: Currently not working hope to fix in ver. 2.0
     showpages bool true (optional) Show pages that match search criteria
     showfiles bool true (optional) Show files that match search critera
     showcomments bool true (optional) Show comments that match search criteria
    inputid str search-input (optional) The Search Box Input ID , by default "search-input"
    constrained str N/A  

    Examples

     In A DekiScript Block or wrapped in double curly braces {{ }}: The below example will search the entire site and limit the results to our top 15 results.

    SearchSuggestion{max:15};
    
    x
    pages
    files
    comments

     

    In a DekiScript Block or wrapped in double curly braces {{ }}:  The below example will limit the search to the Dekiscript/FAQ page and subpages (must end in /* for subpages to be searched)

    SearchSuggestion{path:'DekiScript/FAQ/*'};

    Since only 1 search box can be on any single page at 1 time the working example of above is not included.

     

     In a DekiScript Block or wrapped in double curly braces {{ }}:  The below example inserts the template with all default parameters.

    {{ SearchSuggestion(); }}
    

    Since only 1 search box can be on any single page at 1 time the working example of above is not included.

    Reference(s)

    Refer to the Announcement forum thread for questions and/or comments

    See this page for a full details of how this was created from it original author: Howleyda.

    Credits/Special Thanks

    Howleyda for the initial creation of this template and allowing me to extend upon it.

    Template/Extension Source Code

    // PURPOSE:  Insert a search box that will help narrow down search results
    // WRITTEN BY:  Original creator: Howleyah 
    //              Edited by: rberinger
    //              Edited by: carles.coll
    // VERSION: 1.5
    // VERSION HISTORY: 1.0 - Initial creation
    //                  1.0 - Reworked to be non-site specific and allow for a max results parameter
    //                  1.5 - Added Parameters(path, showpages, showfiles, showcomments, sortby)
    //                        Added ability to press ENTER to perform default wiki search within Path contraints.
    //                  1.6 - Added Parameters(inputid,constrained)
    //         
    // PARAMETERS:  max - Limits Ajax search results to this number (default = 10)
    //              path - Limits Ajax search to a particular path (default = entire site)
    //                     NOTE:  if a search of subpages is desired you must end the path with /*
    //              showpages - Will Show or Hide Page results (default = true)
    //              showfiles - Will Show or Hide Files results (default = true)
    //              showcomments - Will Show or Hide Comment results (default = true)
    //              sortby - Currently not working (Default sort is by score only at this time)
    //              inputid - The Search Box Input ID , by default "search-input"
    //              constrained - Contraint to apply on the search suggestion
    
    // KNOWN BUGS:  Sortby parameter currently has no effect
    
    // USE:  Must be used within a DekiScript block or wrapped in double curly braces
    //       EXAMPLE:  {{ SearchSuggestion{max: 15, path:'path/to/some/page/*'}
    // If the dynamic results are not what you were searching for you can just hit the ENTER key
    // and a default search will be performed limited within the path constraint if one was desired.
    // When ENTER key is applied only the query text and path arguments will be honored.
    
    
    // USER PARAMETERS
    var maxresults = ($max ?? $0 ?? 10);
    var searchpath = ($path ?? $1 ?? '');
    var sortorder = ($sortby ?? $2 ?? '');
    var showpages = ($showpages ?? $3 ?? true);
    var showfiles = ($showfiles ?? $4 ?? true);
    var showcmts = ($showcomments ?? $5 ?? true);
    var inputid = ($inputid?? $6 ?? "search-input");
    var constrained = ($constrained ?? $7 ?? '');
    
    // Account for user folders
    var searchpath = string.replace(string.tolower(searchpath),'user:','User\\:');
    
    // Make this template site in-specific
    var siteAPI = site.api;
    var siteURI = site.uri;
    
    <input type="text" id=(inputid) />
    <div id="search-suggest"><a class="close" href="#">'x'</a>
    <div class="search-pages"><strong>'pages'</strong></div>
    <div class="search-files"><strong>'files'</strong></div>
    <div class="search-comments"><strong>'comments'</strong></div>
    </div>
    
    
    <div><style type="text/css">'
    #search-suggest strong{
    	margin:0 0 10px 0;
    	color:#999;
    	display:block;
    }
    #search-suggest {
    	border:1px solid #ccc;
    	display:none;
    	padding:0;
    	position:absolute;
    	overflow:hidden;
    	background:#fff;
    	font-size:11px;
    	min-width:150px;
    	z-index:50;
    }
    #search-suggest .search-pages{
    	float:left;
    	padding:5px 20px;
    	max-width:300px;
    }
    #search-suggest .search-files{
    	float:left;
    	padding:5px 20px;
    	max-width:200px;
    }
    #search-suggest .search-comments{
    	float:left;
    	padding:5px 20px;
    	max-width:200px;
    }
    #search-suggest a {
    	display:block;
    	padding:0 0 0 20px;
    	height:16px;
    	overflow:hidden;
    	margin:0 0 10px 0;
    	background-image:url(/skins/common/icons/icons.gif);
    	background-repeat:no-repeat;
    }
    #search-suggest .search-pages a {
    	background-position:0 -768px;
    	margin:0;
    	
    }
    #search-suggest .search-files a {
    	background-position:0 -1040px;
    }
    #search-suggest .search-comments .comment-user {
    	display:block;
    	font-weight:bold;
    	margin-bottom:0px;
    	padding:0;
    }
    #search-suggest .search-comments .comment-text {
    	display:block;
    	font-weight:100;
    	margin-bottom:10px;
    	color:#999;
    }
    #search-suggest .search-pages .page-path {
    	display:block;
    	color:#999;
    	font-size:9px;
    	margin:0 0 10px 0;
    }
    .no-show {
    	display:none;
    }
    #search-suggest a.close{
    	float:right;
    	display:block;
    	font-size:14px;
    	width:15px;
    	padding:0 0 1px 0;
    	line-height:1;
    	-moz-border-radius:7px;
    	border:2px solid #fff;
    	font-weight:bold;
    	text-align:center;
    	background:#ccc;
    	color:#fff;
    	text-decoration:none;
    }
    #search-suggest .close:hover{
    	background:#9f1313;
    	text-decoration:none;
    	color:#fff;
    }
    '</style>
    </div>
    
    <script type="text/javascript">'
    	$("body").ready( function() {
    		$("#search-suggest a.close").click( function() {
    			$("#search-suggest").hide();
    			return false;
    		});
    	
    		$("#'..inputid..'").keyup( function(e) {
    
    			if ($(this).val()==\'\'){
    				$("#search-suggest").hide();
    				return false;
    			} 
    	
    			var q = $(this).val();
    
                            // Get the Code the the key that was press (we are looking for the enter key)
                            var code = (e.keycode ? e.keycode : e.which);
    
                            // Was Enter Pressed?
                            if(code == 13) {
                                // Listen for an Enter KeyPress just incase our reduced results dont return
                                // The stuff our user is looking for.  NOTE: This defaults to an all site search.
    
                                var searchpath='..json.emit(searchpath)..';
                                var constrained ='..json.emit(constrained)..';
                                var sortorder='..json.emit(sortorder)..';
        
                                // Lets build the Query string with our parameters
                                var qrystr = "";
            
                                searchpath = searchpath.replace("\\:","\:");
    
                                if(searchpath != "") { qrystr += "+AND+path%3A" + searchpath }
                                if(constrained != "") { qrystr += "+AND+" + constrained }                                
                                qrystr = Deki.url.encode(q) + qrystr;
    
                                var SiteURI = '..json.emit(siteURI)..';
                                var SearchURI = SiteURI + "Special:Search?search=" + qrystr + "&type=fulltext&go=Find&ns=all";
                                location.href = SearchURI;
                                return false;
                            }
    			
                        clearTimeout($.data(this, "timer"));
    		    var ms = 400; //milliseconds
    		    var wait = setTimeout(function() {
    		      loadsearch(q);
    		    }, ms);
    	    
    	
    			$.data(this, "timer", wait);
    		});
    	});
    	
    function loadsearch(q) {
        var mySite='..json.emit(siteAPI)..';
    
        var maxreturns='..json.emit(maxresults)..';
        var searchpath='..json.emit(searchpath)..';
        var constrained='..json.emit(constrained)..';
        var sortorder='..json.emit(sortorder)..';
        var showpages='..json.emit(showpages)..';
        var showfiles='..json.emit(showfiles)..';
        var showcmts='..json.emit(showcmts)..';
    
        // Lets build the Query string with our parameters
        var qrystr = "";
    
        if(searchpath != "") { qrystr += " AND path:" + searchpath; }
        qrystr = Deki.url.encode(q + "*" + qrystr);
        if(constrained != "") { qrystr += " AND (" + constrained + ")"; }
        if(sortorder != "") { qrystr += "&sortby:" + sortorder }
    
        var qurl=mySite+"/site/search?limit="+maxreturns+"&q="+qrystr;
    
    	var cntpage=0;
    	var cntfile=0;
    	var cntcomments=0;
    	jQuery.get(qurl, function(xml) {
    		$("#search-suggest .search-pages a").remove();
    		$("#search-suggest .search-pages span").remove();
    		$("#search-suggest .search-files a").remove();
    		$("#search-suggest .search-comments a").remove();
    		$("#search-suggest .search-comments span").remove();
    	
                    if(showpages) {
    		$(xml).find("search > page").each(function(){
    			var qpath = $(this).find(" > path").text();
    			var qtitle = $(this).find("> title").text();
    		$("#search-suggest .search-pages ").append(\'<a class="page" href="/\' + qpath + \'">\' + qtitle.substr(0,30) + \'</a><span class="page-path">\' + qpath + \'</span>\');
    			cntpage++;
    		});
                    } else {
                        cntpage = 0;
                    }
    
                    if(showfiles) {		
    		$(xml).find("search > file").each(function(){
    			var quri = $(this).find(" > contents").attr("href");
    			var qtitle = $(this).find(" > filename").text();
    			$("#search-suggest .search-files").append(\'<a class="file" href="\' + quri + \'">\' + qtitle + \'</a>\');
    			cntfile++;
    		});
                    } else {
                        cntfile = 0;
                    }
    		
                    if(showcmts) {
    		$(xml).find("search > comment").each(function(){
    			var quser = $(this).find("username").text();
    			var qcomment = $(this).find(" > content").text();
    			var quri = $(this).find("path").text();
    			$("#search-suggest .search-comments").append(\'<a href="/\' + quri + \'" class="comment-user">\' + quser + \'</a><span class="comment-text">\' + qcomment.substr(0,40) + \'...</span>\');
    			cntcomments++;
    		});
                    } else {
                        cntcomments = 0;
                    }
    		
    		if(cntpage>0){
    			$("#search-suggest .search-pages").show();
    		} else {
    			$("#search-suggest .search-pages").hide();
    		}
    		
    		if(cntfile>0){
    			$("#search-suggest .search-files").show();
    		} else {
    			$("#search-suggest .search-files").hide();
    		}
    		
    		if(cntcomments>0){
    			$("#search-suggest .search-comments").show();
    		} else {
    			$("#search-suggest .search-comments").hide();
    		}
    		
    		if(cntpage>0 || cntfile>0 || cntcomments>0){
    			$("#search-suggest").show();
    		} else {
    			$("#search-suggest").hide();
    		}
    	});
    }
    '</script>
    
    

     

    Disclaimers

    None.

    Was this page helpful?
    Tag page

    Files 3

    FileVersionSizeModified 
    Viewing 2 of 2 comments: view all
    I've tried this and it isn't working. I started by creating a new user with unsafe content, the logged of as myself and signed in as the unsafe user. I created a template using the name suggested. I copied the code as suggested and pasted it into a Dekiscript block. There are no extra spaces at the end. When I save it, the code is still visible, see graphics for: template_searchsug 1 and 2. When I insert and save the template I get the field and a large block of text from the code, but nothing works, see graphic for: template_searchsug 3. So, what's going wrong?
    Posted 06:05, 5 Mar 2010
    If a path contains a "&", the clicked link will go to an edit instead of the page itself.

    I had some documents that were under a section called "Operations & Maintenance Guide". Upon clicking the suggestion, I would get an edit page with the title "Operations"
    Posted 06:48, 10 May 2011
    Viewing 2 of 2 comments: view all
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by