Official Content

Starting with GeneXus Next edition, the Order clause supports the use of expressions to sort results in queries. 

The ability to sort using expressions in the Order clause allows you to apply complex record sorting criteria, as long as the expression can be resolved on the server.

A specific case of this functionality is the ability to sort records based on the distance between vectors represented in the Embedding data type, which allows performing semantic searches and sorting based on similarity of meaning.

Samples

Sample Order Clause with Embedding data type

In the case of the Embedding data type, you can sort records based on semantic proximity to the search term using an expression such as:

order <TrnEmbeddingAttribute>.Distance(&SearchEmbedding)

Where:

  • TrnEmbeddingAttribute: Transaction attribute that is an Embedding data type.
  • Distance: Method that calculates the distance between two embedding vectors: the TrnEmbeddingAttribute and the provided &SearchEmbedding. It returns a numeric value representing this distance, which is then used for ordering.
  • &SearchEmbedding: Variable that holds the search vector. You generate this vector from a search string using the GenerateEmbedding function.

GeneXus sorts the results based on the semantic similarity between each record and the search term. To ensure consistency of results, the &SearchEmbedding vector must be calculated using the same model and dimensions as the TrnEmbeddingAttribute. 

You can generate this vector with the GenerateEmbedding function.

&SearchEmbedding = <TrnEmbeddingAttribute>.GenerateEmbedding(&Search, &Messages)

Before executing the query, GeneXus generates a vector based on the provided search term, using the selected embeddings model. This vector is then evaluated in the Order clause to calculate the distances.

During the execution of the Order clause, GeneXus calculates the distance between the vector of each record (TrnEmbeddingAttribute) and the search vector &SearchEmbedding. The results are ordered according to this distance, placing the most similar records at the beginning.

You can use this Order clause within a For Each command to retrieve and display records sorted by semantic relevance. For example:

&SearchEmbedding = ProductEmbedding.GenerateEmbedding(&Search, &Messages)

for each Product
    Order ProductEmbedding.Distance(&SearchEmbedding)
        msg(ProductName)
Endfor

Here, the search embedding is generated first using the GenerateEmbedding function, where the &Search variable contains the user-provided search term. 

The For Each command then iterates through all Product records, using the Order clause to compare each product's ProductEmbedding with the &SearchEmbedding via the Distance method.

GeneXus automatically sorts the products based on their semantic similarity to the search term, with products having embeddings most similar to the search embedding appearing first. 

Finally, the msg function displays the ProductName of each product in this semantically sorted order.

Sample Order Clause with Expressions

The following examples show possible uses of the Order clause by applying expressions with different attribute data types:

  • order (len(ProductName))

    ProductName is an attribute of VarChar type, and len() calculates the length of the string. This expression allows you to sort the records based on the number of characters in the product name.
     
  • order (abs(ProductPrice))

    ProductPrice is an attribute of Numeric type. The abs() function is used to sort records by the absolute value of the price, useful for cases where the price includes negative values (such as discounts) and you want to sort by the magnitude of the value.
     
  • Order dow(ProductDate)

    ProductDate is an attribute of Date type. The dow( ) function extracts the day of the week from this date, allowing you to sort records by day of the week.

See Also

Server Side Functions/Methods

 

Last update: December 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant