0

我尝试使用 mongoosastic 搜索,但它不起作用

Job.js(猫鼬模式)

var mongoose = require('mongoose');
var mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;

var JobSchema = Schema({
  title: { type: String, es_indexed:true },
  category: { type: Schema.Types.ObjectId, ref: 'Category', es_indexed:true},
  salary: { type: String, es_indexed:true },
});


JobSchema.plugin(timestamps);
JobSchema.plugin(mongoosastic, {
  hosts: [
    'localhost:9200'
  ]});
module.exports = mongoose.model('Job', JobSchema);

路由.js

var express = require('express'); 
var app = express();       
var Job = require('../models/job');


Job.createMapping(function(err, mapping) {
  if (err) {
    console.log('error creating mapping (you can safely ignore this)');
    console.log(err);
  } else {
    console.log('mapping created!');
    console.log(mapping);
  }
});


app.post('/api/search/', function(req, res, next) {
  Job.search({query_string: {query: req.body.test}}, function(err, results) {
        if (err) return next(err);
        res.send(results);
    });
});

这是已经保存在 mongodb 中的数据集

    {
     "id": 12313,
     "title": "Engineer"
    },
    {
     "id": 13123,
     "title": "Doctor"  
    },
    {
     "id": 121243134,
     "title": "Software Engineer"
    }

当我尝试像“工程师”一样运行搜索和搜索时,我不断得到这个结果。

在此处输入图像描述

为 Curl 更新

curl -XGET localhost:9200/_mapping

在此处输入图像描述

curl -XGET localhost:9200/_search

在此处输入图像描述

4

1 回答 1

2

由于title是一个已分析的字符串,它的值是使用标准分析器索引的,即以小写形式,因此“工程师”将被索引,而“工程师”

尝试用小写字母搜索“engineer”。

更新

根据我们的讨论,问题似乎只是您的 Elasticsearch 是空的。由于您在 MongoDB 中有现有数据,因此您需要确保在模型上调用该synchronize()方法,以便在 Elasticsearch 中索引您的所有 MongoDB 集合。

于 2015-11-24T07:47:25.800 回答