0

我将mongoosastic用于elasticsearch。我完成了所有设置并且工作正常。但问题是结果没有得到正确。

文件:-猫鼬和猫鼬。

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var medicineSchema = require('./search')
var mongoosastic = require("mongoosastic");

var UserProfileSchema = new Schema({
    userId: String,
    username: String,
    address: String,
    number: Number,
    task: [{
        name: {
            type: String,
            es_boost: 2.0 // or es_indexed:true
        },
        taskCode: String,
    }]
});
UserProfileSchema.plugin(mongoosastic);
UserProfileSchema.plugin(mongoosastic, {
    host: "localhost",
    port: 9200,
    //  ,curlDebug: true
});
UserProfile = module.exports = mongoose.model('UserProfile', UserProfileSchema);
UserProfile.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);
    }
});

我的搜索查询:

var UserProfileSchema = require('../../app/models/user');
 UserProfileSchema.search({
        query_string: {
            query: name
        }
    }, function(err, result) {
        if (err) {
            callback({
                RESULT_CODE: '-1',
                MESSAGE: 'System error'
            });
        } else {
            callback({
                RESULT_CODE: '1',
                DATA: result
            });
        }
    });

现在我的问题是如果 任务数组有 3 个对象,当我搜索任务字符串,即“abc”时,它将返回完整的集合。所有任务但我只想从任务数组中搜索字符串对象。即名称:abc对象

……

"task" [{
    name: 'abc',
    taskCode: 123
},{
    name: 'xyz',
    taskCode: 123
},{
    name: 'cdx',
    taskCode: 123
}]

4

1 回答 1

0

好消息是您的task字段已经是nested模式中的类型,这是实现您期望的先决条件。

现在为了实现您想要inner_hits在查询中使用的内容。

UserProfileSchema.search({
  "query": {
    "nested": {
      "path": "task",
      "query": {
        "match": {
          "task.name": name
        }
      },
      "inner_hits": {}        <--- this does the magic
    }
  }
}, ...
于 2015-08-30T03:52:36.057 回答