【问题标题】:How to get some document at bottom in Elasticsearch without using sort?如何在不使用排序的情况下在 Elasticsearch 底部获取一些文档?
【发布时间】:2021-09-13 13:41:03
【问题描述】:

我正在使用弹性搜索 7.9.0
存储的文档如下:

{
  "student": {
    "marks": [
      {
        "sub": 80
      },
      {
        "sub": 90
      },
      {
        "sub": 100
      }
    ],
    "total_marks": 270
  }
}

如果学生缺席考试,我的文件将是

{
  "student": {
    "marks": [
      {
        "sub": 0
      },
      {
        "sub": 0
      },
      {
        "sub": 0
      }
    ],
    "total_marks": 0
  }
}

现在在搜索学生时,我的命中应该包含所有学生,但缺席的学生应该排在最后。
我不应该在总分上添加排序。
还有一些字段要匹配,排序应该根据elasticsearch的默认评分来完成。比如topper可能先来,或者不及格的学生也可能先来,但所有缺席的学生都应该最后来。
我们怎样才能做到这一点?

我尝试为totalmarks 添加负提升。它没有按预期工作。
请帮我。提前致谢。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    Boosting query 可用于对某些文档进行降级

    肯定的:- 我使用 match_all 来返回所有文档。

    Negative :- 适用于从正查询过滤的文档子集,即负文档也必须在正查询中返回。

    查询

    {
      "query": {
        "boosting": {
          "positive": {
            "match_all": {}
          },
          "negative": {
            "range": {
              "student.total_marks": {
                "gte": 0,
                "lte": 0
              }
            }
          },
          "negative_boost": 0.2,
          "boost": 1
        }
      }
    }
    

    结果

    "hits" :
     [
          {
            "_index" : "index32",
            "_type" : "_doc",
            "_id" : "RTsT5HsBssOzZCY8olGD",
            "_score" : 1.0,
            "_source" : {
              "student" : {
                "name" : "Guru",
                "new_student" : true,
                "total_marks" : 100
              }
            }
          },
          {
            "_index" : "index32",
            "_type" : "_doc",
            "_id" : "RjsT5HsBssOzZCY8plFr",
            "_score" : 1.0,
            "_source" : {
              "student" : {
                "name" : "Mayur",
                "new_student" : false,
                "total_marks" : 90
              }
            }
          },
          {
            "_index" : "index32",
            "_type" : "_doc",
            "_id" : "STtN5HsBssOzZCY8olFq",
            "_score" : 1.0,
            "_source" : {
              "student" : {
                "name" : "Abc",
                "new_student" : true,
                "total_marks" : 90
              }
            }
          },
          {
            "_index" : "index32",
            "_type" : "_doc",
            "_id" : "RzsT5HsBssOzZCY8rVFz",
            "_score" : 0.2,
            "_source" : {
              "student" : {
                "name" : "Darshan",
                "new_student" : false,
                "total_marks" : 0
              }
            }
          },
          {
            "_index" : "index32",
            "_type" : "_doc",
            "_id" : "SDsT5HsBssOzZCY8uFEe",
            "_score" : 0.2,
            "_source" : {
              "student" : {
                "name" : "Manu",
                "new_student" : true,
                "total_marks" : 0
              }
            }
        }
    ]
    

    【讨论】:

    • negetive 中的range 不会被考虑用于过滤文档吧?只会考虑用负值的嘘声吧?
    • @ManonandanSK 是的,负提升仅适用于正查询返回的文档子集。正查询将过滤掉文档,负查询将降级它的一个子集
    猜你喜欢
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    相关资源
    最近更新 更多