# JsonObject to SortBuilder object

**URL:** <https://discuss.elastic.co/t/jsonobject-to-sortbuilder-object/21723>\
**Category:** Elasticsearch\
**Created:** [January 20, 2015, 12:50am UTC](https://discuss.elastic.co/t/jsonobject-to-sortbuilder-object/21723 "2015-01-20T00:50:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Thurston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_thurston/32/975_2.png) [@Alex\_Thurston](https://discuss.elastic.co/u/Alex_Thurston)\
**Post date:** [January 20, 2015, 12:50am UTC](https://discuss.elastic.co/t/jsonobject-to-sortbuilder-object/21723/1 "2015-01-20T00:50:16Z")

</div>

I would like to turn an arbitrary JsonObject (which presumably follows the  
Search/Sort DSL into a SortBuilder which can then be passed to the  
SearchRequestBuilder::addSort.

I've gotten this to work by simple parsing the JsonObject myself and  
calling the appropriate calls in the SortBuilder, but that means that I  
have to implement the parsing for every variation of the DSL.

If I've got a Java JsonObject that looks like:

{  
"first\_name": "asc"  
}

OR

{  
"first\_name": {  
"order": "asc"  
}  
}

OR

{  
"\_geo\_distance":{  
"my\_position":{  
"order": "asc"  
}  
}  
}

All of which are valid Json for the sort, I would imagine there's a way to  
call:

JsonObject sort\_json =   
SortBuilder sort = new SortBuilder()  
sort.setSort(sort\_json);

I'm almost certain I'm missing something but can't for the life of me  
figure out how to do it.

Thanks in advance.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c09c4e67-68fe-4552-9161-eca139264511%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c09c4e67-68fe-4552-9161-eca139264511%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Alex\_Thurston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_thurston/32/975_2.png) [@Alex\_Thurston](https://discuss.elastic.co/u/Alex_Thurston)\
**Post date:** [January 20, 2015, 9:03pm UTC](https://discuss.elastic.co/t/jsonobject-to-sortbuilder-object/21723/2 "2015-01-20T21:03:11Z")

</div>

Here's a simpler way to ask the question:

I've got this:

GeoDistanceSortBuilder sorter = new  
GeoDistanceSortBuilder("values.geo\_location");  
sorter.point(0.0, 0.0);  
sorter.order(SortOrder.DESC);  
sorter.unit(DistanceUnit.KILOMETERS);  
sorter.geoDistance(GeoDistance.PLANE);  
sorter.toString()

Which produces the string

"\_geo\_distance"{  
"values.geo\_location" : [0.0, 0.0],  
"unit" : "km",  
"distance\_type" : "plane",  
"reverse" : true  
}

I would like to do the opposite. I have the above string, and I want to  
turn it into a GeoDistanceSortBuilder without having to manually parse it.

On Monday, January 19, 2015 at 7:50:17 PM UTC-5, Alex Thurston wrote:

> I would like to turn an arbitrary JsonObject (which presumably follows the  
> Search/Sort DSL into a SortBuilder which can then be passed to the  
> SearchRequestBuilder::addSort.
> 
> I've gotten this to work by simple parsing the JsonObject myself and  
> calling the appropriate calls in the SortBuilder, but that means that I  
> have to implement the parsing for every variation of the DSL.
> 
> If I've got a Java JsonObject that looks like:
> 
> {  
> "first\_name": "asc"  
> }
> 
> OR
> 
> {  
> "first\_name": {  
> "order": "asc"  
> }  
> }
> 
> OR
> 
> {  
> "\_geo\_distance":{  
> "my\_position":{  
> "order": "asc"  
> }  
> }  
> }
> 
> All of which are valid Json for the sort, I would imagine there's a way to  
> call:
> 
> JsonObject sort\_json =   
> SortBuilder sort = new SortBuilder()  
> sort.setSort(sort\_json);
> 
> I'm almost certain I'm missing something but can't for the life of me  
> figure out how to do it.
> 
> Thanks in advance.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a756b6b3-7e28-44f7-be88-e8d6e102b6e5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a756b6b3-7e28-44f7-be88-e8d6e102b6e5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:37am UTC](https://discuss.elastic.co/t/jsonobject-to-sortbuilder-object/21723/3 "2017-07-06T00:37:41Z")

</div>

Closed on July 6, 2017, 12:37am UTC


