首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试实现有条件的ElasticSearch排序

尝试实现有条件的ElasticSearch排序
EN

Stack Overflow用户
提问于 2021-09-27 09:00:49
回答 1查看 34关注 0票数 0

这是我的映射:

代码语言:javascript
复制
{
  "test": {
    "aliases": {
      
    },
    "mappings": {
      "courses": {
        "properties": {
          "is_sponsored": {
            "type": "long"
          },
          "sponsored_end_date": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "sponsored_start_date": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "settings": {
            "index": {
              "creation_date": "1609945591003",
              "number_of_shards": "5",
              "number_of_replicas": "1",
              "uuid": "3S6mwaIbSFuTKPtuj8sSWw",
              "version": {
                "created": "6070199"
              },
              "provided_name": "test"
            }
          }
        }
      }
    }
  }
}

我想在顶部显示那些"is_sponsored“值为true且当前日期介于"sponsored_start_date”和"sponsored_end_date“之间的课程。一旦通过了"sponsored_end_date“,它就应该显示在正常位置。我是ElasticSearch的新手,所以请建议一种方法来做这件事。我正在使用php。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-09-29 05:27:10

您可以使用function_score来提升某些文档

查询

代码语言:javascript
复制
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "is_sponsored": 1
                  }
                },
                {
                  "range": {
                    "sponsored_start_date": {
                      "lte": "now"
                    }
                  }
                },
                {
                  "range": {
                    "sponsored_end_date": {
                      "gte": "now"
                    }
                  }
                }
              ]
            }
          },
          "weight": 10
        }
      ],
      "score_mode": "sum"
    }
  }
}

结果

代码语言:javascript
复制
    "hits" : [
      {
        "_index" : "index45",
        "_type" : "_doc",
        "_id" : "sdb6L3wBJ0n9LhX2J02r",
        "_score" : 10.0,
        "_source" : {
          "is_sponsored" : 1,
          "sponsored_start_date" : "2021-09-01",
          "sponsored_end_date" : "2021-09-30"
        }
      },
      {
        "_index" : "index45",
        "_type" : "_doc",
        "_id" : "stb6L3wBJ0n9LhX2PE0c",
        "_score" : 1.0,
        "_source" : {
          "is_sponsored" : 0,
          "sponsored_start_date" : "2021-09-01",
          "sponsored_end_date" : "2021-09-30"
        }
      },
      {
        "_index" : "index45",
        "_type" : "_doc",
        "_id" : "s9b6L3wBJ0n9LhX2Z00v",
        "_score" : 1.0,
        "_source" : {
          "is_sponsored" : 1,
          "sponsored_start_date" : "2021-10-01",
          "sponsored_end_date" : "2021-10-30"
        }
      }
    ]

满足所有三个条件的文档将获得更高的权重。其余的文档将具有正常的评分。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69343936

复制
相关文章

相似问题

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