1

When ordering results from Azure Search, the sort order of the results is that string values that start with numbers or special characters come before string values that start with letters.

As best I can tell, the default ordering rules for the first letter of the order-by field is:

! ” # $ % & ' () * + , - ./. 0 1 2 3 4 5 6 7 8 9. :;< = >? @. A B C D E F G H I J K L M N...

In order to fulfill business requirements, I need a custom ordering of the resulting values like the following:

A B C D E F G H I J K L M N.! ” # $ % & ' () * + , - ./. 0 1 2 3 4 5 6 7 8 9. :;< = >? @. ...

This sort order is desired to display product descriptions that start with letters before product descriptions that start with numbers or special characters.

Is there a way to customize the default ordering in Azure Search?

If not, is there some other way to accomplish this business requirement? Perhaps somehow weighting product descriptions that start with letters to have a more weight than those that don't... but I think that would have to happen when the data is loaded into the search repository rather than when retrieving the data. I'd prefer to be able to customize ordering when retrieving search results so it could be applied to any field.

PoorInRichfield
  • 1,436
  • 2
  • 19
  • 29
  • key ascii of '!' is 33 while 'A' is 65 and 'a' is '97'.I can't think on a way to achieve that. – Thiago Custodio May 15 '20 at 16:53
  • Yeah, even without asking for magic from the Azure Search Team, I'm not quite sure how one would go about this level of ordering customization! – PoorInRichfield May 15 '20 at 17:02
  • 1
    My approach would be have a secondary field without special chars only for ordering – Thiago Custodio May 15 '20 at 17:18
  • While I like the idea, Thiago, a new ordering field pushes the ordering logic onto the business instead of solving the issue with code. I.e., some business user is going to have to come-up with alternate descriptions for each product rather than having code 'just figure it out'. – PoorInRichfield May 15 '20 at 18:27
  • no, you can copy the content of each field you will enable ordering and use a analyzer that removes special chars, when ordering, just specify the copy field rather than original. No interaction on user side. – Thiago Custodio May 15 '20 at 18:31
  • Perhaps my original posting isn't clear... the business has a very specific order requirement (second sample) that puts alpha characters first, a select group of special characters second, numeric third, followed by some more special characters :-/ – PoorInRichfield May 15 '20 at 18:43
  • I think you're only solution is to order it using LINQ in the backend. – Thiago Custodio May 15 '20 at 18:48

1 Answers1

0

Here's the solution I've come-up with so far that should meet the needs of the business without requiring them to do any extra work. Constructive criticism welcome.

Note that I have two services involved in implementing Azure Search: An Azure Function that loads the search index data from a Cosmos database and a web API that is used to query the search index.

  1. [Azure Function] Create a custom field in the search index with a name like "defaultOrderBy" and of type "Edm.Int32". This field will be used as the default order-by field and will not be retrievable via a search query.

  2. [Azure Function] Create a key-value pair collection in a configuration or settings file that assigns each order-by character a numeric "weight".

For example:

A = 0
B = 2
C = 4

...

! = 84
" = 86

...

0 = 112
1 = 114

...
  1. [Azure Function] When loading the search index data, the new "defaultOrderBy" column will be populated with the "weight" equivalent of the first letter in the product description field. For example, If the product description is "10-Day Super Fantastic Chewable Vitamins", then the first character is "1" which will get converted to a weight of 114 that is saved in the "defaultOrderBy" index field.

  2. [Web API] When calling the Search API to get a list of products, the default order-by will be the new "defaultOrderBy" column. Because it only contains numbers that represent the weight for our custom search ordering, the ordering should work as desired.

PoorInRichfield
  • 1,436
  • 2
  • 19
  • 29