{"_id":"5a8fae0468264c001f20cc37","category":{"_id":"5a8fae0368264c001f20cc03","version":"5a8fae0268264c001f20cc00","project":"5633ebff7e9e880d00af1a53","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-23T20:11:49.377Z","from_sync":false,"order":5,"slug":"best-practices","title":"Elasticsearch Tips & Tricks"},"parentDoc":null,"project":"5633ebff7e9e880d00af1a53","user":"5633ec9b35355017003ca3f2","version":{"_id":"5a8fae0268264c001f20cc00","project":"5633ebff7e9e880d00af1a53","__v":2,"createdAt":"2018-02-23T06:00:34.961Z","releaseDate":"2018-02-23T06:00:34.961Z","categories":["5a8fae0268264c001f20cc01","5a8fae0268264c001f20cc02","5a8fae0368264c001f20cc03","5a8fae0368264c001f20cc04","5a8fae0368264c001f20cc05","5a8fae0368264c001f20cc06","5a8fae0368264c001f20cc07","5a8fae0368264c001f20cc08","5a8fae0368264c001f20cc09","5abaa7eb72d6dc0028a07bf3"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"2.0.0","version":"2.0"},"__v":1,"updates":["5b3d1fb4264fdf00034048eb"],"next":{"pages":[],"description":""},"createdAt":"2015-12-23T20:08:51.808Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"Throughout the Bonsai documentation, we use a standard terminology for discussing Elasticsearch-related subjects. This terminology is described in the [Elasticsearch documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/glossary.html), but we'll highlight and discuss some very common terms here.\n[block:api-header]\n{\n  \"title\": \"Node\"\n}\n[/block]\nA node is simply a running instance of Elasticsearch. While it's possible to run several instances of Elasticsearch on the same hardware, it really is a best practice to limit a server to a single running instance of Elasticsearch. On Bonsai, a node is technically a virtual server running in the cloud. Bonsai follows the best practice of one Elasticsearch instance per server.\n[block:api-header]\n{\n  \"title\": \"Cluster\"\n}\n[/block]\nA cluster is a collection of nodes running Elasticsearch. From the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-cluster): \"A cluster consists of one or more nodes which share the same cluster name. Each cluster has a single master node which is chosen automatically by the cluster and which can be replaced if the current master node fails.\"\n[block:api-header]\n{\n  \"title\": \"Index\"\n}\n[/block]\nIn Elasticsearch parlance, the word \"index\" can either be used as a verb or a noun. This can sometimes be confusing for users new to Elasticsearch, and especially for users for whom English is not their first language. The intended meaning is usually understood through syntax and context clues.\n\n### Index (noun)\n\nFrom the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-index): \"An index is like a table in a relational database... [It] is a logical namespace which maps to one or more [primary shards](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-primary-shard) and can have zero or more [replica shards](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-replica-shard).\"\n\nTo add to this, an index is sort of an abstraction because the shards (discussed in the section on [Shards](doc:basic-glossary#shards)) are the \"real\" search engines. Queries to an index's contents are routed to its shards, each of which is actually a [Lucene](https://lucene.apache.org/) instance. Because this happens in the background, it can appear that the index is performing the actual work, and the shards are more akin to a [Heroku dyno](https://www.heroku.com/dynos) or perhaps are simply there to add compute resources.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"\\\"How Many Indices Can I Put on a Shard?\\\"\",\n  \"body\": \"Because an Elasticsearch index is both abstract and opaque with regards to data storage and retrieval, sometimes new users miss the connection between indices and shards. A common support question that arises is \\\"how do I put more indices on my shard?\\\" The answer is that the index is composed of shards, not vice-versa.\\n\\nIn other words, an index can have many shards, but each shard can only belong to one index. This relationship precludes collocating multiple indices on a single shard.\"\n}\n[/block]\n### Index (verb)\n\nThe process of populating an Elasticsearch index (noun) with data. It is most commonly used as a transitive verb with the _data_ as the direct object, rather than the index (noun) being populated. In other words, the process is performed _on_ the data, so that you would say: \"I need to index my data,\" and not \"I need to index my index.\"\n\nIndexing is a critical step in running a search engine. Without indexing your content, you will not be able to query it using Elasticsearch, or take advantage of any of the powerful search features Elasticsearch offers.\n\nElasticsearch does not have any mechanism for extracting the contents of your website or database on its own. Indexing is something that must be managed by the application and/or the [Elasticsearch client](doc:basic-glossary#elasticsearch-client). Refer to the documentation for your platform as necessary for details.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Elasticsearch is a search engine, not a content crawler. If your use case involves scanning domains or websites, you'll need to use a crawler like [Apache Nutch](https://nutch.apache.org/). In this case, the crawler would be responsible for scraping content and indexing it into your cluster.\",\n  \"title\": \"Elasticsearch is not a Crawler\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Shards\"\n}\n[/block]\nFrom the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-shard): \"Each document is stored in a single [primary shard](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-primary-shard). When you index a document, it is indexed first on the primary shard, then on all replicas of the primary shard.\" \n\nTo add to this, each shard is technically a standalone search engine. There are two types of shards: primary and replica. We have some documentation on what shards are and where they come from in [What are shards and replicas?](what-are-shards-and-replicas), but we'll also elaborate on some of the differences here.\n\n### Primary Shards\n\nAccording to the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-primary-shard): \"Each document is stored in a single primary shard. When you index a document, it is indexed first on the primary shard, then on all replicas of the primary shard.\"\n\nAnother way to think about primary shards is \"the number of ways your data is split up.\" One reason to have an index composed of X primary shards is that each shard will contain 1/X of your data. Queries can then be performed in parallel. This is useful and can definitely improve performance if you have millions of documents. \n[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"Elasticsearch has [an entire article](https://www.elastic.co/guide/en/elasticsearch/guide/current/kagillion-shards.html) dedicated to the tradeoffs and limitations of a large number of primary shards. The really short version is that primary shards offer diminishing returns while the overhead increases sharply. \\n\\nThe takeaway is that you never want to use a large number of primary shards without some serious capacity planning and testing. If you are wondering how many primary shards you'll need, you can check out [The Ideal Elasticsearch Index](https://bonsai.io/blog/ideal-elasticsearch-cluster/) (specifically the benchmarking section), or simply [shoot us an email](mailto:support:::at:::bonsai.io).\",\n  \"title\": \"So Why Not a Kagillion Shards?\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"danger\",\n  \"title\": \"Primary Shards Can Not Be Added/Removed Later\",\n  \"body\": \"From one of our more popular [blog entries](https://bonsai.io/blog/ideal-elasticsearch-cluster/): \\\"Elasticsearch uses a naive hashing algorithm to route documents to a given primary shard. This design choice allows documents to be randomly distributed in a reproducible way. This avoids “hot spots” that affect performance and overallocation. However, it has one major downside, which is that **the number of primary shards can not be changed after an index has been created.** Replicas can be added and removed at will, but the number of primary shards is basically written in stone.\"\n}\n[/block]\n### Replica Shards\n\nThe [Elasticsearch definition](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-replica-shard) for replica shards sums it up nicely:\n\n> A replica is a copy of the primary shard, and has two purposes:\n> \n> 1. Increase failover: a replica shard can be promoted to a primary shard if the primary fails\n> 2. Increase performance: get and search requests can be handled by primary or replica shards. By default, each primary shard has one replica, but the number of replicas can be changed dynamically on an existing index. A replica shard will never be started on the same node as its primary shard. \n\nAnother way to think about replica shards is \"the number of _redundant_ copies of your data.\" If your index has 1 primary shard and 2 replica shards, then you can think of the cluster as having 3 total copies of the data. If the primary shard is lost -- for example, if the server running it dies, or there is a network partition -- then the cluster can recover automatically by using one of the replicas.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Replication is a Best Practice\",\n  \"body\": \"If you're running a production application, all of your indices should have a replication factor of at least 1. Otherwise, you're exposed to data loss if anything unexpected happens.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Replicas are Easy\",\n  \"body\": \"In contrast to primary shards, which can not be added/removed after the index is created, replicas can be added and removed at any time.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Document(s)\"\n}\n[/block]\nFrom the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-document): \"A document is a JSON document which is stored in Elasticsearch. It is like a row in a table in a relational database.\" \n\nAs referenced, an analogue for an Elasticsearch document would be a database record. It is a collection of related fields and values. For example, it might look something like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n\\t\\\"title\\\" : \\\"Hello, World!\\\",\\n  \\\"author\\\" : \\\"John Doe\\\",\\n  \\\"description\\\" : \\\"This is a JSON document!\\\"\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nYou may be thinking to yourself: \"My data is sitting in Postgres/MySQL/whatever, which is most certainly not in a JSON format! How do I turn the contents of a DB table into something Elasticsearch can read?\"\n\nNormally this work is handled automatically by a [client](doc:basic-glossary#elasticsearch-client). Most users will never need to worry about translating the database contents into the Elasticsearch documents, as it will be handled automatically and \"behind the scenes,\" so to speak.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"What about Word/Excel/PDF...?\",\n  \"body\": \"Sometimes new users are confused about the term \\\"document,\\\" because their mental model (and possibly even the data they want to index) involves file formats like Word, PDF, Excel, RTF, PPT, and others. In Elasticsearch terminology, these formats are sometimes called \\\"rich text documents,\\\" and are completely different from Elasticsearch documents.\\n\\nOne reason this can be confusing is that Elasticsearch _can_ index and search rich text documents. There are some plugins -- the [mapper-attachment](https://www.elastic.co/guide/en/elasticsearch/plugins/2.4/mapper-attachments.html) and [ingest-attachment](https://www.elastic.co/guide/en/elasticsearch/plugins/master/ingest-attachment.html) (both [supported on Bonsai](doc:plugins)) -- which use the [Apache Tika](https://tika.apache.org/) toolkit for extracting the contents of rich text documents and pushing it into Elasticsearch.\\n\\nThat said, if the data you're trying to index resides in a rich text format, you can still index it into Elasticsearch. But when reading the documentation, keep in mind that \\\"document\\\" should refer to the JSON representation of the content, and some variant of \\\"rich text\\\" will refer to the file you're trying to index.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Elasticsearch Client\"\n}\n[/block]\nA _client_ is software that sits between your application and Elasticsearch cluster. It's used to facilitate communication between your application and Elasticsearch. At a minimum, it will take data from the application, translate it into something Elasticsearch can understand, and then push that data into the cluster.\n\nMost clients will also handle a lot of other Elasticsearch-related tasks, such as:\n\n* Automatically creating indices with the correct mappings/settings\n* Handling search queries and responses\n* Setting up and managing aliases\n\nIt is unlikely that you will need to create your own client. Elasticsearch maintains a list of [language-specific clients](http://nocf-www.elastic.co/guide/en/elasticsearch/client/index.html) that are well-documented and in wide use. Clients also exist for popular frameworks and content management systems, like:\n\n* Ruby on Rails ([elasticsearch-rails](https://github.com/elastic/elasticsearch-rails) and [searchkick](https://github.com/ankane/searchkick))\n* [Django](https://github.com/django-haystack/django-haystack)\n* [Drupal](https://www.drupal.org/project/search_api_elasticsearch)\n* [Wordpress](https://wordpress.org/plugins/elasticpress/)\n* ... and many more.\n\nIn short, there is _probably_ already an open sourced, well-documented client available.\n\nThis is the amount of memory that is allocated to your cluster.\n\nThis is the amount of disk storage that is allocated to your cluster.","excerpt":"","slug":"basic-glossary","type":"basic","title":"Elasticsearch Core Concepts"}

Elasticsearch Core Concepts


Throughout the Bonsai documentation, we use a standard terminology for discussing Elasticsearch-related subjects. This terminology is described in the [Elasticsearch documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/glossary.html), but we'll highlight and discuss some very common terms here. [block:api-header] { "title": "Node" } [/block] A node is simply a running instance of Elasticsearch. While it's possible to run several instances of Elasticsearch on the same hardware, it really is a best practice to limit a server to a single running instance of Elasticsearch. On Bonsai, a node is technically a virtual server running in the cloud. Bonsai follows the best practice of one Elasticsearch instance per server. [block:api-header] { "title": "Cluster" } [/block] A cluster is a collection of nodes running Elasticsearch. From the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-cluster): "A cluster consists of one or more nodes which share the same cluster name. Each cluster has a single master node which is chosen automatically by the cluster and which can be replaced if the current master node fails." [block:api-header] { "title": "Index" } [/block] In Elasticsearch parlance, the word "index" can either be used as a verb or a noun. This can sometimes be confusing for users new to Elasticsearch, and especially for users for whom English is not their first language. The intended meaning is usually understood through syntax and context clues. ### Index (noun) From the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-index): "An index is like a table in a relational database... [It] is a logical namespace which maps to one or more [primary shards](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-primary-shard) and can have zero or more [replica shards](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-replica-shard)." To add to this, an index is sort of an abstraction because the shards (discussed in the section on [Shards](doc:basic-glossary#shards)) are the "real" search engines. Queries to an index's contents are routed to its shards, each of which is actually a [Lucene](https://lucene.apache.org/) instance. Because this happens in the background, it can appear that the index is performing the actual work, and the shards are more akin to a [Heroku dyno](https://www.heroku.com/dynos) or perhaps are simply there to add compute resources. [block:callout] { "type": "info", "title": "\"How Many Indices Can I Put on a Shard?\"", "body": "Because an Elasticsearch index is both abstract and opaque with regards to data storage and retrieval, sometimes new users miss the connection between indices and shards. A common support question that arises is \"how do I put more indices on my shard?\" The answer is that the index is composed of shards, not vice-versa.\n\nIn other words, an index can have many shards, but each shard can only belong to one index. This relationship precludes collocating multiple indices on a single shard." } [/block] ### Index (verb) The process of populating an Elasticsearch index (noun) with data. It is most commonly used as a transitive verb with the _data_ as the direct object, rather than the index (noun) being populated. In other words, the process is performed _on_ the data, so that you would say: "I need to index my data," and not "I need to index my index." Indexing is a critical step in running a search engine. Without indexing your content, you will not be able to query it using Elasticsearch, or take advantage of any of the powerful search features Elasticsearch offers. Elasticsearch does not have any mechanism for extracting the contents of your website or database on its own. Indexing is something that must be managed by the application and/or the [Elasticsearch client](doc:basic-glossary#elasticsearch-client). Refer to the documentation for your platform as necessary for details. [block:callout] { "type": "info", "body": "Elasticsearch is a search engine, not a content crawler. If your use case involves scanning domains or websites, you'll need to use a crawler like [Apache Nutch](https://nutch.apache.org/). In this case, the crawler would be responsible for scraping content and indexing it into your cluster.", "title": "Elasticsearch is not a Crawler" } [/block] [block:api-header] { "title": "Shards" } [/block] From the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-shard): "Each document is stored in a single [primary shard](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-primary-shard). When you index a document, it is indexed first on the primary shard, then on all replicas of the primary shard." To add to this, each shard is technically a standalone search engine. There are two types of shards: primary and replica. We have some documentation on what shards are and where they come from in [What are shards and replicas?](what-are-shards-and-replicas), but we'll also elaborate on some of the differences here. ### Primary Shards According to the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-primary-shard): "Each document is stored in a single primary shard. When you index a document, it is indexed first on the primary shard, then on all replicas of the primary shard." Another way to think about primary shards is "the number of ways your data is split up." One reason to have an index composed of X primary shards is that each shard will contain 1/X of your data. Queries can then be performed in parallel. This is useful and can definitely improve performance if you have millions of documents. [block:callout] { "type": "warning", "body": "Elasticsearch has [an entire article](https://www.elastic.co/guide/en/elasticsearch/guide/current/kagillion-shards.html) dedicated to the tradeoffs and limitations of a large number of primary shards. The really short version is that primary shards offer diminishing returns while the overhead increases sharply. \n\nThe takeaway is that you never want to use a large number of primary shards without some serious capacity planning and testing. If you are wondering how many primary shards you'll need, you can check out [The Ideal Elasticsearch Index](https://bonsai.io/blog/ideal-elasticsearch-cluster/) (specifically the benchmarking section), or simply [shoot us an email](mailto:support@bonsai.io).", "title": "So Why Not a Kagillion Shards?" } [/block] [block:callout] { "type": "danger", "title": "Primary Shards Can Not Be Added/Removed Later", "body": "From one of our more popular [blog entries](https://bonsai.io/blog/ideal-elasticsearch-cluster/): \"Elasticsearch uses a naive hashing algorithm to route documents to a given primary shard. This design choice allows documents to be randomly distributed in a reproducible way. This avoids “hot spots” that affect performance and overallocation. However, it has one major downside, which is that **the number of primary shards can not be changed after an index has been created.** Replicas can be added and removed at will, but the number of primary shards is basically written in stone." } [/block] ### Replica Shards The [Elasticsearch definition](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-replica-shard) for replica shards sums it up nicely: > A replica is a copy of the primary shard, and has two purposes: > > 1. Increase failover: a replica shard can be promoted to a primary shard if the primary fails > 2. Increase performance: get and search requests can be handled by primary or replica shards. By default, each primary shard has one replica, but the number of replicas can be changed dynamically on an existing index. A replica shard will never be started on the same node as its primary shard. Another way to think about replica shards is "the number of _redundant_ copies of your data." If your index has 1 primary shard and 2 replica shards, then you can think of the cluster as having 3 total copies of the data. If the primary shard is lost -- for example, if the server running it dies, or there is a network partition -- then the cluster can recover automatically by using one of the replicas. [block:callout] { "type": "info", "title": "Replication is a Best Practice", "body": "If you're running a production application, all of your indices should have a replication factor of at least 1. Otherwise, you're exposed to data loss if anything unexpected happens." } [/block] [block:callout] { "type": "success", "title": "Replicas are Easy", "body": "In contrast to primary shards, which can not be added/removed after the index is created, replicas can be added and removed at any time." } [/block] [block:api-header] { "title": "Document(s)" } [/block] From the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/glossary.html#glossary-document): "A document is a JSON document which is stored in Elasticsearch. It is like a row in a table in a relational database." As referenced, an analogue for an Elasticsearch document would be a database record. It is a collection of related fields and values. For example, it might look something like this: [block:code] { "codes": [ { "code": "{\n\t\"title\" : \"Hello, World!\",\n \"author\" : \"John Doe\",\n \"description\" : \"This is a JSON document!\"\n}", "language": "json" } ] } [/block] You may be thinking to yourself: "My data is sitting in Postgres/MySQL/whatever, which is most certainly not in a JSON format! How do I turn the contents of a DB table into something Elasticsearch can read?" Normally this work is handled automatically by a [client](doc:basic-glossary#elasticsearch-client). Most users will never need to worry about translating the database contents into the Elasticsearch documents, as it will be handled automatically and "behind the scenes," so to speak. [block:callout] { "type": "info", "title": "What about Word/Excel/PDF...?", "body": "Sometimes new users are confused about the term \"document,\" because their mental model (and possibly even the data they want to index) involves file formats like Word, PDF, Excel, RTF, PPT, and others. In Elasticsearch terminology, these formats are sometimes called \"rich text documents,\" and are completely different from Elasticsearch documents.\n\nOne reason this can be confusing is that Elasticsearch _can_ index and search rich text documents. There are some plugins -- the [mapper-attachment](https://www.elastic.co/guide/en/elasticsearch/plugins/2.4/mapper-attachments.html) and [ingest-attachment](https://www.elastic.co/guide/en/elasticsearch/plugins/master/ingest-attachment.html) (both [supported on Bonsai](doc:plugins)) -- which use the [Apache Tika](https://tika.apache.org/) toolkit for extracting the contents of rich text documents and pushing it into Elasticsearch.\n\nThat said, if the data you're trying to index resides in a rich text format, you can still index it into Elasticsearch. But when reading the documentation, keep in mind that \"document\" should refer to the JSON representation of the content, and some variant of \"rich text\" will refer to the file you're trying to index." } [/block] [block:api-header] { "title": "Elasticsearch Client" } [/block] A _client_ is software that sits between your application and Elasticsearch cluster. It's used to facilitate communication between your application and Elasticsearch. At a minimum, it will take data from the application, translate it into something Elasticsearch can understand, and then push that data into the cluster. Most clients will also handle a lot of other Elasticsearch-related tasks, such as: * Automatically creating indices with the correct mappings/settings * Handling search queries and responses * Setting up and managing aliases It is unlikely that you will need to create your own client. Elasticsearch maintains a list of [language-specific clients](http://nocf-www.elastic.co/guide/en/elasticsearch/client/index.html) that are well-documented and in wide use. Clients also exist for popular frameworks and content management systems, like: * Ruby on Rails ([elasticsearch-rails](https://github.com/elastic/elasticsearch-rails) and [searchkick](https://github.com/ankane/searchkick)) * [Django](https://github.com/django-haystack/django-haystack) * [Drupal](https://www.drupal.org/project/search_api_elasticsearch) * [Wordpress](https://wordpress.org/plugins/elasticpress/) * ... and many more. In short, there is _probably_ already an open sourced, well-documented client available. This is the amount of memory that is allocated to your cluster. This is the amount of disk storage that is allocated to your cluster.