Search code examples
typeahead.jstwitter-typeaheadbloodhound

Typeahead/Bloodhound: Removing duplicates from local


I am unclear on how to remove duplicates using the dupDetector parameter in Bloodhound.

I am using v. 0.11.1

Pulling the dataset from a database with records like this:

building_name  room  department
Rooney         123   English
Rooney         456   Chemistry
Rooney         987   Chemistry
Meyer          65    Dog Walking
Flatiron       498   Weaving

My Bloodhound call:

var buildingName = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name', 'room', 'department'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      local: buildingJson,
      dupDetector: function(remoteMatch, localMatch) {
        return remoteMatch.building_name === localMatch.building_name;
      }
});

The functionality I am looking for is the ability to search Rooney, 456, or English and the result set only show a single building_name, since that building name is the same for all three results. Currently, it is returning all three records in the list.

Is that possible?

All info I could find about dupDetector was comparing a remote and prefetch. I'm only using a single data source, it just has multiple records with the same name.


Solution

  • Unfortunately, dupDetector only works with a remote or prefetched datasource.

    There are issues with a local datasource, hence why your dupDetector is not working. It is never being called. Try using console.log to see this.

    https://github.com/twitter/typeahead.js/issues/606#issuecomment-51221195

    Looks like you're loading from a JSON anyway, so why not just pop it on your "local" server and "prefetch" it instead. Otherwise it looks like you will have to do it manually.

    var buildingName = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name', 'room', 'department'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch: ../buildingJson.json,
        dupDetector: function(remoteMatch, localMatch) {
            return remoteMatch.building_name === localMatch.building_name;
      }
    });