# Generative Query Rewriting - Azure AI Search

## **Overview**

Query rewriting involves modifying a user's search query to make it more effective by enhancing it with additional terms and refining the results. The search service processes the query (or a modified version) through a generative model that creates alternative query suggestions.

This technique enhances semantic ranking by addressing typos or spelling mistakes in user queries and broadening queries using synonyms.

Here’s how search with query rewriting operates:

* The user submits a query through the search parameter in the request.
    
* The search service processes the query (or its variation) using a generative model to create alternative queries.
    
* The search service uses both the original and rewritten queries to fetch search results.
    

Query rewriting is an optional capability. Without it, the search service relies solely on the original query to generate results.

---

## **Prerequisites**

To effectively follow this example, it is essential to have the following prerequisites in place:

* An Azure AI Search instance with a Basic or higher tier
    
* An index within the search instance with a semantic configuration
    
* A semantic ranker enabled on the search instance
    

Below I also explain how to enable semantic ranker and add a semantic configuration.

---

## **Enable semantic ranker**

Go to your Azure AI Search service and click on the 'Semantic ranker' from the left menu. Confirm that you have selected the free plan which has 1.000 requests per month.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1761077530059/c7fb3f2a-a12b-485f-b084-f6b4b17d62a2.png align="center")

Once you surpass the first 1,000 requests included in the free plan, an error message will notify you that your quota has been reached when you attempt the next semantic query. To continue utilizing semantic ranking after reaching the limit, upgrading to the standard plan is necessary.

---

## **Creating the semantic configuration**

Now, we need to add a semantic configuration to the index. Open the index you are using and go to 'Semantic configuration' and click on 'Create'.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1761077549575/0fd125b4-2de5-4ca7-a2fc-fe3993ea37de.png align="center")

Configure the semantic configuration similar to the following:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1761077606291/230a5d7b-c4bc-4b0b-84d8-4e190cbb5b9a.png align="center")

I’m only including the content fields here and leaving out the title and keyword fields since my index doesn’t contain a title or keywords.

---

## **Execute a search request with query rewrites**

Let's take a look at the data within my index.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1761077614667/9857219b-fc37-47bd-a7d7-986eb16484b6.png align="center")

Next, execute a search request using the parameters below:

```json
{
    "search": "buy cape",
    "semanticConfiguration": "my-semantic-cfg",
    "queryType": "semantic",
    "queryRewrites": "generative|count-5",
    "queryLanguage": "en-US",
    "debug": "queryRewrites",
    "top": 1
}
```

Let's take a closer look at the results:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1761077763992/7f419f9b-16ad-4b53-94d3-69d6122f1fac.png align="center")

We can confirm that the correct task is returned. Additionally, take note of the query rewrites included in the response. Since we enabled the "debug" property with "queryRewrites" for testing, the response contains a **@search.debug** object showing the input query and its rewrites. By setting the "queryRewrites" property to "generative|count-5," the response provides up to five query rewrites. The "inputQuery" represents the query sent to the generative model for rewriting, which may differ from the user’s original "search" query.
