mongodb - How to replace prefetch source in Bloodhound with a 'real time' alternative? -


i've been using bloodhound prefetch [docs] option defined.

this works fine, except when add content json file being prefetched, not available search result unless restart browser.

so trying make search results reflect updated file content in 'real time'.

i tried replacing prefetch remote causes search functionality not work intended (it shows non-matched results).

below code using prefetch.

version info: typeahead.bundle.min.js @ v0.10.5.

function searchfunction() {     var template =         "<p class=\"class_one\">{{area}}</p><p class=\"class_two\">{{title}}</p><p class=\"class_three\">{{description}}</p>";     var compiled_template = hogan.compile(template);     var datasource = new bloodhound({         datumtokenizer: function(d) {             return bloodhound.tokenizers.whitespace(d.tokens.join(                 ' '));         },         querytokenizer: bloodhound.tokenizers.whitespace,         prefetch: '/static/my_file.json'         # remote: '/search'     });     datasource.initialize();     $('.my_lookup .typeahead').typeahead({}, {         source: datasource.ttadapter(),         name: 'courses',         displaykey: 'title',         templates: {             suggestion: compiled_template.render.bind(                 compiled_template)         }     }).focus().on('typeahead:selected', function(event, selection) {         var title = selection.title             // things title variable     }); } 

edit:

i started thinking perhaps need server side logic perform search on database contains content within local json file.

using code posted below, following works:

  • searches database in real time.
  • all matches returned.

the following not work:

  • it not offer suggestions, have type full token name.
  • if searching apple, search after typing a, p etc, if doesn't results, shows error in firebug: typeerror: data null. after few of these errors, stops triggering searches , no error displayed.

and, results database in following format, , don't know how apply hogan template suggestions each result:

{     "matches": [{         "tokens": ["apple", "orange"],         "area": "nautical",         "_id": {             "$oid": "4793765242f9d1337be3d538"         },         "title": "boats",         "description": "here description"     }, {         "tokens": ["apple", "pineapple"],         "area": "aviation",         "_id": {             "$oid": "4793765242f9d1337be3d539"         },         "title": "planes",         "description": "here description."     }] } 

js

function searchfunction() {  var engine = new bloodhound({     remote: {         url: '/search?q=%query%',         wildcard: '%query%'     },   datumtokenizer: bloodhound.tokenizers.whitespace('q'),   querytokenizer: bloodhound.tokenizers.whitespace,  });  engine.initialize();  $('.my_lookup .typeahead').typeahead({ }, { source: engine.ttadapter(), name: 'courses', displaykey: 'title', templates: { suggestion: function (data) {                 return "// not sure how apply markup each match"       } } }).focus().on('typeahead:selected', function(event, selection) {  console.log(selection); var title = "// again not sure how access individual match data" // things title variable }); } 

mongodb schema

database: courses
collection: courses
documents:

{     "_id" : objectid("4793765242f9d1337be3d538"),     "tokens" : [          "apple",          "orange"     ],     "area" : "nautical",     "title" : "boats",     "description" : "here description." } 

and:

{     "_id" : objectid("4793765242f9d1337be3d539"),     "tokens" : [          "apple",          "pineapple"     ],     "area" : "aviation",     "title" : "planes",     "description" : "here description." } 

etc

python (using bottle routes)

@route('/search') def search():     """     query courses database matches in tokens field.         """     # query     query  = request.get.q     # define database     dbname = 'courses'     db = connection[dbname]     # define collection     collection = db.courses     # make query     matches = collection.find({"tokens":query})     # send results     results = {}     results['matches'] = matches     response.content_type = 'application/json'     return dumps(results) 


Comments