The OpenSearch Backend

The OpenSearch backend is the main class for interacting with OpenSearch. It is a subclass of the BaseEngine class and provides the following methods:

class django_haystack_opensearch.haystack.OpenSearchSearchBackend(connection_alias: str, **connection_options: Any)

Bases: BaseSearchBackend

OpenSearch backend for django-haystack using opensearch-py.

This backend is compatible with OpenSearch 3.3 and maintains API compatibility with the elasticsearch7_backend while using opensearch-py instead of elasticsearch.

__init__(connection_alias: str, **connection_options: Any)

Initialize OpenSearch backend with opensearch-py client.

Parameters:
  • connection_alias – The alias of the connection.

  • **connection_options – The connection options.

build_schema(fields: dict[str, Any]) tuple[str, dict[str, Any]]

Build the schema/mapping for the index.

Parameters:

fields – The fields to build the schema for.

Returns:

A tuple with the content field name and the mapping.

build_search_kwargs(query_string: str, sort_by: list[tuple[str, str]] | None = None, start_offset: int = 0, end_offset: int | None = None, fields: str | list[str] | set[str] = '', highlight: bool = False, facets: dict[str, Any] | None = None, date_facets: dict[str, Any] | None = None, query_facets: list[tuple[str, str]] | None = None, narrow_queries: set[str] | None = None, spelling_query: str | None = None, within: dict[str, Any] | None = None, dwithin: dict[str, Any] | None = None, distance_point: dict[str, Any] | None = None, models: Collection[Model] | None = None, limit_to_registered_models: bool | None = None, result_class: type[SearchResult] | None = None, **extra_kwargs: dict[str, Any]) dict[str, Any]

Build search kwargs for OpenSearch query.

Parameters:

query_string – The query string to build the search kwargs for.

Keyword Arguments:
  • sort_by – The sort by configuration.

  • start_offset – The start offset.

  • end_offset – The end offset.

  • fields – The fields to build the search kwargs for.

  • highlight – The highlight configuration.

  • facets – The facets to build the search kwargs for.

  • date_facets – The date facets to build the search kwargs for.

  • query_facets – The query facets to build the search kwargs for.

  • narrow_queries – The narrow queries to build the search kwargs for.

  • spelling_query – The spelling query to build the search kwargs for.

  • within – The within filter to build the search kwargs for.

  • dwithin – The dwithin filter to build the search kwargs for.

  • distance_point – The distance point to build the search kwargs for.

  • models – The models to build the search kwargs for.

  • limit_to_registered_models – Whether to limit the models to registered models.

  • result_class – The result class to build the search kwargs for.

  • extra_kwargs – The extra kwargs to build the search kwargs for.

Returns:

A dictionary with the search kwargs.

Raises:

ImproperlyConfigured – If the connection alias is not configured.

clear(models: Iterable[Any] | None = None, commit: bool = True) None

Clears the backend of all documents/objects for a collection of models.

Parameters:
  • models – The models to clear.

  • commit – Whether to commit the changes.

Raises:

TransportError – If the index deletion fails.

extract_file_contents(file_obj: Any) dict[str, Any] | None

Extract text and metadata from a binary file using OpenSearch ingest-attachment.

Parameters:

file_obj – A file-like object containing the binary data.

Returns:

A dictionary with ‘contents’ and ‘metadata’, or None if extraction fails.

get_facet_fieldname(field_name: str) str

Return the correct backend field name for faceting/filtering.

For text fields, this returns the ‘.keyword’ subfield. For other fields, it returns the field name itself.

Parameters:

field_name – The field name to get the facet fieldname for.

Returns:

The backend field name for faceting.

more_like_this(model_instance: Model, additional_query_string: str | None = None, start_offset: int = 0, end_offset: int | None = None, models: Collection[Model] | None = None, limit_to_registered_models: bool | None = None, result_class: type[SearchResult] | None = None, **kwargs)

Find documents similar to the given model instance.

Parameters:

model_instance – The model instance to find similar documents for.

Keyword Arguments:
  • additional_query_string – The additional query string to filter the documents.

  • start_offset – The start offset.

  • end_offset – The end offset.

  • models – The models to find similar documents for.

  • limit_to_registered_models – Whether to limit the models to registered models.

  • result_class – The result class to use for the search results.

  • kwargs – Additional keyword arguments to pass to the search.

Returns:

A list of search results.

remove(obj_or_string: Any, commit: bool = True) None

Remove a document from the index.

Parameters:
  • obj_or_string – The object or string to remove.

  • commit – Whether to commit the changes.

Raises:

TransportError – If the removal fails.

search(query_string, *args, **kwargs)
setup() None

Setup the index and mappings.

Raises:

TransportError – If the index creation or mapping update fails.

update(index: str, iterable: Iterable[Any], commit: bool = True) None

Update the backend with documents from the given SearchIndex.

Parameters:
  • index – The index to update.

  • iterable – The iterable of objects to update.

  • commit – Whether to commit the changes.

Raises:
  • TransportError – If the update fails.

  • Exception – If an unexpected error occurs.

DEFAULT_FIELD_MAPPING: ClassVar[dict[str, Any]] = {'analyzer': 'snowball', 'type': 'text'}
DEFAULT_SETTINGS: ClassVar[dict[str, Any]] = {'settings': {'analysis': {'analyzer': {'edgengram_analyzer': {'filter': ['haystack_edgengram', 'lowercase'], 'tokenizer': 'standard'}, 'ngram_analyzer': {'filter': ['haystack_ngram', 'lowercase'], 'tokenizer': 'standard'}}, 'filter': {'haystack_edgengram': {'max_gram': 15, 'min_gram': 2, 'type': 'edge_ngram'}, 'haystack_ngram': {'max_gram': 4, 'min_gram': 3, 'type': 'ngram'}}}, 'index': {'max_ngram_diff': 2}}}
FIELD_MAPPINGS: ClassVar[dict[str, Any]] = {'boolean': {'type': 'boolean'}, 'date': {'type': 'date'}, 'datetime': {'type': 'date'}, 'edge_ngram': {'analyzer': 'edgengram_analyzer', 'type': 'text'}, 'float': {'type': 'float'}, 'integer': {'type': 'long'}, 'location': {'type': 'geo_point'}, 'long': {'type': 'long'}, 'ngram': {'analyzer': 'ngram_analyzer', 'type': 'text'}}
RESERVED_CHARACTERS = ('\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/')
RESERVED_WORDS = ('AND', 'NOT', 'OR', 'TO')
class django_haystack_opensearch.haystack.OpenSearchSearchEngine(using=None)

Bases: BaseEngine

OpenSearch search engine for django-haystack.

backend

The backend for the search engine.

alias of OpenSearchSearchBackend

query

The query for the search engine.

alias of OpenSearchSearchQuery

class django_haystack_opensearch.haystack.OpenSearchSearchQuery(using='default')

Bases: BaseSearchQuery

OpenSearch search query class.

build_query_fragment(field: str, filter_type: str, value: Any) str

Generates a query fragment from a field, filter type and a value.

Parameters:
  • field – The field to build the query fragment for.

  • filter_type – The filter type to build the query fragment for.

  • value – The value to build the query fragment for.

Returns:

A query fragment.

matching_all_fragment() str

Generate the query that matches all documents.

Returns:

The query that matches all documents.