Sorting results with Elastic Search, Mongo Db and PHP


(Marc Witteveen) #1

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.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/4ab37ef4-8f6f-455e-bb6f-947873dad0d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Zachary Tong) #2

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 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.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/8a8cd570-b849-4cdf-a7ec-974212a0993d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Marc Witteveen) #3

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 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.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/7f7410c2-2205-41ea-a633-21a7574502d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4