【发布时间】:2021-06-23 07:21:33
【问题描述】:
我有一个如下所示的产品文档:
{
"_index": "productss",
"_type": "_doc",
"_id": "2fb60b1880f0251af4340af009",
"_score": 5.0262785,
"_source": {
"prepTime": {
"durationType": "Min",
"value": 8,
"imageUrl": ""
},
"shopId": "CCXow8ALRDrALRSKFC",
"productTimings": [
{
"startHour": 8,
"endHour": 9,
"startMin": 30,
"endMin": 45,
"dayOfWeek": [
"Mon",
"Tue",
"Wed",
"Thu",
"Fri"
]
},
{
"startHour": 16,
"endHour": 18,
"startMin": 30,
"endMin": 45,
"dayOfWeek": [
"Sat",
"Sun"
]
}
]
}
}
我的模型看起来像这样。
const mongoosastic = require('mongoosastic');
const { Schema } = mongoose;
const Timing = new Schema({
startHour: { type: Number, es_indexed: true },
endHour: { type: Number, es_indexed: true },
startMin: { type: Number, es_indexed: true },
endMin: { type: Number, es_indexed: true },
dayOfWeek: [{ type: String, es_indexed: true }],
});
const productsSchema = mongoose.Schema({
_id: { type: String },
prepTime : {
durationType : { type: String, es_indexed: true },
value : { type: Number, es_indexed: true },
imageUrl : { type: String, es_indexed: true }
},
shopId: { type: String, es_indexed: true },
productTimings: {
type: [Timing],
es_indexed: true,
es_type: 'nested',
es_include_in_parent: true,
}
});
productsSchema.plugin(mongoosastic);
module.exports = mongoose.model('Products', productsSchema, 'Products');
我需要获取 startHours:Startminute 小于当前时间且 endHour:endMinute 小于当前时间的所有产品。它还应该与当前日期的 dayOfWeek 匹配。
请注意,productTimings 中可能有上午时段和晚上时段。
我已经尝试了以下方法并做到了这一点,但无法继续进行:
prodDetails = await client.search({
index: 'productss',
body:
{
query:
{
bool: {
must: [
{ match: { shopId } },
{
"nested": {
"path": "productTimings",
"query": {
"range": {
"productTimings.startHour": {
"lte": 12,
}
},
"range": {
"productTimings.endHour":{
"gte": 11,
}
}
}
}
}
]
}
}
},
},
from: pageno * size,
size,
});```
【问题讨论】:
-
嗨,我一直在使用同一个包,mongoosastic。我面临的问题是,当我使用 es_type: 'nested' 创建嵌套索引时,在 elasticsearch 中生成和更新的映射仍然是一般的扁平对象类型。查询时出现此错误,
failed to create query: [nested] nested object under path [productTimings] is not of nested type。我需要做任何额外的步骤来确保映射是嵌套类型的吗?
标签: node.js mongodb elasticsearch