Typeahead.js: The suggestion engine of bloodhound

Created on 22 Sep 2016  路  3Comments  路  Source: twitter/typeahead.js

If you in the typeahead.js examples type in "aro" (to find for example north carolina) the basic suggestion engine finds it, but the bloodhound suggestion does not. Is this the wanted behaviour or is it an error in the implementation of the bloodhound suggestion engine?

Most helpful comment

The "aro" search only works in the first example because it has a substringMatcher function whicih performs regex matches. It got me very confused too, because there's no bloodhound example with that behaviour. Fortunately someone already posted a solution https://github.com/twitter/typeahead.js/issues/96

So in the second example (states with bloodhound) you'd create a custom datumTokenizer function (such as below). It does match strings such as "aro" for Carolina.

datumTokenizer: function(d) { var test = Bloodhound.tokenizers.whitespace(d); $.each(test,function(k,v){ i = 0; while( (i+1) < v.length ){ test.push(v.substr(i,v.length)); i++; } }) return test; },

All 3 comments

The "aro" search only works in the first example because it has a substringMatcher function whicih performs regex matches. It got me very confused too, because there's no bloodhound example with that behaviour. Fortunately someone already posted a solution https://github.com/twitter/typeahead.js/issues/96

So in the second example (states with bloodhound) you'd create a custom datumTokenizer function (such as below). It does match strings such as "aro" for Carolina.

datumTokenizer: function(d) { var test = Bloodhound.tokenizers.whitespace(d); $.each(test,function(k,v){ i = 0; while( (i+1) < v.length ){ test.push(v.substr(i,v.length)); i++; } }) return test; },

sorry for irrelevent comment, but how do you use this bloodhound and typeahead. I have been beating my head for last two days but can't figure out how to use it.

P.S I am need to coding

Thanx @claudio5678 ! This custom function you've posted saved me hours. I believe this piece of code could be optimized, e.g.:

datumTokenizer : function (d) {
    var test = Bloodhound.tokenizers.whitespace(d);
    $.each(test, function (k, v) {
        let i = 1; // start with 1 insted of 0 because test already contains 1st value
        while (i < v.length - 1) {
            test.push(v.substr(i, v.length));
            i++;
        }
        $.unique(test); // removes duplicate values
    });
    return test;
},
Was this page helpful?
0 / 5 - 0 ratings