sorting - Elasticsearch如何按条件排序

标签 sorting elasticsearch nested

在我的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"
              }
            }
          }
        }
      },

最佳答案

我希望这能解决您的问题

{
  "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"
          }
        }
      }
    }
  ]
}

关于sorting - Elasticsearch如何按条件排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39274990/

相关文章:

r - 在嵌套列表中查找元素的索引?

javascript - 嵌套的 Javascript 模板……这可能/有效吗?

mysql select语句结果限制

python - 合并排序单词列表

MySQL Hibernate 对 2 列进行排序

elasticsearch - 无法在 Titan 中添加混合索引

c# - Elasticsearch 和 .NET

php - 如何先按新日期排序然后按过去日期排序?

ruby - 如何使用动态键对 Ruby Hash 进行排序

python - Elasticsearch Bulk insert w/Python - 套接字超时错误