日期字段上的Elasticsearch无痛脚本编译错误

piwo6bdm  于 8个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(131)

在我的电子商务项目中,我试图根据销售开始字段和来自PIM的ordering Data对产品进行排序。例如:我在ES文档中有salestart键,我们也有orderingDatalike(1,2,3,4)
在FED(react)中,我们需要通过orderingData订购产品,并需要将不可供销售的数据推送到最后

from: fromIndex,
    index: 'products',
    body: {
      size: 30,
      query: {
         bool: {
           must: 
             term: ['category.categoryId'] = 1
         }
      },
      sort: [{_script: {
                 type: 'number',
                 script: {
                   "lang": "painless",
                   "source" : "(doc['saleStart'].value.toInstant().toEpochMilli() > params.current_time.toInstant().toEpochMilli()) ? 1 : 0",
                   "params": {
                      "current_time" : moment(new Date())
                    }
             },
             order: 'asc',
            }},
           'orderingData': 'asc']
    }

但我得到了这样的编译错误

"error": "[script_exception] runtime error, with { script_stack={ 0=\"(doc['saleStart'].value.getMillis() > params.current_time.getMillis()) ? 1 : 0\" & 1=\"                                                             ^---- HERE\" } & script=\"(doc['saleStart'].value.getMillis() > params.current_time.getMillis()) ? 1 : 0\" & lang=\"painless\" & position={ offset=61 & start=0 & end=82 } }"
}

尝试了多种方法,仍然得到相同的错误。我新的elasticsearch请给予我一些解决方案,使其工作
Thanks in advance

a0zr77ik

a0zr77ik1#

推送测试数据
POST _bulk
{ "index": { "_index": "test_products", "_id": "1" } }
{ "category": { "categoryId": 1 }, "saleStart": "2023-08-09T23:00:00Z", "orderingData": 1 }
{ "index": { "_index": "test_products", "_id": "2" } }
{ "category": { "categoryId": 1 }, "saleStart": "2023-08-10T08:00:00Z", "orderingData": 2 }
{ "index": { "_index": "test_products", "_id": "3" } }
{ "category": { "categoryId": 1 }, "saleStart": "2023-08-10T08:00:00Z", "orderingData": 3 }
{ "index": { "_index": "test_products", "_id": "4" } }
{ "category": { "categoryId": 1 }, "saleStart": "2023-08-11T15:00:00Z", "orderingData": 4 }

测试查看当前时间戳,注意可以显示UTC时间戳。

GET /test_products/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "current_timestamp": {
      "script": {
        "lang": "painless",
        "source": "Instant.ofEpochMilli(System.currentTimeMillis()).toString()"
      }
    }
  }
}

如果日期大于当前时间戳,查询将按销售开始日期排序。如果没有,它将按orderingData排序。此外,如果时间戳相同,它将在sort by saleStart之后按orderingData排序。

GET /test_products/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "lang": "painless",
          "source": """
            def saleStart;
            if (doc['saleStart'].size() != 0) {
              saleStart = ZonedDateTime.parse(doc['saleStart'].value.toString()).toInstant();
            }
            def now = Instant.ofEpochMilli(System.currentTimeMillis());
            if (saleStart != null && saleStart.isAfter(now)) {
              return saleStart.toEpochMilli();
            } else {
              return now.toEpochMilli();
            }
          """
        },
        "order": "desc"
      }
    },
    {
      "orderingData": {
        "order": "desc"
      }
    }
  ]
}

相关问题