简体   繁体   English

如何根据elasticsearch中存储的字段值获取前5个文档?

[英]How to get top 5 documents based on a stored field value in elasticsearch?

Example - test scores of one subject of students are stored in Elastic search index.示例 - 学生的一个科目的考试成绩存储在弹性搜索索引中。 Now I want to find top 5 students who scored highest in that subject.现在我想找到该科目得分最高的前 5 名学生。

Let me show it by using the example, But the first understand the concept.让我通过使用示例来展示它,但首先要了解这个概念。

First query based on the given subject, in my example famous math subject :) and then it will fetch all the students, having math subject, after that sort in desc order based in their score and then size param restricted the result to top k students.根据给定的题目第一个查询,在我的例子著名math学科:),然后将获取所有的学生,有math学科,是经过sortdesc顺序基于他们的score ,然后size PARAM限制的结果顶部k学生.

Imp links related to answer:与答案相关的 Imp 链接:

https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-from-size.html https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-from-size.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html

Create an index创建索引

{
    "mappings": {
        "properties": {
            "sid": {
                "type": "integer"
            },
            "subject" :{
                "type" : "keyword"
            },
            "score" :{
                "type" : "integer"
            }
        }
    }
}

Index some docs索引一些文档

{
   "sid" : 1,
   "subject" :"math",
   "score" : 50
}

{
   "sid" : 2,
   "subject" :"math",
   "score" : 60
}
{
   "sid" : 3,
   "subject" :"math",
   "score" : 70
}
{
   "sid" : 4,
   "subject" :"math",
   "score" : 90
}
{
   "sid" : 5,
   "subject" :"math",
   "score" : 99
}

{
   "sid" : 6,
   "subject" :"math",
   "score" : 100
}

Search query to search on subject and sort results based on marks and return top 3 students.搜索查询以搜索subject并根据分数对结果进行排序并返回前 3 名学生。

{
    "sort": [
        {
            "score": {
                "order": "desc" --> define sort order
            }
        }
    ],
    "query": {
        "term": {
            "subject": "math"
        }
    },
    "size":3 -->important, you can change it to fetch top K students, Just change 3 to whatever no you want to fetch.
}

Search query result搜索查询结果

{
            "_index": "leaderboard",
            "_type": "_doc",
            "_id": "5",
            "_score": null,
            "_source": {
               "sid": 6,
               "subject": "math",
               "score": 100
            },
            "sort": [
               100
            ]
         },
         {
            "_index": "leaderboard",
            "_type": "_doc",
            "_id": "4",
            "_score": null,
            "_source": {
               "sid": 4,
               "subject": "math",
               "score": 90
            },
            "sort": [
               90
            ]
         },
         {
            "_index": "leaderboard",
            "_type": "_doc",
            "_id": "3",
            "_score": null,
            "_source": {
               "sid": 3,
               "subject": "math",
               "score": 70
            },
            "sort": [
               70
            ]
         }

Simply sort searched results by the field you want ie score只需按您想要的字段对搜索结果进行排序,即score
This doc may help you https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html该文档可以帮助您https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elasticsearch:如何通过匹配分数获取字段的顶级唯一值? - Elasticsearch: how to get the top unique values of a field sorted by matching score? 如果两个文档的排序值相同,则在Elasticsearch中如何排序文档? - How are the documents ordered in Elasticsearch if the sort value for two documents is same? 如何在 Elasticsearch 中使用多个字段匹配对存储桶中的文档进行排序? - How to sort the documents in the bucket using more then one field match in Elasticsearch? 如何将Elasticsearch中的字段值从字符串更改为整数? - How to change field value in elasticsearch from string to integer? Elasticsearch 首先根据值排序 - Elasticsearch Sort based on value first 在Elasticsearch中获取每组的前n个值 - Get top n values per group in elasticsearch 基于多值字段中元素的Elasticsearch排序 - Elasticsearch sort based on element in multivalue field 基于脚本的 Elasticsearch 日期字段排序 - Script-based sorting on Elasticsearch date field Elastic Seach-按分数过滤前N个文档,然后按字段排序 - Elastic Seach - filter top N documents by score and then sort by field 如何根据字段对mongo进行排序,但优先考虑满足一定条件的文档? - How to sort in mongo based on a field but give preference to documents satisfying certain condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM