# Sorting results with Elastic Search, Mongo Db and PHP

**URL:** <https://discuss.elastic.co/t/sorting-results-with-elastic-search-mongo-db-and-php/16476>\
**Category:** Elasticsearch\
**Created:** [March 19, 2014, 7:43pm UTC](https://discuss.elastic.co/t/sorting-results-with-elastic-search-mongo-db-and-php/16476 "2014-03-19T19:43:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Witteveen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marc_witteveen/32/1522_2.png) [@Marc\_Witteveen](https://discuss.elastic.co/u/Marc_Witteveen)\
**Post date:** [March 19, 2014, 7:43pm UTC](https://discuss.elastic.co/t/sorting-results-with-elastic-search-mongo-db-and-php/16476/1 "2014-03-19T19:43:48Z")

</div>

Hi,

I hope somebody can help me, though my question mentions PHP the sort  
problem happens when I use the console to fetch data.

I am having a MongoDb database with data and use elastic search and PHP to  
search and display the search results.

The document contains multilingual, see the example below:

"data": {  
"ti": {  
"en": "Poland-Gdansk: Network Equipment",  
"nl": "Polen-Gdansk: Netwerk Apparatuur",  
"si": "Poljska-Gdansk: Oprema za omrežje"  
}  
}

_Note: the document has obvious more data but I don't believe it is  
relevent at the moment to make my question understandable. If you want me  
to post a complete document, please let me know and I will be happy to do  
so._  
I am getting the search result fine but when I do a sort om ti field for an  
ascending or descending sort the returned dataset is missed up.

I am using the official elastic search PHP library available on  
[https://github.com/elasticsearch/elasticsearch-php](https://github.com/elasticsearch/elasticsearch-php).

The sort order I set as followed:

$sitelanguage = "en";  
$order = "asc";  
$sortOrder = array(  
"title." . $sitelanguage =\> array(  
"order" =\> $order  
)  
);

These array then is put to the code that build the search query:

$searchParams = array(  
'body' =\> array(  
'query' =\> array(  
'bool' =\> array(  
'must' =\> array(  
array(  
'query\_string' =\> array(  
'default\_field' =\> '\_all',  
'query' =\> 'Gdansk'  
)  
),  
)  
)  
),  
'sort' =\> $sortOrder  
)  
);

It looks to me that with ascending sort order the sort that is executed is  
done with the "en" key while descending is done with the "si" key instead  
of the value "Poland-Gdansk: Network Equipment".

How could I solve is so that sorting is done correctly, so when  
$sitelanguage is set to "en" it sorts only on the value in this array and  
obvious if any other value is set in the variable the sort is done using  
that value.

--  
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/4ab37ef4-8f6f-455e-bb6f-947873dad0d1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ab37ef4-8f6f-455e-bb6f-947873dad0d1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [March 20, 2014, 2:12am UTC](https://discuss.elastic.co/t/sorting-results-with-elastic-search-mongo-db-and-php/16476/2 "2014-03-20T02:12:47Z")

</div>

Could you provide a small recreation of the problem in a gist? Just a few  
sample documents and two searches with different sorts that don't work?

It could be your mapping that is incorrect, or a syntax issue with the  
query/doc/mapping. It may be a typo, but you are sorting on  
"title.", but the document you posted has the inner object named  
"ti", not "title".

But, assuming that is just a typo while making the post, a gist recreation  
would be very helpful to debugging this.

-Zach

On Wednesday, March 19, 2014 2:43:48 PM UTC-5, Marc Witteveen wrote:

> Hi,
> 
> I hope somebody can help me, though my question mentions PHP the sort  
> problem happens when I use the console to fetch data.
> 
> I am having a MongoDb database with data and use Elasticsearch and PHP to  
> search and display the search results.
> 
> The document contains multilingual, see the example below:
> 
> "data": {  
> "ti": {  
> "en": "Poland-Gdansk: Network Equipment",  
> "nl": "Polen-Gdansk: Netwerk Apparatuur",  
> "si": "Poljska-Gdansk: Oprema za omrežje"  
> }  
> }
> 
> _Note: the document has obvious more data but I don't believe it is  
> relevent at the moment to make my question understandable. If you want me  
> to post a complete document, please let me know and I will be happy to do  
> so._  
> I am getting the search result fine but when I do a sort om ti field for  
> an ascending or descending sort the returned dataset is missed up.
> 
> I am using the official Elasticsearch PHP library available on  
> [GitHub - elastic/elasticsearch-php: Official PHP client for Elasticsearch.](https://github.com/elasticsearch/elasticsearch-php).
> 
> The sort order I set as followed:
> 
> $sitelanguage = "en";  
> $order = "asc";  
> $sortOrder = array(  
> "title." . $sitelanguage =\> array(  
> "order" =\> $order  
> )  
> );
> 
> These array then is put to the code that build the search query:
> 
> $searchParams = array(  
> 'body' =\> array(  
> 'query' =\> array(  
> 'bool' =\> array(  
> 'must' =\> array(  
> array(  
> 'query\_string' =\> array(  
> 'default\_field' =\> '\_all',  
> 'query' =\> 'Gdansk'  
> )  
> ),  
> )  
> )  
> ),  
> 'sort' =\> $sortOrder  
> )  
> );
> 
> It looks to me that with ascending sort order the sort that is executed is  
> done with the "en" key while descending is done with the "si" key instead  
> of the value "Poland-Gdansk: Network Equipment".
> 
> How could I solve is so that sorting is done correctly, so when  
> $sitelanguage is set to "en" it sorts only on the value in this array and  
> obvious if any other value is set in the variable the sort is done using  
> that value.

--  
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/8a8cd570-b849-4cdf-a7ec-974212a0993d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a8cd570-b849-4cdf-a7ec-974212a0993d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Marc\_Witteveen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marc_witteveen/32/1522_2.png) [@Marc\_Witteveen](https://discuss.elastic.co/u/Marc_Witteveen)\
**Post date:** [March 20, 2014, 9:12am UTC](https://discuss.elastic.co/t/sorting-results-with-elastic-search-mongo-db-and-php/16476/3 "2014-03-20T09:12:10Z")

</div>

title and ti have the same duplicate dataset, I used ti for testing to  
reduce the array elements to just two, but no luck.

I'll get some test data and post it in a bit.

/M

On Thursday, 20 March 2014 03:12:47 UTC+1, Zachary Tong wrote:

> Could you provide a small recreation of the problem in a gist? Just a few  
> sample documents and two searches with different sorts that don't work?
> 
> It could be your mapping that is incorrect, or a syntax issue with the  
> query/doc/mapping. It may be a typo, but you are sorting on  
> "title.", but the document you posted has the inner object named  
> "ti", not "title".
> 
> But, assuming that is just a typo while making the post, a gist recreation  
> would be very helpful to debugging this.
> 
> -Zach
> 
> On Wednesday, March 19, 2014 2:43:48 PM UTC-5, Marc Witteveen wrote:
> 
> > Hi,
> > 
> > I hope somebody can help me, though my question mentions PHP the sort  
> > problem happens when I use the console to fetch data.
> > 
> > I am having a MongoDb database with data and use Elasticsearch and PHP  
> > to search and display the search results.
> > 
> > The document contains multilingual, see the example below:
> > 
> > "data": {  
> > "ti": {  
> > "en": "Poland-Gdansk: Network Equipment",  
> > "nl": "Polen-Gdansk: Netwerk Apparatuur",  
> > "si": "Poljska-Gdansk: Oprema za omrežje"  
> > }  
> > }
> > 
> > _Note: the document has obvious more data but I don't believe it is  
> > relevent at the moment to make my question understandable. If you want me  
> > to post a complete document, please let me know and I will be happy to do  
> > so._  
> > I am getting the search result fine but when I do a sort om ti field for  
> > an ascending or descending sort the returned dataset is missed up.
> > 
> > I am using the official Elasticsearch PHP library available on  
> > [GitHub - elastic/elasticsearch-php: Official PHP client for Elasticsearch.](https://github.com/elasticsearch/elasticsearch-php).
> > 
> > The sort order I set as followed:
> > 
> > $sitelanguage = "en";  
> > $order = "asc";  
> > $sortOrder = array(  
> > "title." . $sitelanguage =\> array(  
> > "order" =\> $order  
> > )  
> > );
> > 
> > These array then is put to the code that build the search query:
> > 
> > $searchParams = array(  
> > 'body' =\> array(  
> > 'query' =\> array(  
> > 'bool' =\> array(  
> > 'must' =\> array(  
> > array(  
> > 'query\_string' =\> array(  
> > 'default\_field' =\> '\_all',  
> > 'query' =\> 'Gdansk'  
> > )  
> > ),  
> > )  
> > )  
> > ),  
> > 'sort' =\> $sortOrder  
> > )  
> > );
> > 
> > It looks to me that with ascending sort order the sort that is executed  
> > is done with the "en" key while descending is done with the "si" key  
> > instead of the value "Poland-Gdansk: Network Equipment".
> > 
> > How could I solve is so that sorting is done correctly, so when  
> > $sitelanguage is set to "en" it sorts only on the value in this array and  
> > obvious if any other value is set in the variable the sort is done using  
> > that value.

--  
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/7f7410c2-2205-41ea-a633-21a7574502d7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7f7410c2-2205-41ea-a633-21a7574502d7%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, 1:41am UTC](https://discuss.elastic.co/t/sorting-results-with-elastic-search-mongo-db-and-php/16476/4 "2017-07-06T01:41:56Z")

</div>


