基于嵌套键值数组对弹性搜索查询进行排序 [英] Sort elastic search query based on a nested key value array

查看:84
本文介绍了基于嵌套键值数组对弹性搜索查询进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json

I have a json

{
    "uniqueKey": "918084",
    "dataValue": {
        "metadata": {
            "timestamps": [{
                "key": "startTime",
                "value": "2017-02-07T18:00:00-06:00"
            }, {
                "key": "processedTime",
                "value": "2017-02-07T18:05:00-06:00"
            }]
        }
    }
}

我必须编写一个查询以对startTime进行排序。有谁能在弹性搜索中为此编写查询。 dataValue是一个嵌套字段。

I have to write a query to sort on startTime. Does anyone how I can write a query for this in elastic search. dataValue is a nested field.

推荐答案

您可以对 nested_pa​​th 使用 sort >。

假设您具有以下映射:

You can use sort with nested_path.
Suppose you have the following mapping:

{
  "test-so": {
    "mappings": {
      "with-dates": {
        "properties": {
          "datavalue": {
            "type": "nested",
            "properties": {
              "metadata": {
                "type": "nested",
                "properties": {
                  "timestamps": {
                    "type": "nested",
                    "properties": {
                      "key": {
                        "type": "keyword"
                      },
                      "value": {
                        "type": "date",
                        "format": "date_optional_time"
                      }
                    }
                  }
                }
              }
            }
          },
          "uniquekey": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

您可以使用排序,如下所示:

You can use sort as follow:

GET /test-so/with-dates/_search
{
  "query": {
    "nested": {
      "path": "datavalue.metadata.timestamps",
      "query": {
        "term": {
          "datavalue.metadata.timestamps.key": {
            "value": "startTime"
          }
        }
      }
    }
  },
  "sort": [
    {
      "datavalue.metadata.timestamps.value": {
        "order": "desc",
        "nested_path": "datavalue.metadata.timestamps"
      }
    }
  ]
}

这将返回按日期在 startTime 上订购的文档。

希望这样做帮助,也许映射不完全相同。 (注意:已在Elastic 5.1上测试)。

That will return the documents ordered by the date on startTime.
Hope this help, maybe the mapping is not exactly the same. (note: tested on Elastic 5.1).

这篇关于基于嵌套键值数组对弹性搜索查询进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆