Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions _search-plugins/search-relevance/query-sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ All query sets comprise one or more entries. Each entry is a JSON object contain
| :--- | :--- | :--- |
| `queryText` | String | The user query string. Required. |
| `referenceAnswer` | String | The expected or correct answer to the user query. This field is used for generating judgments, especially with large language models (LLMs). Optional. |
| Custom fields | String | Any additional field (beyond `queryText` and `referenceAnswer`) is stored as a custom field on the entry. Custom fields are available as [Mustache template variables]({{site.url}}{{site.baseurl}}/search-plugins/search-relevance/search-configurations/#using-mustache-templates) in a search configuration, referenced by field name. Optional. |

### Basic query set example

Expand Down Expand Up @@ -117,6 +118,20 @@ This format includes the `referenceAnswer` field alongside the `queryText`. It i

The `referenceAnswer` field is particularly useful when using [LLMs to generate judgments]({{site.url}}{{site.baseurl}}/search-plugins/search-relevance/judgments/). The LLM can use the reference answer as a ground truth to compare against the retrieved search results, allowing it to accurately score the relevance of the response.

### Query set with custom fields example

In addition to `queryText`, each entry can include arbitrary custom fields. These values are available as Mustache template variables in a [search configuration]({{site.url}}{{site.baseurl}}/search-plugins/search-relevance/search-configurations/#using-mustache-templates), letting a single search configuration vary its filters or parameters for each query.

#### Example query set with custom fields

```json
{"queryText": "phone", "category": "electronics"}
{"queryText": "steel", "category": "materials"}
{"queryText": "keyboard", "category": "electronics"}
```

For example, a search configuration whose query references `{{category}}` filters each query's results by the category value provided in the corresponding entry.

## Managing query sets

You can retrieve or delete query sets using the following APIs.
Expand Down
30 changes: 29 additions & 1 deletion _search-plugins/search-relevance/search-configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
:--- | :--- | :---
`name` | String | The name of the search configuration.
`description` | String | Description of the search configuration.
`query` | Object | Defines the query in OpenSearch query DSL. Use `%SearchText%` as a placeholder for the user query. Needs to be escaped.
`query` | Object | Defines the query in OpenSearch query DSL. Use the `%SearchText%` placeholder or [Mustache](https://mustache.github.io/) template variables (such as `{{queryText}}`) to substitute query set values at runtime. Needs to be escaped. For more information, see [Using Mustache templates](#using-mustache-templates).
`index` | String | The target index queried by this search configuration.
`searchPipeline` | String | Specifies an existing search pipeline. Optional.

Expand All @@ -45,6 +45,34 @@
}
```

### Using Mustache templates

Check failure on line 48 in _search-plugins/search-relevance/search-configurations.md

View workflow job for this annotation

GitHub Actions / style-job

[vale] reported by reviewdog 🐶 [OpenSearch.HeadingCapitalization] 'Using Mustache templates' is a heading and should be in sentence case. Raw Output: {"message": "[OpenSearch.HeadingCapitalization] 'Using Mustache templates' is a heading and should be in sentence case.", "location": {"path": "_search-plugins/search-relevance/search-configurations.md", "range": {"start": {"line": 48, "column": 5}}}, "severity": "ERROR"}

Instead of the `%SearchText%` placeholder, a query can use [Mustache](https://mustache.github.io/) template variables. If the `query` contains `{{`, it is rendered as a Mustache template; otherwise, the `%SearchText%` placeholder is substituted. Existing `%SearchText%` configurations continue to work unchanged.

The following variables are available when a template is rendered:

* `{{queryText}}`: The `queryText` of the current query set entry.
* Any custom field defined on the query set entry, referenced by its field name (for example, `{{category}}`). For more information, see [Query sets]({{site.url}}{{site.baseurl}}/search-plugins/search-relevance/query-sets/).

Substituted values are automatically JSON-escaped, so query text containing quotation marks or other special characters does not break the query.

Mustache partials (`{{>...}}`) are not supported and cause the query to be rejected.
{: .note}

#### Example request: Creating a search configuration that uses a Mustache template

The following request creates a search configuration that matches the user query against the `title` field and filters results by a `category` custom field provided by each query set entry:

```json
PUT _plugins/_search_relevance/search_configurations
{
"name": "mustache_filter",
"query": "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"title\":\"{{queryText}}\"}}],\"filter\":[{\"term\":{\"category\":\"{{category}}\"}}]}}}",
"description": "Title match filtered by a category custom field",
"index": "ecommerce"
}
```

## Managing search configurations

You can retrieve or delete configurations using the following APIs.
Expand Down
Loading