首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ElasticSearch -2域上的复杂序?

ElasticSearch -2域上的复杂序?
EN

Stack Overflow用户
提问于 2020-11-09 10:30:46
回答 1查看 38关注 0票数 0

我有这个样本数据

代码语言:javascript
复制
Name: A
Price: 200
PromotionPrice: 100
PromotionStart: 2020/01/01
PromotionEnd:   2020/01/15

如果没有晋升,PromotionPrice将为0。

我想把那些有PromotionPrice先开始的(ascdesc)和那些没有PromotionPrice的排序。(过滤掉那些有PromotionPrice但超出促销时间范围)

我试着在_script上工作,但这对我来说太复杂了。

目前正在开发版本7。

我的地图:

代码语言:javascript
复制
"Price" : {"type": "long"}
"PromotionPrice": {"type": "long"},
"PromotionStart": {"type": "date"},  
"PromotionEnd": {"type": "date"},

我的代码:按PricePromotionPrice排序,但没有优先考虑那些项目和升级。

代码语言:javascript
复制
"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"
                }
            }
        ]

预期结果:

代码语言:javascript
复制
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的逻辑:

如果我想对ascdesc进行排序,下面是那些有晋升的:

nowtype将包含在params

代码语言:javascript
复制
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

遗憾的是这个不起作用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-09 10:53:52

很好的开始,您的脚本几乎包含正确的逻辑,您只需要添加一个常数到常规价格,您将在下面的反应中看到。

下面的脚本可以帮助您按照预期的方式对结果进行排序。它的工作方式包含在脚本中的注释中:

代码语言:javascript
复制
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"
    }
  }
}

结果:

代码语言:javascript
复制
    "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
        ]
      }
    ]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64749890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档