来自内部数组的 Elasticsearch 全文查询问题
Elasticsearch full-text query issue from inner array
这是我的对象。我想在任务中找到文本,即 task.name.
{
"_id" : ObjectId("55e2acd77e5e4ddc037b3ef5"),
"userId" : "123",
"username" : "user12",
"address" : "abc",
"number" : 928228828282.0,
"task" : [{
"name" : "metac",
"productCode" : "1234",
"_id" : ObjectId("55e2acd77e5e4ddc037b3ef7")
}, {
"name" : "alfa33",
"productCode" : "1234",
"_id" : ObjectId("55e2acd77e5e4ddc037b3ef6")
}],
"__v" : 0
}
所以当我查询时,它将 return 所有任务。
curl -XPOST 'localhost:9200/userprofiles/_search?pretty' -d '
{
"query": { "match": { "name": "alfa33" } }
}
输出:
{
"took": 51,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.19178301,
"hits": [
{
"_index": "userprofiles",
"_type": "userprofile",
"_id": "55e2acd77e5e4ddc037b3ef5",
"_score": 0.19178301,
"_source": {
"userId": "123",
"username": "user12",
"address": "abc",
"number": 928228828282,
"task": [
{
"name": "metac"
},
{
"name": "alfa33"
}
]
}
}
]
}
}
如您所见,任务 return 完整数组我只想要已选中的 1 个任务。
我在节点内使用 mongoosastic 它会给我带来问题所以我尝试直接使用请求到 Elasticsearch。
mongoosastic 配置 - >elasticsearch search text return full array issue 我试过这个解决方案但没有用。
目前我正在 git bush 中使用 curl 命令搜索我的结果,没有使用搜索功能
编辑
文件:-猫鼬和猫鼬。
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" 字段,请按如下方式设置映射:
{
"mappings": {
"doc": {
"... all the other fields ...": {},
"task": {
"properties": {
"_id": {
"type": "string"
},
"name": {
"type": "string"
},
"productCode": {
"type": "string"
}
},
"type": "nested"
}
}
}
}
重新索引文档后。您需要将嵌套查询与
内部命中查询 return 哪个任务与查询匹配:
{
"query": {
"nested": {
"path": "task",
"query": {
"match": {
"name": "alfa33"
}
},
"inner_hits": {}
}
}
}
inner_hits
部分将 return 匹配查询的特定任务
本身。
我已经 post举了一个例子
full output here 因为它是
有点长到 post 完整。
这是我的对象。我想在任务中找到文本,即 task.name.
{
"_id" : ObjectId("55e2acd77e5e4ddc037b3ef5"),
"userId" : "123",
"username" : "user12",
"address" : "abc",
"number" : 928228828282.0,
"task" : [{
"name" : "metac",
"productCode" : "1234",
"_id" : ObjectId("55e2acd77e5e4ddc037b3ef7")
}, {
"name" : "alfa33",
"productCode" : "1234",
"_id" : ObjectId("55e2acd77e5e4ddc037b3ef6")
}],
"__v" : 0
}
所以当我查询时,它将 return 所有任务。
curl -XPOST 'localhost:9200/userprofiles/_search?pretty' -d '
{
"query": { "match": { "name": "alfa33" } }
}
输出:
{
"took": 51,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.19178301,
"hits": [
{
"_index": "userprofiles",
"_type": "userprofile",
"_id": "55e2acd77e5e4ddc037b3ef5",
"_score": 0.19178301,
"_source": {
"userId": "123",
"username": "user12",
"address": "abc",
"number": 928228828282,
"task": [
{
"name": "metac"
},
{
"name": "alfa33"
}
]
}
}
]
}
}
如您所见,任务 return 完整数组我只想要已选中的 1 个任务。
我在节点内使用 mongoosastic 它会给我带来问题所以我尝试直接使用请求到 Elasticsearch。
mongoosastic 配置 - >elasticsearch search text return full array issue 我试过这个解决方案但没有用。
目前我正在 git bush 中使用 curl 命令搜索我的结果,没有使用搜索功能
编辑
文件:-猫鼬和猫鼬。
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" 字段,请按如下方式设置映射:
{
"mappings": {
"doc": {
"... all the other fields ...": {},
"task": {
"properties": {
"_id": {
"type": "string"
},
"name": {
"type": "string"
},
"productCode": {
"type": "string"
}
},
"type": "nested"
}
}
}
}
重新索引文档后。您需要将嵌套查询与 内部命中查询 return 哪个任务与查询匹配:
{
"query": {
"nested": {
"path": "task",
"query": {
"match": {
"name": "alfa33"
}
},
"inner_hits": {}
}
}
}
inner_hits
部分将 return 匹配查询的特定任务
本身。
我已经 post举了一个例子 full output here 因为它是 有点长到 post 完整。