1
:
var bodyparser = require('body-parser');
var express = require('express');
var status = require('http-status');
var _ = require('underscore');
var mongoose = require('mongoose');
var productSchema = require('./product');
var schema = new mongoose.Schema(productSchema);
schema.index({ name: 'text' });
module.exports = function(wagner) {
var api = express.Router();
api.use(bodyparser.json());
api.get('/product/text/:query', wagner.invoke(function(Product) {
return function(req, res) {
console.log("we are in the get " + req.params.query);
Product.
find(
{ $text : { $search : req.params.query } },
{ score : { $meta: 'textScore' } }).
sort({ score: { $meta : 'textScore' } }).
limit(10).
exec(handleMany.bind(null, 'products', res));
};
}));
return api;
};
function handleMany(property, res, error, result) {
console.log("We are handling the many");
if (error) {
console.log(error);
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
var json = {};
json[property] = result;
res.json(json);
}
내가 MongoDB를 3.4.2을 실행하는거야 나는 명시 적으로 문을 실행했다 db.products.ensureIndex ({name : "text"}); MongoDB 셸 및 처음 오류가 발생하지 않았습니다. 쿼리가 2000 밀리 초 이상 걸리면 여전히 시간 초과 오류가 간헐적으로 발생합니다. 나는 위의 코드에서 스키마 인덱스를 사용하고 있기 때문에 명시 적으로 MongoDB 셸에 인덱스를 추가 할 필요가 없다고 생각했지만 확실하지는 않습니다.
감사합니다,
윌리엄
과거에도 색인이 있음에도 불구하고 이와 동일한 문제가있었습니다. 어쩌면 노드를 업그레이드했기 때문일 수 있습니까? 이것은 매우 도움이되었습니다 덕분에, 나는 며칠 동안 머리를 긁적 거리고있었습니다. – timhc22