错误是在下面的 Product.find 语句中触发的:
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);
}
我在 Windows 10 上运行 MongoDB 3.4.2。我在 MongoDB shell 中明确运行了语句 db.products.ensureIndex({name: "text"});时间我没有收到错误。但是,当查询时间超过 2000 毫秒时,它仍然会间歇性地出现超时错误。我认为我不必在 MongoDB shell 中显式添加索引,因为我在上面的代码中添加了架构索引,但我不确定。
谢谢
威廉
最佳答案
我进入 MongoDB shell 并将索引放在产品集合上,如下所示:
db.products.createIndex({name: "text"})
这是我的解决方案,它有效,但我不知道是否存在某个地方的故障,使得该解决方案成为必要。
关于javascript - 无法规范化查询 : IndexNotFound: text index required for $text query', 代码:17287,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44031415/