【问题标题】:Query Sorted nested documents in Elasticsearch在 Elasticsearch 中查询排序的嵌套文档
【发布时间】:2019-10-11 06:28:47
【问题描述】:

我是 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(工程学士)学位的学生排名高于同时获得硕士和博士学位的学生。

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

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

提前致谢。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    最简单的解决方案是将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"
          }
        }
      ]
    }
    

    如果这有帮助,请告诉我!

    【讨论】:

    • 感谢您的解决方案,但我想获得更通用的解决方案。 @Kamal 我的学位列表不只是“BE,MS,PhD”。如果我需要只用“BE”对学生进行排名,那么我必须在“不得”中传递一个更大的列表,这使得它有点复杂。我在寻找是否有办法通过匹配排序的内部命中的第一个元素来提高分数。
    猜你喜欢
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2013-01-08
    • 2018-03-09
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多