【问题标题】:elasticsearch search text return full array issueelasticsearch搜索文本返回完整数组问题
【发布时间】:2015-11-24 03:01:40
【问题描述】:

我将 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
            });
        }
    });

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

......

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

【问题讨论】:

    标签: node.js elasticsearch mongoose mongoosastic


    【解决方案1】:

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

    现在为了实现您想要的,您需要在查询中使用inner_hits

    UserProfileSchema.search({
      "query": {
        "nested": {
          "path": "task",
          "query": {
            "match": {
              "task.name": name
            }
          },
          "inner_hits": {}        <--- this does the magic
        }
      }
    }, ...
    

    【讨论】:

    • 我可以在 query_string: { query: name} 中编写这种类型的查询,如我在示例中给出的。因为我试过 **match in query_string ** 它返回错误
    • 尝试用上面的查询替换UserProfileSchema.search中的整个查询。
    • 确定我现在会尝试并让您知道请帮助我,我为此花了很多时间。
    • 在elasticsearch的控制台中,它会显示错误无法解析源和我在数组中的查询[查询........]
    • 你用的是什么版本的ES? inner_hits 在 1.5.0 中添加。也许你可以用你得到的输出来更新你的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    相关资源
    最近更新 更多