Package: openimis-be-opensearch-reports
openIMIS Backend opensearch_reports reference moduleAdding Environmental Variables to Your BuildTo configure environmental variables for your build, include the following:OPENSEARCH_HOST- For the non-dockerized instance in a local context, set it to 0.0.0.0:9200.
For the dockerized instance, use opensearch:9200.OPENSEARCH_ADMINThis variable is used for the admin username. (default value: admin)OPENSEARCH_PASSWORDThis variable is used for the admin password. (default value: admin)How to configure documents in openimis businesss module?To configure Django ORM models with OpenSearch documents using a noSQL approach, follow these steps:Begin by creating adocuments.pyfile within your module.Inside thedocuments.pyfile, define a Document class that corresponds to your Django ORM model.
For example:fromdjango_opensearch_dsl.registriesimportregistryfromdjango_opensearch_dslimportDocument,fieldsasopensearch_fields@registry.register_documentclassBeneficiaryDocument(Document):benefit_plan=opensearch_fields.ObjectField(properties={'code':opensearch_fields.KeywordField(),'name':opensearch_fields.KeywordField(),})individual=opensearch_fields.ObjectField(properties={'first_name':opensearch_fields.KeywordField(),'last_name':opensearch_fields.KeywordField(),'dob':opensearch_fields.DateField(),})status=opensearch_fields.KeywordField(fields={'status_key':opensearch_fields.KeywordField()})classIndex:name='beneficiary'# Name of the Opensearch indexsettings={'number_of_shards':1,'number_of_replicas':0}auto_refresh=True# automatically send updates and new objects to openSearchclassDjango:model=Beneficiaryrelated_models=[BenefitPlan,Individual]fields=['id',]queryset_pagination=5000In the Django class, you can use thefieldsproperty to include all
the fields that are present in your Django ORM model.If you wish to include related fields (foreign keys), make sure to specify them
in therelated_modelsproperty of your Django class. Add all the foreign key fields
that you want to include.classDjango:model=Beneficiaryrelated_models=[BenefitPlan,Individual]fields=['id',]queryset_pagination=5000If you want to customize the default behavior of field creation, declare the fields you wish to modify
using the appropriate type. For foreign keys, use theObjectFieldtype.@registry.register_documentclassBeneficiaryDocument(Document):benefit_plan=opensearch_fields.ObjectField(properties={'code':opensearch_fields.KeywordField(),'name':opensearch_fields.KeywordField(),})individual=opensearch_fields.ObjectField(properties={'first_name':opensearch_fields.KeywordField(),'last_name':opensearch_fields.KeywordField(),'dob':opensearch_fields.DateField(),})status=opensearch_fields.KeywordField(fields={'status_key':opensearch_fields.KeywordField()})Note thatTextFieldtypes are typically converted to non-aggregable fields in OpenSearch.
If you require aggregable fields, use theKeywordFieldtype.By default, theauto_refreshproperty in the Index class should enable data to be
automatically transferred on every CRUD operation managed by Django ORM. Ensure that
this property is properly configured in your project.How to initialize data after deploymentIf you have initialized the application but still have some data to be transferred, you can effortlessly
achieve this by using the commands available in the business module:python manage.py add_<model_name>_data_to_opensearch.
This command loads existing data into OpenSearch.How to Import a DashboardLocate the dashboard definition file in.ndjsonformat within
theopenimis-be_<module-name>/import_datadirectory.Log in to your OpenSearch instance.Expand the sidebar located on the left side of the page.Navigate toManagementand selectDashboards Management.On the left side of the page, click onSaved Objects.At the top-right corner of the table, click onImport.A new side-modal will appear on the right side of the page.
Drag and drop the file fromopenimis-be_<module-name>/import_datainto the import dropzone.This action will import the dashboards along with related
charts that should be accessible on the visualization page.Verify if the dashboards have been imported properly.How to Export Dashboards with Related Objects like Visualizations in OpenSearch?Log in to your OpenSearch instance.Expand the sidebar located on the left side of the page.Navigate toManagementand selectDashboards Management.On the left side of the page, click onSaved Objects.At the top-right corner of the table, click onExport <N> objects.Ensure that you have selected dashboards only. Additionally, choose the option to
include related objects, and then click export all.You should have downloaded file in.ndjsonformat.Save file in the business model for initialization after deployment inopenimis-be_<module-name>/import_data.Rename filename intoopensearch_<model-name>_dashboard.ndjson