The basic principles of Elasticsearch data indexes and search
The basic principles of Elasticsearch data indexes and search
Elasticsearch is an open source distributed search and analysis engine, which is widely used as the back -end data storage and search engine of the application.It provides fast and real -time search and analytical functions, which can handle large -scale data sets and have powerful scalability and high reliability.This article will introduce the basic principles of Elasticsearch data indexes and search, and provide relevant programming code and configuration examples.
1. Elasticsearch basic concept
Before understanding the index and search principles of Elasticsearch, we need to understand some basic concepts:
1.1. Node (Node): The node is one of the members of the Elasticsearch cluster for storage and processing data.A cluster is usually composed of multiple nodes, and each node can be a masterpiece or data node.
1.2. Index: Index is a logical naming space for tissue and storage related documents.Each index can contain multiple types (Type), and the type contains multiple documents.
1.3. Type (Type): Type defines the structure and field of the document in the index.Each type has a mapping definition to describe the types and attributes of each field in the index.
1.4. Document: Document is the basic data unit in the index, which is represented in JSON format.Each document has a unique ID logo for retrieval and update operations.
2. Data indexing principle
Elasticsearch data index is based on the principle of Inverted Index.Invoicant index is a data structure for quickly searching for documents. It uses vocabulary as a key and a document that appears in the vocabulary as a value.This structure can greatly improve search efficiency.
2.1. Index creation
Before creating indexes, we first need to define the mapping of indexes.The mapping defines the types and attributes of each field, including text analyzers, index options, sorting methods, etc.The following is an example code that creates indexes and defines mapping:
python
PUT /my_index
{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
The above code creates an index called "My_index" and defines the mapping of the two fields of "Title" and "Content".The Chinese segmentation "IK_MAX_WORD" is used here, which can be divided into Chinese texts.
2.2. Document insertion
Once the index creation is completed, we can insert the document into the index.Each document is a JSON object and contains fields and corresponding values.The following is an example code that inserts the document into the index:
python
POST /my_index/_doc/1
{
"Title": "Elasticsearch Data Index",
"Content": "Elasticsearch is a distributed search and analysis engine"
}
The above code inserts a document containing the title and content into an index named "My_index", and the unique ID of the document is "1".
2.3. Index and search
Once the document is inserted into the index, we can use search and query to quickly retrieve the document.The following is a simple search example code:
python
GET /my_index/_search
{
"query": {
"match": {
"title": "Elasticsearch"
}
}
}
The above code will return a document containing the keyword "Elasticsearch" in all the title.
3. Elasticsearch configuration
In addition to basic indexes and search operations, we can also adjust the behavior and performance of Elasticsearch through configuration files.Here are some common configuration option examples:
3.1. Cluster configuration
Configure files through the `ElasticSearch.yml` configuration file, the role of the cluster, the character of the node, and the network binding address.Here are some commonly used configuration options:
yaml
cluster.name: my_cluster
node.name: my_node
network.host: 192.168.0.1
3.2. Index configuration
The type and attribute of the field can be set by mapping definition.For example, you can specify whether a field is indexed, whether the original value is stored, and whether it is enabled.
3.3. Saber and copy
Elasticsearch divides each index into multiple shards, and saves a copy of the shards on multiple nodes in the cluster.You can configure the number of shards and copies through the index settings. By default, each index has 5 main shards and 1 copy.
In summary, this article introduces the basic principles of data indexes and search for Elasticsearch.We understand the basic concepts, such as nodes, indexes, types, and documents, and learn how to create indexes, insert documents, and conduct search and queries.In addition, we also briefly introduced some Elasticsearch configuration options to customize according to actual needs.Through flexible configuration and powerful search functions, Elasticsearch has become a popular data storage and search engine.
Note: The code example in this article is based on Elasticsearch 7.x version, and the specific grammar may vary from the version.Please adjust according to the actual situation.