elasticsearch - 在Elasticsearch中查询排序的嵌套文档

标签 elasticsearch

如果我要查询的查询非常简单明了,我是Elasticsearch和歉意的新手。

我正在使用以下学生及其教育详细信息的映射,

PUT students
{
  "mappings" : {
      "properties" : {
        "StudentName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "Education" : {
          "type" : "nested",
          "properties" : {
            "degreeName" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "schoolName" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "endDate" : {
              "type" : "date"
            },
            "startDate" : {
              "type" : "date"
            }
          }
        }
      }
  }
}

我的数据集中有近15000名学生。
文件范例:
PUT students/_doc/2
{
  "StudentName":"Student 2",
  "Education": [
    {
      "degreeName": "MS",
      "schoolName": "School Y",
      "startDate": "2016-05-01",
      "endDate":"2014-01-01"
    },
    {
      "degreeName": "PhD",
      "schoolName": "School X",
      "startDate": "2019-01-01",
      "endDate":"2017-05-01"
    },
    {
      "degreeName": "BE",
      "schoolName": "School Z",
      "startDate": "2013-05-01",
      "endDate":"2009-01-01"
    }]
}

PUT students/_doc/3
{
  "StudentName":"Student 3",
  "Education": [
    {
      "degreeName": "BE",
      "schoolName": "School P",
      "startDate": "2003-01-01",
      "endDate":"1999-05-01"
    }]
}

我的问题是,我正在尝试做一个简单的查询来显示以“BE”为学位的学生。但是,我希望拥有工程学学士学位的学生的排名要比拥有硕士和博士学位的学生更高。

从我的示例中,如果我查询“BE”,则学生3的排名应高于学生2。我应该能够基于“endDate”属性以降序对嵌套文档进行排序,然后在“degreeName”与“BE”匹配时进行提升在排序嵌套字段的第一个元素中。

有人能对此有所启发吗?我经历了嵌套查询,嵌套过滤器。我确实知道如何使用“内部匹配”对嵌套字段中的元素进行排序。但是我想知道是否有任何方法可以进行排序,然后查询以提供额外的提升。

提前致谢。

最佳答案

最简单的解决方案是在should子句中包含must子句,在should子句中,您仅提及让学生 with BE but without MS or PhD 的逻辑。

所有这些都在您的Boolean Query

请注意,must在逻辑上类似于AND逻辑,而should将是OR

完成此操作后,您只需在 Sort 中添加逻辑(如链接中所述),即可先使用_score进行排序,然后再根据Education.endDate字段进行排序。

下面是解决方案:

POST students/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match" :{
            "StudentName": "student"
          }
        }
      ], 
      "should": [
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "Education",
                  "query": {
                    "terms": {
                      "Education.degreeName.keyword": [
                        "BE"
                      ]
                    }
                  }
                }
              }
            ],
            "must_not": [
              {
                "nested": {
                  "path": "Education",
                  "query": {
                    "terms": {
                      "Education.degreeName.keyword": [
                        "MS",
                        "PhD"
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    { "_score" : { "order": "desc"}},
    {
      "Education.endDate": {
        "order": "desc"
      }
    }
  ]
}

让我知道这是否有帮助!

关于elasticsearch - 在Elasticsearch中查询排序的嵌套文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335407/

相关文章:

amazon-web-services - ELK堆栈(Elasticsearch,Logstash,Kibana)-logstash是必需的组件吗?

elasticsearch - 使用 JDBC 输入插件在多个节点上运行 Logstash

java - elasticsearch - 没有为 [查询] 注册的查询]

elasticsearch - 重启vagrant box后如何自动启动服务?

javascript - 如何将 Kaggle 数据集添加到 elasticsearch 中?

elasticsearch - 如何使用timelion添加两个不同的字段

java - Kibana 查询语言到 java elasticsearch 查询

elasticsearch - 如何将 score_mode=sum 与 Elasticsearch 多匹配查询一起使用

c# Nest 和 Elasticsearch 聚合

elasticsearch - 仅返回匹配的数组项,而不是ElasticSearch中的所有文档值