ElasticPressLabs icon indicating copy to clipboard operation
ElasticPressLabs copied to clipboard

BUG: WP_User_Query orderby

Open ws256 opened this issue 3 years ago • 2 comments

Hi there.

In order to get relevant user search results, we used 'orderby' => 'relevance' in WP_User_Query But I would like to use other sorting options as well. quote from documentation "You can sort by multiple fields as well i.e. title meta.my_key.raw" 'orderby' => 'relevance meta.my_key.long', Only one sort option works.
If you specify two parameters, we get a 400 error. "Query Response Code: HTTP 400"

$wp_user_query = new WP_User_Query([
	'search' => esc_attr($query_str),
	'number' => 10,
	'paged' => $current_page,
	'orderby' => 'title meta.user_points.long',
	'order' => 'DESC',
	'offset' => $offset,
	'search_columns' => [
		'user_nicename',
		'display_name'
	],
	'search_fields' => [
		'meta' => [
			'display_name',
			'first_name',
			'last_name',
			'description',
			'expertise',
			'user_points',
			'city',
			'soft'
		],
	],
	'ep_integrate' => true,
]);

Error:

{ "error": { "root_cause": [ { "type": "query_shard_exception", "reason": "No mapping found for [title] in order to sort on", "index_uuid": "IV7U_v67T0-abeNgmVWPfw", "index": "devsite-user" } ], "type": "search_phase_execution_exception", "reason": "all shards failed", "phase": "can_match", "grouped": true, "failed_shards": [ { "shard": 0, "index": "devsite-user", "node": "-vua6l6HSJesVPT9PmU8uA", "reason": { "type": "query_shard_exception", "reason": "No mapping found for [title] in order to sort on", "index_uuid": "IV7U_v67T0-abeNgmVWPfw", "index": "devsite-user" } } ] }, "status": 400 }

  • WordPress version: 6.0.2
  • ElasticPress version: 4.3.0
  • Elasticsearch version: 7.10.2
  • Local
  • Post Search, Autosuggest, Users, Protected Content, Related Posts, Facets, Terms, Custom Search Results

Separate - works 'orderby' => 'meta.user_points.long' or 'orderby' => 'relevance'

ws256 avatar Sep 20 '22 07:09 ws256

Hi @ws256 ,

The reason you get that is because there's no field mapping "title" for users. Try something like:

'orderby' => 'display_name meta.user_points.long'

And this should work.

Probably worth changing in the docs to avoid confusions.

oscarssanchez avatar Sep 28 '22 19:09 oscarssanchez

Hi @oscarssanchez,

'orderby' => 'display_name meta.user_points.long' In this case, there are no errors 400. But the issuance is not at all what you need. Sorting on the numerical field meta.user_points.long does not occur

Even if you use it 'orderby' => 'display_name relevance', The only James Bond is on the 3rd page page. The results are no rrelevant.

In order for the search results to match the search query, you should use only one sorting option 'orderby' => 'relevance'

The other sorting options do not work if you use two keys.

'orderby' => 'meta.user_points.long'

or sorting by numeric field, it also works only if there is one key

ws256 avatar Sep 28 '22 20:09 ws256