两个字段的复杂顺序?

8wtpewkr  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(495)

我有这个样本数据

Name: A
Price: 200
PromotionPrice: 100
PromotionStart: 2020/01/01
PromotionEnd:   2020/01/15
``` `PromotionPrice` 如果没有提升,则为0
我想把这些分类 `PromotionPrice` 先交代( `asc` 或者 `desc` )那些没有 `PromotionPrice` 稍后再来(过滤掉那些有 `PromotionPrice` 但超出促销时间范围)
我试着在 `_script` 但对我来说太复杂了。

### 目前正在开发版本7。

我的Map:

"Price" : {"type": "long"}
"PromotionPrice": {"type": "long"},
"PromotionStart": {"type": "date"},
"PromotionEnd": {"type": "date"},

我的代码:排序方式 `Price` 或者 `PromotionPrice` 但没有优先考虑这些项目与推广第一。

"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"params": {
"now": "120000000"
},
"source": "if (doc['PromotionPrice'].value != 0 && params.now <= doc['PromotionEnd'].value.getMillis() && params.now >= doc['PromotionStart'].value.getMillis()) {doc['selling_price'].value} else {doc['normal_price'].value}"
},
"order": "asc"
}
}
]

预期结果:

Name: A
Price: 200
PromotionPrice: 100
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01

Name: B
Price: 500
PromotionPrice: 200
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01

Name: C
Price: 500
PromotionPrice: 300
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01

Name: E
Price: 100
PromotionPrice: 0
PromotionStart: 2020/11/01
PromotionEnd: 2020/12/01

Name: D
Price: 500 #have PromotionPrice but treated as not having
PromotionPrice: 300 #cuz out of promotion time range
PromotionStart: 2020/11/01
PromotionEnd: 2020/11/05

进一步主题:
基于@val的逻辑:
如果我想分类 `asc` 或者 `desc` 以上升职人员:
与 `now` 以及 `type` 将包含在 `params` ```
def isPromo = (doc.new_price.value > 0 && doc.start_date.value.getMillis() <  Long.parseLong(params.now) && doc.end_date.value.getMillis() >  Long.parseLong(params.now)); 
if (isPromo? && if (params.type == 'asc') {return true;} return false;) 
(return doc.new_price.value: (doc.price.value+100000000) return doc.new_price.value: (doc.price.value-100000000

可惜这个不行

xdnvmnnf

xdnvmnnf1#

很好的开始,你的脚本几乎包含正确的逻辑,你只需要添加一个常量到常规价格,你会看到我在下面的回应。
下面的脚本可以帮助您按预期的方式对结果进行排序。它的工作方式是在脚本的注解中内联的:

POST promotions/_search
{
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": """
        // current time
        def now = new Date().getTime();

        // check if the current product satisfies to the promotion constraints
        def isPromo = (doc.PromotionPrice.value > 0 && doc.PromotionStart.value.getMillis() < now && doc.PromotionEnd.value.getMillis() > now);

        // if the product qualifies for promotion return its promotion price, otherwise return its normal price + a constant
        return isPromo ? doc.PromotionPrice.value : (doc.Price.value + 100000000);
        """
      },
      "order": "asc"
    }
  }
}

结果:

"hits" : [
      {
        "_index" : "promotions",
        "_type" : "_doc",
        "_id" : "ilucrHUB7CBHpnCGfnBn",
        "_score" : null,
        "_source" : {
          "Name" : "A",
          "Price" : 200,
          "PromotionPrice" : 100,
          "PromotionStart" : "2020-11-01",
          "PromotionEnd" : "2020-12-01"
        },
        "sort" : [
          100.0
        ]
      },
      {
        "_index" : "promotions",
        "_type" : "_doc",
        "_id" : "i1ucrHUB7CBHpnCGfnBn",
        "_score" : null,
        "_source" : {
          "Name" : "B",
          "Price" : 500,
          "PromotionPrice" : 200,
          "PromotionStart" : "2020-11-01",
          "PromotionEnd" : "2020-12-01"
        },
        "sort" : [
          200.0
        ]
      },
      {
        "_index" : "promotions",
        "_type" : "_doc",
        "_id" : "jFucrHUB7CBHpnCGfnBn",
        "_score" : null,
        "_source" : {
          "Name" : "C",
          "Price" : 500,
          "PromotionPrice" : 300,
          "PromotionStart" : "2020-11-01",
          "PromotionEnd" : "2020-12-01"
        },
        "sort" : [
          300.0
        ]
      },
      {
        "_index" : "promotions",
        "_type" : "_doc",
        "_id" : "jVucrHUB7CBHpnCGfnBn",
        "_score" : null,
        "_source" : {
          "Name" : "E",
          "Price" : 100,
          "PromotionPrice" : 0,
          "PromotionStart" : "2020-11-01",
          "PromotionEnd" : "2020-12-01"
        },
        "sort" : [
          1.000001E8
        ]
      },
      {
        "_index" : "promotions",
        "_type" : "_doc",
        "_id" : "jlucrHUB7CBHpnCGfnBn",
        "_score" : null,
        "_source" : {
          "Name" : "D",
          "Price" : 500,
          "PromotionPrice" : 300,
          "PromotionStart" : "2020-11-01",
          "PromotionEnd" : "2020-11-05"
        },
        "sort" : [
          1.000005E8
        ]
      }
    ]

相关问题