Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Guide] Advanced Index Actions #350

Closed
Tracked by #355
nhtruong opened this issue Apr 4, 2023 · 3 comments
Closed
Tracked by #355

[Guide] Advanced Index Actions #350

nhtruong opened this issue Apr 4, 2023 · 3 comments
Labels
CCI enhancement New feature or request good first issue Good for newcomers

Comments

@nhtruong
Copy link
Contributor

nhtruong commented Apr 4, 2023

Create guides/advanced_index_actions.md similar to the Ruby guide

You must assure that:

  • All code-blocks, when run from start to finish in order they appear in the guide, do not throw any errors. If you want to demonstrate that certain statements will throw errors, they must be wrapped in a try/catch and print out the expected error.
  • All code-blocks are properly marked with the language it's written in (i.e. ```ruby)
  • The instructions, though mostly identical to the Ruby guide, are suitable for this repo.
@nhtruong nhtruong added enhancement New feature or request good first issue Good for newcomers untriaged Need triage CCI labels Apr 4, 2023
@nhtruong nhtruong mentioned this issue Apr 4, 2023
6 tasks
@nhtruong nhtruong removed the untriaged Need triage label Apr 4, 2023
@ReinGrad
Copy link

ReinGrad commented Apr 4, 2023

Prerequisites

Before you begin, make sure you have the following:

*OpenSearch-Py library installed
*Python 3.x or later installed
*Basic understanding of OpenSearch concepts

Pattern

Indexing Documents with Advanced Features

Indexing Documents with Parent-Child Relationship
You can index documents with a parent-child relationship in OpenSearch using the parent parameter. The parent parameter allows you to specify the parent document ID for a child document.

from opensearchpy import OpenSearch

# Connect to OpenSearch
client = OpenSearch()

# Index a parent document
parent_doc = {
    'title': 'Parent Document',
    'content': 'This is the parent document.'
}
client.index(index='my_index', doc_type='_doc', document=parent_doc, id=1)

# Index a child document with a specified parent document ID
child_doc = {
    'title': 'Child Document',
    'content': 'This is a child document.',
    'parent': 1  # Specify the parent document ID
}
client.index(index='my_index', doc_type='_doc', document=child_doc)

Indexing Documents with Routing

You can specify a routing value when indexing documents in OpenSearch using the routing parameter. The routing value is used to determine which shard a document should be stored in based on the routing value and the routing configuration of the index.

from opensearchpy import OpenSearch

# Connect to OpenSearch
client = OpenSearch()

# Index a document with a specified routing value
doc = {
    'title': 'Document with Routing',
    'content': 'This document has a specified routing value.',
    'routing': 'user1'  # Specify the routing value
}
client.index(index='my_index', doc_type='_doc', document=doc, routing='user1')

Indexing Documents with Versioning

You can enable versioning for documents in OpenSearch to allow for optimistic concurrency control. The version number is used to ensure that multiple updates to the same document do not conflict.

from opensearchpy import OpenSearch

# Connect to OpenSearch
client = OpenSearch()

# Index a document with versioning enabled
doc = {
    'title': 'Document with Versioning',
    'content': 'This document has versioning enabled.',
    'version': 1  # Specify the version number
}
client.index(index='my_index', doc_type='_doc', document=doc)

@nhtruong
Copy link
Contributor Author

nhtruong commented Apr 5, 2023

@ReinGrad Thanks for putting in the work. Sorry for not being clear but you should create a PR that adds uides/advanced_index_actions.md that is VERY similar to the Ruby guide. As in, The format and instructions are the same but replace the Ruby sample code with Python Sample code.

@Nicksqain
Copy link
Contributor

@nhtruong I have made a PR to solve this issue #363

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CCI enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants