繁体   English   中英

Elasticsearch 查询对象数组的多个 object 字段

[英]Elasticsearch Query on multiple object fields of an Array of Objects

我有一个看起来像这样的产品文档:

{
        "_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"
                    ]
                }
            ]
            
        }
    }

我的 model 看起来像这样。

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,
        });```

您快到了,但查询格式严重错误。

试试这个——我已经对其进行了调整以匹配上面的示例文档:

const body = {
  size,
  from: pageno * size,
  "query": {
    "bool": {
      "must": [
        {
          "match": { shopId }
        },
        {
          "nested": {
            "path": "productTimings",
            "query": {
              "bool": {
                "must": [
                  {
                    "range": {
                      "productTimings.startHour": {
                        "gte": 8
                      }
                    }
                  },
                  {
                    "range": {
                      "productTimings.endHour": {
                        "lte": 9
                      }
                    }
                  },
                  {
                    "match": {
                      "productTimings.dayOfWeek": "Fri"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

prodDetails = await client.search({ 
    index: 'productss',
    body: body
})

提示:我建议使用term查询而不是match 甚至在此之前,我会将shopIddayOfWeek转换为keywords 当它们被映射为String (= text ) 时,它们将被standard分析器小写——你不希望这样。 您可能需要keyword映射保证的区分大小写的完全匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM