Result in specific order


(Masood Rehman) #1

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

(Christoph) #2

You probably want to take a look at sorting.


(Masood Rehman) #3

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


(Masood Rehman) #4

you can see it in my Desire Output format.


(Christoph) #5

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.


(Masood Rehman) #6

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 ?


(Christoph) #7

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


(Masood Rehman) #8

Got it.

Thank you very much.


(system) #9

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