My elasticsearch mapping has been created to support following data structure:
"commodities" : [ { "name" : "commodity1", "displayPrice" : "price1", "prices" : [ "price" : { "type" : "price1", "amount" : "1000" }, "price" : { "type" : "price2", "amount" : "1100" } "price" : { "type" : "price3", "amount" : "1200" } ] }, { "name" : "commodity2", "displayPrice" : "price2", "prices" : [ "price" : { "type" : "price1", "amount" : "1300" }, "price" : { "type" : "price2", "amount" : "1100" } "price" : { "type" : "price3", "amount" : "1500" } ] } ]
The price object is of nested type. "displayPrice" is "not_analyzed". prices.price.type is "not_analyzed".
Now, I want to do two things here: 1. When user searches for price, the DSL query should be able to find and return the display price, e.g., if user wants to search for commodity with display price between 950 and 1150, he should get both, commodity1 and commodity2, as, for commodity1, the displayPrice is "price1" and the price.type="price1" has value 1000. 2. When user wants to sort commodity by price, the DSL should be able to sort based on the displayPrice for individual commodity.
Any help / pointers would be really appreciated.
Thanks.
==================================================================
Edited, with further details of requirements:
Thanks a lot for going through the question and preparing the code. I should have ideally done it. I believe I've mis-quoted my question. Let me re-phrase the question: I've two requirements from the given data-set:
- When user searches for commodity with price range 950-1150, the system should first check what is the value for "displayPrice" and then use this value as a query to find the "price" object with that "price.type". So, e.g., for commodity1, the "displayPrice" is "price1". The corresponding "price" object under "prices" (with "price.type"="price1") has amount as "1000". So, this commodity should be returned. Similarly, for commodity2 has "displayPrice" as "price2". The corresponding "price" object in commodity2 under "prices" (with "price.type"="price2") has amount as "1100". So, this commodity should be returned.
- When user sorts the commodities by price, the value for "displayPrice" should be picked. The "price" object having "price.type" as this value should be used for sorting. So, in commodity1, it should use "1000" (against "price1") and in commodity2, it should use "1100" (against "price2").