elasticsearch搜索文本返回全数组问题 [英] elasticsearch search text return full array issue
问题描述
我正在使用 mongoosastic 进行 elasticsearch .我完成了所有设置,并且工作正常.但是问题是结果没有得到正确的显示.
I am using mongoosastic for elasticsearch. and i done all setup and its working fine. but problem is result are not getting properly.
文件:猫鼬和猫鼬.
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对象
Now my problem is if task array has 3 object and when i search for task string i.e "abc" it will return full collection. with all task But i want only searched string object from task array. i.e name :abc object
......
......
"task" [{
name: 'abc',
taskCode: 123
},{
name: 'xyz',
taskCode: 123
},{
name: 'cdx',
taskCode: 123
}]
推荐答案
The good thing is that your task
field is already of type nested
in your schema, which is a pre-condition for achieving what you expect.
Now in order to achieve what you want you need to use inner_hits
in your query.
UserProfileSchema.search({
"query": {
"nested": {
"path": "task",
"query": {
"match": {
"task.name": name
}
},
"inner_hits": {} <--- this does the magic
}
}
}, ...
这篇关于elasticsearch搜索文本返回全数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!