1

On my ElasticSearch (2.x) I have documents like this:

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

I'd like to sort documents by formats.prices.price but only where the formats.prices.currency === 'EUR'

I tried to do a nested field on formats.prices and then run this query:

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

But unfortunately I cannot get the right order.

UPDATE: Relevant part of mapping:

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

1 Answer 1

1

i hope this will solve your problem

{
  "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"
          }
        }
      }
    }
  ]
}
Sign up to request clarification or add additional context in comments.

7 Comments

Nope. It seems it rewrites all the currency to 'EUR' and the order isn't correct.
can you give me your mapping ?
"properties": { "formats": { "type": "nested", "properties": { "name": { "type": "string" }, "prices": { "type": "nested", "properties": { "price": { "type": "double" }, "currency": { "type": "string" } } } } }, "title": { "type": "string" } }
something like this ? it's working for this one i have checked
Mmmm what is changing is formats is not nested in my mapping, should I change it to nested too?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.