Result in specific order

I have the following index in elasticsearch 2.3.4.
What will be the query that return me the Desired Output given below from the given collection.

Collection:

[
    {
          "product_id": 1,
          "product_title": "product title 1",
          "product_status": "launched"
    },
    {
          "product_id": 2,
          "product_title": "product title 2",
          "product_status": "launched"
    },
    {
          "product_id": 7,
          "product_title": "product title 7",
          "product_status": "upcoming"
    },
    {
          "product_id": 8,
          "product_title": "product title 8",
          "product_status": "upcoming"
    },
    {
          "product_id": 3,
          "product_title": "product title 3",
          "product_status": "discontinued"
    },
    {
          "product_id": 4,
          "product_title": "product title 4",
          "product_status": "discontinued"
    },
    {
          "product_id": 5,
          "product_title": "product title 5",
          "product_status": "comingsoon"
    },
    {
          "product_id": 6,
          "product_title": "product title 6",
          "product_status": "comingsoon"
    }
]

Desired Output:

[
    {
          "product_id": 1,
          "product_title": "product title 1",
          "product_status": "launched"
    },
    {
          "product_id": 2,
          "product_title": "product title 2",
          "product_status": "launched"
    },
    {
          "product_id": 3,
          "product_title": "product title 3",
          "product_status": "discontinued"
    },
    {
          "product_id": 4,
          "product_title": "product title 4",
          "product_status": "discontinued"
    },
    {
          "product_id": 5,
          "product_title": "product title 5",
          "product_status": "comingsoon"
    },
    {
          "product_id": 6,
          "product_title": "product title 6",
          "product_status": "comingsoon"
    },
    {
          "product_id": 7,
          "product_title": "product title 7",
          "product_status": "upcoming"
    },
    {
          "product_id": 8,
          "product_title": "product title 8",
          "product_status": "upcoming"
    },
]

You probably want to take a look at sorting.

yes but i want to sort it based on product_status in a specific order not asc or desc.

you can see it in my Desire Output format.

Your example is ordered by product_id so I assumed this was the goal. If you want to sort on a different field, thats still sorting and nothing a query will solve. I'd say you add another numeric field along with product_status that you can sort on, as you can then specify your ordering criteria.

Can i use script based sorting ? OR

if i replace the product status value from string to numeric like e.g

launched = 1
discontinued = 2
comingsoon = 3
upcoming = 4

This will be right ?

Yes, and Yes. I'd suggest the second approach, or adding an extra field, since scripts might be slower.

Got it.

Thank you very much.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.