Search code examples
weaviate

Sort Weaviate results based on number field


We are using Weaviate to serve e-commerce results.

Our Weaviate database stores all the products we sell.

Based on the customer and the search term we create a vector and use this to query the database. This property is called search_engine_query_vector.

For example if a customer has a habit of buying expensive products and searches for "TV" the system will most likely create a vector which is "closer" to the more expensive TVs in the database. So their first page of results is the most expensive TVs.

While this works well 99% of the time we also want ppl to be able to sort based on price.

For this we will send a query to Weaviate, where we only return products which are close to our vector(It is assumed this is all the TVs). like below:

client.query.get("Product", ["sku", "responseBody", "_additional { certainty }",
                                         "stores { ...on Store {storeId salesPrice additionalResponseBody}}"]).with_near_vector(
        {"vector": search_engine_query_vector, "similarity": TV_CUTOFF})
    .limit(10)
    .sort_base_on_price()

My question is there functionality in the api analogous to sort_base_on_price? you can assume price is a number field in the schema.


Solution

  • Great to hear you're working with Weaviate for an e-commerce solution. Weaviate added an initial version of sorting functionality in version 1.13!

    With the Weaviate version 1.13.0 we also released the python-client v3.5.0 that introduced this functionality too. You can find the needed method documentation for python here or for other clients here!

    For your use case, you could also try the following GraphQL query in the Weaviate Console:

    {
      Get {
        Product(nearVector :{
          vector:[search_engine_query_vector],
          certainty: TV_CUTOFF
          } 
          sort:{
            order:desc,
            path:["price_field"]
          }
          limit: 10
        ) {
          sku
          responseBody
          stores { ...on Store {
            storeId 
            salesPrice 
            additionalResponseBody
          }
          _additional {
            certainty
          }
        }
      }
    }