3

I am trying to implement something similar to the stackoverflow tag suggestion input field. In the database, I have two tables: one is for tags and another is for tagaliases or tag synonyms (tagSynonyms).

When a user types a term, the search engine should be smart enough to detect the aliases of the typed term, and display only the main tag corresponding to the typed term. What is the best way for implementing this with bloodhound ?

Seme1
  • 133
  • 3
  • 10

1 Answers1

0

Just send the typed term to the server and execute a sql-statement similar to this one:

select name from tag t left outer join tagSyms s on s.tagID=t.tagID where s.alias=$searchstring OR t.tagName=$searchstring

and return json formatted data. JSON-Format: { val: 'tagname' }

On Client site:

var engine = new Bloodhound({
  name: 'tags',
  remote: 'http://example.com/tags?q=%QUERY',
  datumTokenizer: function(d) {
    return Bloodhound.tokenizers.whitespace(d.val);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace
});
engine.initialize();

$("#inputField").typeahead(...);

Also take a look at the displayKey and template keys in the typeahead.js

When you want a sorted list, you will be fine if the sql is ordered.

You control what in the suggestion box gets displayed with the templates:

For example if you want to display the counter too, just edit the JSON to { val: 'tagname', counter:x } and the typeahead to:

.typeahead(
        ....,{
        templates:{
            suggestion:function(data){
               return "<p>"+data.val+"(used: "+data.counter+" times)</p>";
            }
        },
        source: engine.ttAdapter()

});

anstue
  • 990
  • 12
  • 24
  • Thanks a lot for the response. The SQL query at the server side is expected to return a sorted tag list based on their use frequency. In other words, along with the name of each tag I also need to pass in a "counter". The displayed suggestions should display the possible tags in an decreasing order based on the "counter" value associated with the name of each tag. Is it possible to have this implemented with bloodhound ? – Seme1 Sep 28 '14 at 14:17
  • Some of the synonyms I have are totally different terms from the query. How can I tell bloodhound that they are the same ? for example, when a user enters cyan, I have "cyan" saved in the database as a synonym for "blue". In the suggestions list, I would like "blue" to be displayed instead of "cyan" even though the user typed cyan. – Seme1 Sep 28 '14 at 15:37