Duplicate fields in sorting - ELASTIC 6.0

Before version 6.0 of the elastic, it was possible to send fields in the repeated ordering that was not released exception, after the update, every time there is a repeated field, an exception is thrown, preventing the query correctly.

What is the best solution for the type of case that has default orders when the template is created and later the user can select a field equal to the default?

 $template['sort'] = [
                        'image' => 'desc',
                        'available_for_purchase' => 'desc',
                        '{{sortField}}' => '{{sortOrder}}',
                        '_score' => 'desc',
            ];

In that {{sortField}} you can happen to come one of the already defined fields, which in case would have made an error. I thought of storing the template, searching the template before the query, organizing this "sort" position by removing duplicates with PHP for example and then executing the query using "SOURCE.QUERY", but then it would not make sense to use the template already that at any moment I'm using the template itself.

Would you have any idea how to use the template in this case?

  1. Note: I have tried to send the ID_TEMPLATE together with the separate and exclusive SORT, but it does not work.

  2. Note: I already tried to send a json inside the PARAMS symbolizing the fields in the order to be applied at the moment of the query, for example:

     {
     	"source": {
     		"id": "any_id",
     		"sort": "{{sortValue}}"
     	},
     	"params": {
     		"query": "text",
     		"sortValue": [{
     			"_score": "asc"
     		}]
     	}
     }
1 Like

The solution found was to create within each ordered field a field only for ordering, as for example:

               'available_for_purchase' =>
                    [
                        'type' => 'keyword',
                        'fields' => [
                            'sort' => [
                                'type' => 'keyword',
                            ]
                        ]
                    ],

Therefore, the user would never select a field directly "available_for_purchase", but rather, "available_for_purchase.sort" which has the same value and the ordering will work perfectly.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.