Mongodb 如何用';实时';可供替代的
我一直在使用定义了Mongodb 如何用';实时';可供替代的,mongodb,python-2.7,typeahead.js,bloodhound,hogan.js,Mongodb,Python 2.7,Typeahead.js,Bloodhound,Hogan.js,我一直在使用定义了prefetch[]选项的侦探犬 这很好,除非我将内容添加到正在预取的json文件中,否则它不能作为搜索结果使用,除非我重新启动浏览器 所以我试图让搜索结果“实时”反映更新的文件内容 我试图简单地用remote替换prefetch,但这会导致搜索功能无法正常工作(它显示不匹配的结果) 下面是我与预取一起使用的代码 版本信息:typeahead.bundle.min.jsatv0.10.5 function searchFunction() { var template
prefetch
[]选项的侦探犬
这很好,除非我将内容添加到正在预取的json文件中,否则它不能作为搜索结果使用,除非我重新启动浏览器
所以我试图让搜索结果“实时”反映更新的文件内容
我试图简单地用remote
替换prefetch
,但这会导致搜索功能无法正常工作(它显示不匹配的结果)
下面是我与预取一起使用的代码
版本信息:typeahead.bundle.min.js
atv0.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
// do things with the title variable
});
}
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 to apply markup to each match"
}
}
}).focus().on('typeahead:selected', function(event, selection) {
console.log(selection);
var title = "// again not sure how to access individual match data"
// do things with the title variable
});
}
MongoDB模式
数据库:课程
收藏:课程
文件:
{
"_id" : ObjectId("4793765242f9d1337be3d538"),
"tokens" : [
"apple",
"orange"
],
"area" : "Nautical",
"title" : "Boats",
"description" : "Here is a description."
}
以及:
{
"_id" : ObjectId("4793765242f9d1337be3d539"),
"tokens" : [
"apple",
"pineapple"
],
"area" : "Aviation",
"title" : "Planes",
"description" : "Here is a description."
}
等
Python(使用路由)
{
"_id" : ObjectId("4793765242f9d1337be3d539"),
"tokens" : [
"apple",
"pineapple"
],
"area" : "Aviation",
"title" : "Planes",
"description" : "Here is a description."
}
@route('/search')
def search():
"""
Query courses database for matches in tokens field.
"""
# get the query
query = request.GET.q
# define the database
dbname = 'courses'
db = connection[dbname]
# define the collection
collection = db.courses
# make the query
matches = collection.find({"tokens":query})
# send back results
results = {}
results['matches'] = matches
response.content_type = 'application/json'
return dumps(results)