> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Configure searchable attributes the right way

> Learn how to select, prioritize, and set up searchable attributes for your Algolia index.

export const SearchQuery = () => <Tooltip tip="The text users enter into a search box. In the Search API, this corresponds to the query parameter. A search query is often used with filters, facets, and other parameters, but these aren't part of the query text itself.">
    search query
  </Tooltip>;

export const Records = () => <Tooltip tip="A record is a searchable object in an Algolia index. Each record consists of named attributes." cta="Algolia records" href="/doc/guides/sending-and-managing-data/prepare-your-data#algolia-records">
    records
  </Tooltip>;

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

export const Filter = () => <Tooltip tip="A filter is a condition that limits which records Algolia returns. Filters often use one or more facet-value pairs, such as brand:Apple AND color:red. You can also filter by numeric values, dates, tags, booleans, or geographic constraints." cta="Filtering" href="/doc/guides/managing-results/refine-results/faceting">
    filter
  </Tooltip>;

Setting searchable attributes is **one of the most important aspects of configuring your search** and one of the first steps you need to take. It goes much further than telling Algolia which attributes to search for, and it also defines which ones to look at first and what strategy to adopt. A small change in setting searchable attributes could lead to entirely different results. For that reason, it's crucial to understand how it works.

Besides deciding [which attributes Algolia should search](/doc/guides/managing-results/must-do/searchable-attributes), there are two aspects you can control with searchable attributes:

* The [relative importance](#attribute-order) of each attribute
* Whether the [position of the match](#word-position) in the attribute matters

## Attribute order

While setting searchable attributes, you can order them, making some more relevant than others.

Knowing how to order your searchable attributes can be challenging when you have several.
One method is to **compare them in pairs**, each attribute with the previous one, and move them around accordingly.
This is similar to [insertion sorting](https://wikipedia.org/wiki/Insertion_sort).

For example, suppose you have the following attributes in the <Records /> of a "movies" <Index />:

* `director`
* `cast`
* `title`
* `genres`
* `plot_summary`

1. Compare the first two: `director` and `cast`.
   If you think `cast` should come before `director` (for example, you want to see movies with Clint Eastwood before movies directed by Clint Eastwood), move `cast` to the first place.
2. Compare `director` and `title` and decide that the movie's name is more important than the director's.
   That means you'd move `title` to the second place and compare it with the previous one (`cast`).
3. Prioritize a match on an actor's name or a movie title.
   If all records in your index represent movies,
   it may make sense to prioritize `title`,
   so you'd move it first.
   Then, because you don't have any more attributes to compare `title` to, you'd move on to `genres`.

The benefit is that **you're making a thorough, granular comparison of each attribute with the others** and considering your use case for every pair.
This method is much more reliable than doing a global, intuitive sort by what seems more important.

### Searchable attributes with different priorities

**The order of searchable attributes directly affects search relevance.** Attributes higher in the list are more relevant than attributes further down. Therefore, you should set attributes higher in the searchable attributes list when their content is more relevant to your users.

Consider a movie database website where users can search movies by title, actors, and director. You may be tempted to prioritize actors before directors Yet, consider the following dataset:

```json JSON icon=braces theme={"system"}
[
  {
    "title": "Mystic River",
    "director": "Clint Eastwood",
    "actors": ["Sean Penn", "Tim Robbins", "Kevin Bacon", "Laurence Fishburne"]
  },
  {
    "title": "In the Line of Fire",
    "director": "Wolfgang Petersen",
    "actors": ["Clint Eastwood", "John Malkovich", "Renee Russo"]
  },
  {
    "title": "Invictus",
    "director": "Clint Eastwood",
    "actors": ["Morgan Freeman", "Matt Damon"]
  },
  {
    "title": "Million Dollar Baby",
    "director": "Clint Eastwood",
    "actors": ["Clint Eastwood", "Hilary Swank", "Morgan Freeman"]
  }
]
```

For a movie search, setting the `title` as the first searchable attribute makes sense. But what if someone searched for "Clint Eastwood"? Would they primarily be looking for movies with Clint Eastwood, by Clint Eastwood, or both? In other words, how should you rank the `director` and `actors` attributes?

By putting `director` first, movies that Clint Eastwood directed would come before those where he appeared as an actor. This may or may not be desirable. For example, if a user types "jane", do you want to first display movies with director Jane Campion or actress Jane Fonda?

There's no one-size-fits-all approach, and the strategy depends on your use case. **Defining a priority order in your searchable attributes isn't trivial**. It can vary depending on your data, how it's structured, what your users search for, and what they expect as results.

### Searchable attributes with the same priority

Sometimes, setting an attribute before or after another doesn't make sense because you want them to be considered the same.
Here's another dataset, this time with records representing movies, actors, and directors:

```json JSON icon=braces theme={"system"}
[
  {
    "movie_title": "John Wick",
    "cast": ["Keanu Reeves", "Michael Nyqvist", "Alfie Allen"]
  },
  {
    "actor_name": "John Cleese",
    "filmography": [
      "Monty Python and the Holy Grail",
      "Life of Brian",
      "Harry Potter and the Philosopher's Stone"
    ]
  },
  {
    "director_name": "John Carpenter",
    "filmography": ["Halloween", "The Thing", "Escape from New York"]
  }
]
```

If someone typed `John`, it wouldn't necessarily make more sense to show movies with *John* in the title than actors or directors named John. Instead, you may want to rely on other criteria, like popularity, to decide what comes first.

In this case, it makes sense to set `movie_title`, `actor_name`, and `director_name` at the same level.
This way, if someone types `John`, the engine considers all three records equal in the [attribute ranking criterion](/doc/guides/managing-results/relevance-overview/in-depth/ranking-criteria#attribute), and the engine would go to the next [ranking criterion](/doc/guides/managing-results/relevance-overview/in-depth/ranking-criteria) to try and break the tie.

<Info>
  Attributes with the same priority are always [unordered](#word-position).
</Info>

## Word position

Searchable attributes can be ordered or unordered:

* **ordered** means that the engine considers matches at the beginning of an attribute more important than matches in the middle or the end. When an attribute is ordered, the earlier a match occurs, the higher the engine ranks it.
* **unordered** means that the position of the match within the attribute doesn't affect ranking.

<Tip>
  You should usually set the attribute to be unordered since the position of the matching word in the attribute often doesn't matter.
</Tip>

Consider the following dataset:

```json JSON icon=braces theme={"system"}
[
  {
    "title": "Avengers: Infinity War",
    "cast": [
      "Robert Downey Jr.",
      "Chris Hemsworth",
      "Mark Ruffalo",
      "Chris Evans",
      "Scarlett Johansson"
    ]
  },
  {
    "title": "World War Z",
    "cast": [
      "Brad Pitt",
      "Mireille Enos",
      "Daniella Kertesz",
      "James Badge Dale",
      "Ludi Boeken"
    ]
  },
  {
    "title": "War of the Worlds",
    "cast": [
      "Tom Cruise",
      "Dakota Fanning",
      "Justin Chatwin",
      "Miranda Otto",
      "Tim Robbins"
    ]
  },
  {
    "title": "Lost in Translation",
    "cast": ["Scarlett Johansson", "Bill Murray", "Akiko Takeshita"]
  },
  {
    "title": "North",
    "cast": [
      "Elijah Wood",
      "Jason Alexander",
      "Julia Louis-Dreyfus",
      "Marc Shaiman",
      "Scarlett Johansson"
    ]
  }
]
```

If users type "war," it wouldn't make sense to return "World War Z" before "Avengers: Infinity War" because the string happens to appear earlier in the title.
The ranking should rely on other criteria like popularity or reviews, so it makes sense to set `title` as `unordered`.

Things are different with the `cast` attribute.
If users type "Scarlett Johansson," chances are they would be more interested in movies where Scarlett Johansson has a leading role than movies where she played small parts.

Assuming that the order of actors and actresses in the `cast` attribute reflects role size, keeping `cast` ordered makes more sense.

### Set unordered or ordered with the API

You can set attributes as unordered or ordered with [`searchableAttributes`](/doc/api-reference/api-parameters/searchableAttributes) when indexing.
If unordered isn't set for an attribute, it will default to ordered when set by the API.
To run the code examples on this page, [install the latest API client](/doc/libraries/sdk/install).

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SetSettingsAsync(
    "INDEX_NAME",
    new IndexSettings
    {
      SearchableAttributes = new List<string> { "unordered(title)", "cast" },
    }
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.setSettings(
    indexName: "INDEX_NAME",
    indexSettings: IndexSettings(
      searchableAttributes: [
        "unordered(title)",
        "cast",
      ],
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SetSettings(client.NewApiSetSettingsRequest(
    "INDEX_NAME",
    search.NewEmptyIndexSettings().SetSearchableAttributes(
      []string{"unordered(title)", "cast"})))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  UpdatedAtResponse response = client.setSettings(
    "INDEX_NAME",
    new IndexSettings().setSearchableAttributes(Arrays.asList("unordered(title)", "cast"))
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.setSettings({
    indexName: 'theIndexName',
    indexSettings: { searchableAttributes: ['unordered(title)', 'cast'] },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.setSettings(
      indexName = "INDEX_NAME",
      indexSettings = IndexSettings(searchableAttributes = listOf("unordered(title)", "cast")),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->setSettings(
      'INDEX_NAME',
      ['searchableAttributes' => [
          'unordered(title)',

          'cast',
      ],
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.set_settings(
      index_name="INDEX_NAME",
      index_settings={
          "searchableAttributes": [
              "unordered(title)",
              "cast",
          ],
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.set_settings(
    "INDEX_NAME",
    Algolia::Search::IndexSettings.new(searchable_attributes: ["unordered(title)", "cast"])
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.setSettings(
      indexName = "INDEX_NAME",
      indexSettings = IndexSettings(
        searchableAttributes = Some(Seq("unordered(title)", "cast"))
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response = try await client.setSettings(
      indexName: "INDEX_NAME",
      indexSettings: IndexSettings(searchableAttributes: ["unordered(title)", "cast"])
  )
  ```
</CodeGroup>

### Set unordered or ordered from the dashboard

1. Select the **Search** product icon on your dashboard.
2. On the [**Index** page](https://dashboard.algolia.com/explorer/browse), select your index.
3. Click the **Configuration** tab.
4. Go to the **Searchable Attributes** section.
   For each searchable attribute, consider whether it should be [ordered or unordered](#word-position).
5. Save your changes.

<Info>
  When using the dashboard, the UI defaults to setting your attribute as unordered.
</Info>

## Successful strategies

Setting searchable attributes is more an art than a science and highly depends on your use case.
There's no magic formula that works for everyone. However, the following tips will get you started.

### Keep searchable attributes to a minimum

As a rule of thumb, **the more searchable attributes, the noisier the search**.
It may be tempting to add searchable attributes to maximize matches.
Yet, this reduces search relevance by creating *noise* (littering good results with not-so-good ones).

Instead, be conservative with your searchable attributes and **focus on what your users tend to search for**.
Some attributes, like image URLs or review scores, shouldn't be searchable because they don't have textual value.
Some other attributes, like the plot summary of a movie, might look interesting but could be unnecessary.
How often do you find a movie by searching for the synopsis? Probably far less often than with the title or lead actors.

The same advice goes for the length of the attribute values.
Suppose you have a "movies" index where the records have an `actors` attribute.
If this attribute has the entire cast, it's less efficient than if it only had the leading roles.
Most users would probably look at leading roles when searching for an actor.

### Choose the appropriate attribute order

Knowing how to order your searchable attributes can be challenging when you have several.
One method is to **compare them in pairs**, each attribute with the previous one, and move them around accordingly.
The process is like [insertion sorting](https://wikipedia.org/wiki/Insertion_sort).

For example, suppose you have the following attributes in the records of a "movies" index:

* `director`
* `cast`
* `title`
* `genres`
* `plot_summary`

You would:

1. Compare the first two: `director` and `cast`. If you think `cast` should come before `director` (for example, you want to see movies with Clint Eastwood before movies directed by Clint Eastwood), move `cast` to the first place.
2. Compare `director` and `title` and decide that the movie's name is more important than the director's. That means you'd move `title` to the second place and compare it with the previous one (`cast`).
3. Prioritize a match on an actor's name or a movie title. If all records in your index represent movies, it may make sense to prioritize `title`, so you'd move it first. Then, because you don't have any more attributes to compare `title` to, you'd move on to `genres`.

The benefit is that **you're making a thorough, granular comparison of each attribute with the others** and considering your use case for every pair. This method is much more reliable than doing a global, intuitive sort by what seems more important.

### Where to put filters?

Sometimes it's better to prioritize filters over all other attributes.
This might not seem intuitive at first, as you may think users tend to search by attributes like `name` or `title`.

Suppose you have a "movies" index where the records have a `genre` attribute with values like "crime" and "action".
Films fall into these categories, but there are also films whose titles and descriptions use these exact words,
for example, "Crime Doesn't Pay", "Last Action Hero".

In that case, you can decide that when a user searches for words that are more like genres,
it's best to search <Filter /> attributes before the title.
This is guaranteed to return all crime movies whenever someone types in `crime`, regardless of the title.

Making this decision when setting up searchable attributes can affect your results.

### Change your searchable attributes strategy

Be careful before changing your searchable attributes strategy because this affects your entire index.
Don't make decisions based on a single <SearchQuery />.
Instead, experiment with several queries close to what your users search for (use [Algolia analytics](/doc/guides/search-analytics/overview) to explore their search habits).

## See also

* [Fuzzy search and fuzzy matching (blog)](https://www.algolia.com/blog/engineering/discover-what-fuzzy-search-is-with-fuzzy-matching/)
