3

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.

Carey Estes
  • 1,534
  • 2
  • 31
  • 59

1 Answers1

0

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;
  }
});
Xogle
  • 363
  • 3
  • 16
  • ok, so if dupDtector is out for local fetches, does this mean my only option is to edit my local database? Is there any way to parse the Bloodhound return object before it makes it to the result set? I could write a loop to remove duplicates I guess. – Carey Estes Nov 09 '15 at 21:06
  • Why don't you just "prefetch" it "locally" in a JSON file instead. It theoretically should work. – Xogle Nov 09 '15 at 21:24
  • Well, I was originally doing that, but I decided to swap to a local fetch, since I need to build and run file_put_contents, when the db is queried and run. I'll give it a try. – Carey Estes Nov 09 '15 at 22:15