document

class opensearchpy.helpers.document.Document(meta=None, **kwargs)[source]

Bases: ObjectBase

Model-like class for persisting documents in opensearch.

Parameters:
  • meta (Any) –

  • kwargs (Any) –

__repr__()[source]

Return repr(self).

Return type:

str

delete(using=None, index=None, **kwargs)[source]

Delete the instance in opensearch.

Parameters:
  • index (Any | None) – opensearch index to use, if the Document is associated with an index this can be omitted.

  • using (Any | None) – connection alias to use, defaults to 'default'

  • using

  • index

  • kwargs (Any) –

Return type:

Any

Any additional keyword arguments will be passed to OpenSearch.delete unchanged.

classmethod exists(id, using=None, index=None, **kwargs)[source]

check if exists a single document from opensearch using its id.

Parameters:
  • id (Any) – id of the document to check if exists

  • index (Any | None) – opensearch index to use, if the Document is associated with an index this can be omitted.

  • using (Any | None) – connection alias to use, defaults to 'default'

  • id

  • using

  • index

  • kwargs (Any) –

Return type:

Any

Any additional keyword arguments will be passed to OpenSearch.exists unchanged.

classmethod get(id, using=None, index=None, **kwargs)[source]

Retrieve a single document from opensearch using its id.

Parameters:
  • id (Any) – id of the document to be retrieved

  • index (Any | None) – opensearch index to use, if the Document is associated with an index this can be omitted.

  • using (Any | None) – connection alias to use, defaults to 'default'

  • id

  • using

  • index

  • kwargs (Any) –

Return type:

Any

Any additional keyword arguments will be passed to OpenSearch.get unchanged.

classmethod init(index=None, using=None)[source]

Create the index and populate the mappings in opensearch.

Parameters:
  • index (Any | None) –

  • using (Any | None) –

Return type:

None

classmethod mget(docs, using=None, index=None, raise_on_error=True, missing='none', **kwargs)[source]

Retrieve multiple document by their id’s. Returns a list of instances in the same order as requested.

Parameters:
  • docs (Any) – list of id’s of the documents to be retrieved or a list of document specifications as per https://opensearch.org/docs/latest/opensearch/rest-api/document-apis/multi-get/

  • index (Any | None) – opensearch index to use, if the Document is associated with an index this can be omitted.

  • using (Any | None) – connection alias to use, defaults to 'default'

  • missing (str) – what to do when one of the documents requested is not found. Valid options are 'none' (use None), 'raise' (raise NotFoundError) or 'skip' (ignore the missing document).

  • docs

  • using

  • index

  • raise_on_error (bool) –

  • missing

  • kwargs (Any) –

Return type:

Any

Any additional keyword arguments will be passed to OpenSearch.mget unchanged.

save(using=None, index=None, validate=True, skip_empty=True, return_doc_meta=False, **kwargs)[source]

Save the document into opensearch. If the document doesn’t exist it is created, it is overwritten otherwise. Returns True if this operations resulted in new document being created.

Parameters:
  • index (Any | None) – opensearch index to use, if the Document is associated with an index this can be omitted.

  • using (Any | None) – connection alias to use, defaults to 'default'

  • validate (bool) – set to False to skip validating the document

  • skip_empty (bool) – if set to False will cause empty values (None, [], {}) to be left on the document. Those values will be stripped out otherwise as they make no difference in opensearch.

  • return_doc_meta (bool) – set to True to return all metadata from the update API call instead of only the operation result

  • using

  • index

  • validate

  • skip_empty

  • return_doc_meta

  • kwargs (Any) –

Return type:

Any

Any additional keyword arguments will be passed to OpenSearch.index unchanged.

:return operation result created/updated

classmethod search(using=None, index=None)[source]

Create an Search instance that will search over this Document.

Parameters:
  • using (Any | None) –

  • index (Any | None) –

Return type:

Any

to_dict(include_meta=False, skip_empty=True)[source]

Serialize the instance into a dictionary so that it can be saved in opensearch.

Parameters:
  • include_meta (bool) – if set to True will include all the metadata (_index, _id etc). Otherwise just the document’s data is serialized. This is useful when passing multiple instances into opensearchpy.helpers.bulk.

  • skip_empty (bool) – if set to False will cause empty values (None, [], {}) to be left on the document. Those values will be stripped out otherwise as they make no difference in opensearch.

  • include_meta

  • skip_empty

Return type:

Any

update(using=None, index=None, detect_noop=True, doc_as_upsert=False, refresh=False, retry_on_conflict=0, script=None, script_id=None, scripted_upsert=False, upsert=None, return_doc_meta=False, **fields)[source]

Partial update of the document, specify fields you wish to update and both the instance and the document in opensearch will be updated:

doc = MyDocument(title='Document Title!')
doc.save()
doc.update(title='New Document Title!')
Parameters:
  • index (Any | None) – opensearch index to use, if the Document is associated with an index this can be omitted.

  • using (Any | None) – connection alias to use, defaults to 'default'

  • detect_noop (bool) – Set to False to disable noop detection.

  • refresh (bool) – Control when the changes made by this request are visible to search. Set to True for immediate effect.

  • retry_on_conflict (int) – In between the get and indexing phases of the update, it is possible that another process might have already updated the same document. By default, the update will fail with a version conflict exception. The retry_on_conflict parameter controls how many times to retry the update before finally throwing an exception.

  • doc_as_upsert (bool) – Instead of sending a partial doc plus an upsert doc, setting doc_as_upsert to true will use the contents of doc as the upsert value

  • return_doc_meta (bool) – set to True to return all metadata from the index API call instead of only the operation result

  • using

  • index

  • detect_noop

  • doc_as_upsert

  • refresh

  • retry_on_conflict

  • script (Any | None) –

  • script_id (Any | None) –

  • scripted_upsert (bool) –

  • upsert (Any | None) –

  • return_doc_meta

  • fields (Any) –

Return type:

Any

:return operation result noop/updated