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

# Options

> List of options to configure indexing to Algolia.

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>;

## Custom object IDs

You can choose which field is used as the object ID.
The field should be unique and can be a string or integer.
By default, the integration uses the `pk` field of the model.

```python Python icon=code theme={"system"}
class ArticleIndex(AlgoliaIndex):
    custom_objectID = "post_id"
```

## Custom index name

You can customize the <Index /> name.
By default, the index name will be the name of the model class.

```python Python icon=code theme={"system"}
class ContactIndex(algoliaindex):
    index_name = "Enterprise"
```

## Field preprocessing and related objects

If you want to process a field before indexing it,
for example, capitalizing a contact's name,
or if you want to index an attribute of a [related object](https://docs.djangoproject.com/en/1.11/ref/models/relations/),
you need to define **proxy methods** for these fields.

### Models

```python Python icon=code theme={"system"}
class Account(models.Model):
    username = models.CharField(max_length=40)
    service = models.CharField(max_length=40)


class Contact(models.Model):
    name = models.CharField(max_length=40)
    email = models.EmailField(max_length=60)
    accounts = models.ManyToManyField(Account)

    def account_names(self):
        return [str(account) for account in self.accounts.all()]

    def account_ids(self):
        return [account.id for account in self.accounts.all()]
```

### Index

```python Python icon=code theme={"system"}
from algoliasearch_django import AlgoliaIndex


class ContactIndex(AlgoliaIndex):
    fields = (
        "name",
        "email",
        "company",
        "address",
        "city",
        "county",
        "state",
        "zip_code",
        "phone",
        "fax",
        "web",
        "followers",
        "account_names",
        "account_ids",
    )

    settings = {
        "searchableAttributes": [
            "name",
            "email",
            "company",
            "city",
            "county",
            "account_names",
        ]
    }
```

With this configuration, you can search for a `Contact` using its `Account` names.

You can use the associated `account_ids` at search-time to fetch more data from your
model.
**Only proxy the fields relevant for search** to keep your records' size as small as possible.

## Index settings

You can configure your index, using all of Algolia's [index settings](/doc/api-reference/api-parameters).

```python Python icon=code theme={"system"}
class ArticleIndex(AlgoliaIndex):
    settings = {
        "searchableAttributes": ["name", "description", "url"],
        "customRanking": ["desc(vote_count)", "asc(name)"],
    }
```

## Restrict indexing to a subset of your data

You can add constraints controlling if a record must be indexed or not.
`should_index` should be a callable that returns a boolean.

```python Python icon=code theme={"system"}
class Contact(models.model):
    name = models.CharField(max_length=20)
    age = models.IntegerField()

    def is_adult(self):
        return self.age >= 18


class ContactIndex(AlgoliaIndex):
    should_index = "is_adult"
```

## Multiple indices per model

You can have several indices for a single model.

<Steps>
  <Step title="Define the indices you want for a model">
    ```python Python icon=code theme={"system"}
    from django.contrib.algoliasearch import AlgoliaIndex


    class MyModelIndex1(AlgoliaIndex):
        name = "MyModelIndex1"
        # ...


    class MyModelIndex2(AlgoliaIndex):
        name = "MyModelIndex2"
        # ...
    ```
  </Step>

  <Step title="Define a meta model to aggregate the indices">
    ```python Python icon=code theme={"system"}
    class MyModelMetaIndex(AlgoliaIndex):
        def __init__(self, model, client, settings):
            self.indices = [
                MyModelIndex1(model, client, settings),
                MyModelIndex2(model, client, settings),
            ]

        def raw_search(self, query="", params=None):
            res = {}
            for index in self.indices:
                res[index.name] = index.raw_search(query, params)
            return res

        def update_records(self, qs, batch_size=1000, **kwargs):
            for index in self.indices:
                index.update_records(qs, batch_size, **kwargs)

        def reindex_all(self, batch_size=1000):
            for index in self.indices:
                index.reindex_all(batch_size)

        def set_settings(self):
            for index in self.indices:
                index.set_settings()

        def clear_objects(self):
            for index in self.indices:
                index.clear_objects()

        def save_record(self, instance, update_fields=None, **kwargs):
            for index in self.indices:
                index.save_record(instance, update_fields, **kwargs)

        def delete_record(self, instance):
            for index in self.indices:
                index.delete_record(instance)
    ```
  </Step>

  <Step title="Register index with the model">
    Register the `AlgoliaIndex` with your `Model`:

    ```python Python icon=code theme={"system"}
    import algoliasearch_django as algoliasearch

    algoliasearch.register(MyModel, MyModelMetaIndex)
    ```
  </Step>
</Steps>

## Temporarily turn off the auto-indexing

To temporarily turn off
the auto-indexing feature,
use the `disable_auto_indexing` context decorator:

```python Python icon=code theme={"system"}
from algoliasearch_django.decorators import disable_auto_indexing

# Used as a context manager
with disable_auto_indexing():
    MyModel.save()

# Used as a decorator
@disable_auto_indexing():
my_method()

# You can also specifiy for which model you want to disable the auto-indexing
with disable_auto_indexing(MyModel):
    MyModel.save()
    MyOtherModel.save()
```
