Execute query suggestion upon selection (avoid having to hit search button)

1 view
Skip to first unread message

ianr

unread,
Jun 3, 2013, 1:08:24 PM6/3/13
to Google-Search-...@googlegroups.com
Is it possible to modify a frontend's xslt code to execute a search when a query suggestion is selected?

The default behavior is select a query suggestion and then click on the search button, but I would like to execute the search and avoid having to hit the search button.

Best wishes,

Ian.

Mathias Bierl

unread,
Jun 3, 2013, 2:11:17 PM6/3/13
to Google-Search-...@googlegroups.com
You just have to change the event handler onclick for the query suggestions.
In addition to write it to the search field you can also click the search button.
This would be the easiest way.

If you want it smoother then you can do the complete search in background and write the results to a result container div.

ianr

unread,
Jun 3, 2013, 3:35:55 PM6/3/13
to Google-Search-...@googlegroups.com
Hi Mathias,

Thanks for the quick reply.

I believe this is the section of code I need to modify,

 <xsl:when test="$show_suggest = '1' and (($type = 'home') or ($type = 'std_top'))">
  <xsl:text disable-output-escaping="yes">&lt;form id="suggestion_form" name="gs" method="GET" action="search" onsubmit="return (this.q.value == '') ? false : true;"&gt;</xsl:text>
  </xsl:when>

So, 

onsubmit="return (this.q.value == '') ? false : true;"    //does this write the query suggestion to the search box when I click on a query suggestion?

Could you please explain how to modify this to write the suggested terms to the search box and execute the search onclick event?

Best wishes,

Ian.

Mathias Bierl

unread,
Jun 3, 2013, 4:20:53 PM6/3/13
to Google-Search-...@googlegroups.com
No the code is in the ss.js file on the GSA.
You have to download this put it on an other server and adjust it there

ianr

unread,
Jun 3, 2013, 7:04:44 PM6/3/13
to Google-Search-...@googlegroups.com
Hi Mathias,

I have downloaded ss.js and I believe I just about have it, but I have a couple questions :)

Here is the relevant code in ss.js with my changes highlighted,

function ss_handleMouseC() {
  var fo = document.getElementById(ss_form_element);
  var tbl = document.getElementById(ss_popup_element);
  var rows = tbl.getElementsByTagName('tr');
  for (var ri = 0; ri < rows.length - 1; ri++) {
    if (rows[ri] == this) {
      // Back up the original query if not already, and adjust the reference
      // index.
      if (!ss_qbackup) {
        ss_qbackup = fo.q.value;
      }
      ss_loc = ri;
      // Find out what type of suggestion it is.
      var suggestion = ss_locateSuggestion(ss_qbackup, ss_loc);
      // Adjust the query in the search box.
      if (suggestion.q) {
        fo.q.value = suggestion.q;
        fo.submit();
//execute search
var searchButton = document.getElementById("???");
searchButton.click();
      } else {
        fo.q.value = ss_qbackup;
        if (suggestion.u) {
          window.location.href = suggestion.u;
        }
      }
      break;
    }
  }


Am I on the right track? 

How do I get access to the search button so I can execute a search? I see there is an element with name btnG in the frontend xslt, is this the search button?

Also at the top of ss.js in the comments they mention "see stylesheet_template.enterprise for detailed descriptions of these variables." I can't seem to find this list, do you know where it can be accessed?

Best wishes and thanks again for your assistance,

Ian.

Jeremy Garreau

unread,
Jun 4, 2013, 3:06:07 AM6/4/13
to Google-Search-...@googlegroups.com
These lines :

fo.q.value = suggestion.q;
fo.submit();

already submit the form when you click a suggestions. 

ianr

unread,
Jun 4, 2013, 3:17:57 AM6/4/13
to Google-Search-...@googlegroups.com
Hi Jeremy,

The default ss.js does not execute a search when I select a query suggestion. Does it behave differently for you?

Best wishes,

Ian.

Mathias Bierl

unread,
Jun 4, 2013, 5:00:53 AM6/4/13
to Google-Search-...@googlegroups.com
Hello

I also tested it on our GSA.
When I select a query suggestion then the search is executed automatically.

If it doesn't do it on your GSA, maybe you don't get query suggestions but only browser field history. And in this case the search is never triggered automatically.

Can you attach a screenshot of your query suggestions box ?

ianr

unread,
Jun 4, 2013, 4:06:19 PM6/4/13
to Google-Search-...@googlegroups.com
Hi Mathias & Jeremy,

After some digging around it seems the query suggestions are not implemented out-of-the-box. I have found the javascript responsible for providing query suggestions and added some code to hopefully execute the search upon selecting a query suggestion. Here it is (my changes highlighted). I can't test the changes now, but if you see a glaring mistake please let me know. Thanks again for your assistance!

SearchBox = function () {
    this.timeDelay = 100;

    var _self = this;

    this.Initialize = function () {
        var searchBox = $("#SearchArea");

        // Set up autocomplete
        searchBox.focus(function () {
            searchBox.autocomplete({
                source: function (request, response) {

                    var searchText = $("#SearchArea").val();

                    $.ajax({
                        url: "/GSASuggest.ashx",
                        contentType: "application/json",
                        dataType: "json",
                        data: "q=" + searchText,
                        timeout: 500,
                        success:
           function (data) {
               response($.map(data, function (item) {
                   return {
                       label: item,
                       value: item
                   }
               }));
           }
                    });

                },
                delay: _self.timeDelay,
                minLength: 1,
select: function(event, ui) {
if(ui.item){
searchBox.value(ui.item.value);
}
searchBox.submit();
}
            });
        });

        // Make search input the focus on the homepage on load.
        // Also triggers the attachment of the autocomplete plugin for the first time.
        if (!$("#first-time-callout").length) {
            $("body.homepage #SearchArea").focus();
        }
    }
}

Mathias Bierl

unread,
Jun 4, 2013, 4:09:01 PM6/4/13
to Google-Search-...@googlegroups.com
The query suggestions are implemented out of the box, if you has enabled them using the XSLT variable at the beginning of the XSLT file

ianr

unread,
Jun 4, 2013, 4:49:53 PM6/4/13
to Google-Search-...@googlegroups.com
Hi Mathias,

I understand suggestions work out-of-the-box when using the xslt. I just came to know that this particular ui is using Google Search Box for SharePoint which supposedly does not support the suggest service. A workaround was used via a proxy to call the suggest service and return the suggestions.

I have successfully achieved the functionality I desired (thank you Mathias and Jeremy for pointing me in the right direction). 

Is there a better solution to integrating GSA search in Sharepoint than using the Google Search Box for SharePoint? 

Best regards,

Ian.
Message has been deleted

Dave Watts

unread,
Jun 5, 2013, 11:32:44 PM6/5/13
to Google-Search-...@googlegroups.com
> Is there a better solution to integrating GSA search in Sharepoint than
> using the Google Search Box for SharePoint?

Not to my knowledge. But it's just a SharePoint web part, and should
be easily customizable.

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on
GSA Schedule, and provides the highest caliber vendor-authorized
instruction at our training centers, online, or onsite.

Jeremy Garreau

unread,
Jun 6, 2013, 2:37:22 AM6/6/13
to Google-Search-...@googlegroups.com
Why my message always get deleted damnit !

You should have tell us you were using the search box. Suggestions works only on the FE view.

It's easily doable using the existing GSAForward.aspx page by adding a few js line in the SearchArea control template.

And no, the search box is not a webpart by definition.

ianr

unread,
Jun 7, 2013, 10:59:08 AM6/7/13
to Google-Search-...@googlegroups.com
I was unaware the solution was using the search box initially, I apologize. It was fairly easy to get the desired functionality with your suggestion.

Thank you!

Ian.
Reply all
Reply to author
Forward
0 new messages