0

i'm just discovering elasticsearch and i'm trying to make my first queries. for example in my case, each article has several prices, I would like to sort these articles by minimum price. and therefore I would not take the other ones into consideration. it is only the minimum prices that will have to sort my articles.
here are my articles :

{
           "name": "john",
           "prices": [
              {
                 "price": "22"
              },
              {
                 "price": "28"
              },
              {
                 "price": "8"
              }
           ]
        },
        {
           "name": "peter",
           "prices": [
              {
                 "price": "7"
              },
              {
                 "price": "5"
              },
              {
                 "price": "2"
              }
           ]
        },
        {
           "name": "billy",
           "prices": [
              {
                 "price": "15"
              },
              {
                 "price": "17"
              }
           ]
        }

1 Answer 1

1
POST /_search

{
    "query" : {
        "match_all": {}
    },
    "sort" : [
        {
            "prices.price": {
                "order": "asc",
                "mode" : "min"
            }
        }
    ]
}

Note: If your prices objects are of nested type (and not object) then you also need to add the "nested_path": "prices" inside the prices.price.

More info on sorting can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html

Sign up to request clarification or add additional context in comments.

2 Comments

may be we need to use GET method instead of POST , If yes so please update your answer
@Manishsharma ElasticSearch supports both GET and POST HTTP methods in Search API. And because in our example we are also sending data inside the body of the request, it should be POST, and not GET. Because GET, should not be used to send data in request body - developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET

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.