Skip to content

Search engine suggestions with DataparkSearch

The possibility to implement search engine suggestion has appeared in DataparkSearch Engine back in 2009. To do that a new command MakePrefixes has been implemented. This command makes automatically all prefixes for every word indexed. To enable this feature, add the following command into your configuration file:

MakePrefixes yes

Due to this feature a separate search base (which has its own DBAddr command) is required for search engine suggestions implementation. All other in configuration is standard. See an example in qsimilar.conf file in doc/samples directory inside DataparkSearch Engine distribution.

The next step is to define the sources where the search suggestions come from. The first option is to create a special table in the SQL database which holds manually selected set of search suggestions. In the example mentioned above such table has the name "hint" with the following structure: rec_id - an unique identifier of type serial; qwords of type text; the later one contain a search suggestion on every row.

Corresponding commands in qsimilar.conf file are:

HTDBAddr pgsql://user@host.ext/search/
HTDBList "SELECT 'htdb:/hint/'||rec_id FROM hint"
HTDBText query "SELECT qwords FROM hint WHERE rec_id=$2"
HTDBText mode "SELECT 'all'"
HTDBText Pop_Rank "SELECT '0.999999'"
Server path htdb:/hint/

The value of section "query" is that search suggestion will be shown to a user; the value of "mode" holds the DataparkSearch mode to be used to perform this query; the "Pop_Rank" section set the priority for suggestions from this source under other equal.

The next source of search suggestions could be the query logging table "qtrack" which is filled with users query parameters if corresponding DBAddr command has the "trackquery" option specified. The obvious drawback for this source is that users can issue totally any queries and not all of them would be ethical/wise to show to other users. There is no any way to censor these records with DataparkSearch Engine. The commands for this source in qsimilar.conf are:

HTDBAddr pgsql://user@host.ext/search/
HTDBList "SELECT 'htdb:/ru/'||MIN(rec_id) FROM qtrack t, qinfo q WHERE q.q_id=t.rec_id AND'tmplt' AND q.value!='suggest.htm' AND found > 0 AND length(qwords) < 128 AND length(qwords) > 2 AND qtime > now()::abstime::int - 40*24*3600 GROUP BY qwords"
HTDBText query "SELECT qwords FROM qtrack WHERE rec_id=$2"
HTDBText mode "SELECT value FROM qinfo WHERE q_id=$2 AND name='m'"
HTDBText Pop_Rank "SELECT '0.75'"
Server path htdb:/ru/

Finally, if you have a directory of web sites you could use site titles from it as the search suggestions.

HTDBAddr pgsql://user@host.ext/com/
HTDBList "SELECT 'htdb:/dir_ru/'||id FROM url_desc WHERE lang='ru' ORDER BY id"
HTDBText query "SELECT url_desc FROM url_desc WHERE id=$2 AND lang='ru'"
HTDBText mode "SELECT 'all'"
HTDBText Pop_Rank "SELECT '0.65'"
RemoteCharset koi8-r
Server path htdb:/dir_ru/

After you specify all of your sources of search suggestions you can run indexer to make the base ready:

./indexer -TW /usr/local/dpsearch/etc/qsimilar.conf

The AJAX technology is used to implement search suggestions on search result pages. I used to use slightly customised htmlhttprequest.js in the first version (you can still find it in doc/samples directory), but now I use YUI library from Yahoo! which is easier to use.

The first step in search template modification is to include YUI library:

<script src=""></script>

It's loaded in the "bottom" section of janus.htm.en, though you can move in to the section "top" if find it suitable.

Then you need to enclose the search query input field into a special div to specify for YUI where its magic should work:

<div id="demo" class="yui3-skin-sam">
  <input class="inputsearch" type="text" name="q" id="q" size="60" value="$&(q)" style="width:100%;" autocomplete="off">

The last modification in the search template put the YUI calls which make the magic:

<script type="text/javascript">
    YUI().use('autocomplete', 'autocomplete-filters', 'autocomplete-highlighters', 'node-load', function (Y) {'#q').plug(Y.Plugin.AutoComplete, {
    resultFilters    : 'phraseMatch',
    resultHighlighter: 'phraseMatch',
    source           : '/cgi-bin/search.cgi?q={query}&m=any&sp=1&sy=0&GroupBySite=yes&s=IRPD&ps=10&tmplt=yui.htm&label=qsimilar&callback={callback}'

And in the last, the thing you will also need is the yui.htm template which produce search results in JSON format used by YUI library as its source of search suggestion for user input.

That's all.

Leave a Reply

Your email address will not be published.