Monday, 16 October 2023

Elastic Search create index on startup

There are two ways to create an index on startup in Elasticsearch:

  1. Use an index template: An index template is a prototype that defines the settings and mappings for an index. When Elasticsearch starts up, it will automatically create any indices that match the patterns defined in the index templates. To create an index template, you can use the PUT _index_template API.
  2. Use a startup script: A startup script is a script that is executed when Elasticsearch starts up. You can use a startup script to create indices, perform other administrative tasks, or initialize your Elasticsearch cluster. To create a startup script, you can use the scripts directory in the Elasticsearch configuration directory.

Here is an example of an index template that you can use to create an index on startup:

JSON
{
  "index_patterns": ["my_index-*"],
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "field1": {
        "type": "text"
      }
    }
  }
}

To create this index template, you would use the following command:

PUT _index_template/my_index
{
  "index_patterns": ["my_index-*"],
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "field1": {
        "type": "text"
      }
    }
  }
}

Once you have created the index template, Elasticsearch will automatically create the my_index index when it starts up.

Here is an example of a startup script that you can use to create an index on startup:

Python
import elasticsearch

es = elasticsearch.Elasticsearch()

# Create the index
es.indices.create("my_index", settings={
  "number_of_shards": 1,
  "number_of_replicas": 1
})

# Add a mapping to the index
es.indices.put_mapping("my_index", {
  "mappings": {
    "properties": {
      "field1": {
        "type": "text"
      }
    }
  }
})

To create this startup script, you would save it as a .py file in the scripts directory in the Elasticsearch configuration directory. Then, you would configure Elasticsearch to run the startup script when it starts up. You can do this by setting the script.engine and script.inline settings in the Elasticsearch configuration file.

Once you have configured Elasticsearch to run the startup script, it will create the my_index index when it starts up.

Which method you choose to create an index on startup depends on your specific needs. If you need to create multiple indices with the same settings and mappings, then using an index template is a good option. If you need to create an index with custom settings and mappings, or if you need to perform other administrative tasks on startup, then using a startup script is a good option.

Friday, 13 October 2023

MariaDB vs PostgreSQL

MariaDB and PostgreSQL are both open-source relational database management systems (RDBMS) that are popular for a variety of applications. However, there are some key differences between the two databases.

MariaDB is a fork of MySQL that was created by the original developers of MySQL. It is designed to be more compatible with MySQL and to offer additional features and improvements. MariaDB is also known for its performance and scalability.

PostgreSQL is an object-relational database management system (ORDBMS) that is known for its stability, reliability, and features. PostgreSQL offers a number of features that are not available in MariaDB, such as support for complex data types, stored procedures, and triggers.

Here is a table that summarizes the key differences between MariaDB and PostgreSQL:

FeatureMariaDBPostgreSQL
DeveloperMariaDB FoundationPostgreSQL Global Development Group
LicensingOpen-sourceOpen-source
CompatibilityCompatible with MySQLCompatible with Oracle Database
FeaturesMore features than MySQLMore features than MariaDB
PerformanceGoodGood
ScalabilityGoodGood

Which one to use?

The best choice for you will depend on your specific needs and requirements. If you are looking for a database that is compatible with MySQL and offers additional features and improvements, then MariaDB is a good choice. If you are looking for a database that is known for its stability, reliability, and features, then PostgreSQL is a good choice.

Here are some additional things to consider when choosing between MariaDB and PostgreSQL:

  • Features: PostgreSQL offers a number of features that are not available in MariaDB, such as support for complex data types, stored procedures, and triggers. If you need these features, then PostgreSQL is the better choice.
  • Performance: MariaDB and PostgreSQL are both known for their performance. However, PostgreSQL may offer better performance for certain workloads, such as complex queries and analytics.
  • Scalability: MariaDB and PostgreSQL are both scalable. However, PostgreSQL may be better suited for very large databases.
  • Community support: Both MariaDB and PostgreSQL have large and active communities. However, the PostgreSQL community is generally considered to be more mature and experienced.

Overall

Both MariaDB and PostgreSQL are good choices for RDBMS. The best choice for you will depend on your specific needs and requirements. If you are not sure which database to choose, it is a good idea to consult with a database expert.