【问题标题】:Elasticsearch how to sort with conditionElasticsearch如何按条件排序
【发布时间】:2017-01-09 13:00:57
【问题描述】:

在我的 ElasticSearch (2.x) 上,我有这样的文档:

{
    "title": "A good title",
    "formats": [{
        "name": "pdf",
        "prices": [{
            "price": 11.99,
            "currency": "EUR"
        }, {
            "price": 18.99,
            "currency": "AUD"
        }]
    }]
}

我想按formats.prices.price 对文档进行排序,但仅限formats.prices.currency === 'EUR' 的位置

我尝试在 formats.prices 上做一个嵌套字段,然后运行这个查询:

{
  "query": {
    "filtered": {
      "query": {
        "and": [
          {
            "match_all": {}
          }
        ]
      }
    }
  },
  "sort": {
    "formats.prices.price": {
      "order": "desc",
      "nested_path": "formats.prices",
      "nested_filter": {
        "term": {
          "currency": "EUR"
        }
      }
    }
  }
}

但不幸的是,我无法得到正确的订单。

更新: 映射的相关部分:

   "formats": {
        "properties": {
          "name": {
            "type": "string"
          },
          "prices": {
            "type": "nested",
            "include_in_parent": true,
            "properties": {
              "currency": {
                "type": "string"
              },
              "price": {
                "type": "double"
              }
            }
          }
        }
      },

【问题讨论】:

    标签: sorting elasticsearch nested


    【解决方案1】:

    希望能解决你的问题

    {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "formats.prices",
                "filter": {
                  "match": {
                    "formats.prices.currency": "EUR"
                  }
                }
              }
            }
          ]
        }
      },
      "from": 0,
      "size": 50,
      "sort": [
        {
          "formats.prices.price": {
            "order": "asc",
            "nested_path": "formats.prices",
            "nested_filter": {
              "match": {
                "formats.prices.currency": "EUR"
              }
            }
          }
        }
      ]
    }
    

    【讨论】:

    • 不。它似乎将所有货币重写为“欧元”并且顺序不正确。
    • 你能给我你的映射吗?
    • "properties": { "formats": { "type": "nested", "properties": { "name": { "type": "string" }, "prices": { “类型”:“嵌套”,“属性”:{“价格”:{“类型”:“双”},“货币”:{“类型”:“字符串”}}}}},“标题”:{ “类型”:“字符串”} }
    • 这样的?它适用于我检查过的这个
    • 嗯,正在发生的变化是formats 没有嵌套在我的映射中,我也应该将其更改为嵌套吗?
    猜你喜欢
    • 2023-01-09
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2018-09-20
    • 2021-12-13
    • 1970-01-01
    • 2013-03-28
    相关资源
    最近更新 更多