foreword
"Author's Homepage" : Sprite Youbai Bubbles
"Personal Website" : Sprite's personal website
"Recommendation Column" :
★ Java one-stop service ★
★ React from entry to proficiency ★
★ Cool front-end code sharing ★
★ From 0 to hero, the road to Vue becoming a god ★
★ uniapp-from construction to promotion ★
★ From 0 to hero, Vue becoming a god Road ★
★ One column is enough to solve the algorithm ★
★ Let’s talk about the architecture from 0 ★
★ The exquisite way of data circulation ★
★ The road to advanced backend ★
Article directory
index management
Indexing is one of the core concepts in Elasticsearch. It is a data structure similar to a table in a database for storing and searching documents. This article will explain how to create, update, delete and maintain Elasticsearch indexes, and learn how to map field types and analyzers.
create index
1. Create an index using the Java API
A new index can be created using the Java API. Here is a code example that creates an index named my_index:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
CreateIndexRequest request = new CreateIndexRequest("my_index");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
The above code creates an index called my_index using the RestHighLevelClient object.
2. Use the CURL command to create an index
Indexes can also be created directly in Elasticsearch via CURL commands. The following is an example of using the CURL command to create an index named my_index:
curl -XPUT 'localhost:9200/my_index?pretty'
update index
1. Update index settings using Java API
The settings of an existing index can be updated using the Java API. Here is an example of code that changes the number of replicas from 1 to 2 for an index named my_index:
UpdateSettingsRequest request = new UpdateSettingsRequest("my_index");
request.settings(Settings.builder()
.put("index.number_of_replicas", 2));
AcknowledgedResponse response = client.indices().putSettings(request, RequestOptions.DEFAULT);
The above code changes the number of replicas of the my_index index to 2 using the UpdateSettingsRequest object.
2. Use the CURL command to update the index settings
The settings of an existing index can be updated using the CURL command. Here is an example of changing the number of replicas from 1 to 2 for an index named my_index:
curl -XPUT 'localhost:9200/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'
{
"index" : {
"number_of_replicas" : 2
}
}
'
delete index
1. Use the Java API to delete the index
An existing index can be dropped using the Java API. The following is a code example that deletes an index named my_index:
DeleteIndexRequest request = new DeleteIndexRequest("my_index");
AcknowledgeResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
The above code deletes the index named my_index using the DeleteIndexRequest object.
2. Use the CURL command to delete the index
An existing index can be deleted using the CURL command. The following is an example of dropping an index named my_index:
curl -XDELETE 'localhost:9200/my_index?pretty'
Mapping Field Types and Analyzers
When creating an index, you need to map field types and analyzers. The field type defines the type of field value, such as string, number, date, etc. Analyzers define rules for converting text into terms. The following is an example of creating an index with title and content fields, and mapping these two fields to type text:
CreateIndexRequest request = new CreateIndexRequest("my_index");
request.mapping("properties", "title", "type=text",
"analyzer=standard",
"fields=keyword",
"content", "type=text",
"analyzer=english");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
The above code uses the CreateIndexRequest object to create an index named my_index and maps it to a text type that contains title and content fields.
in conclusion
This article describes how to create, update, and delete Elasticsearch indexes, and how to map field types and analyzers. When creating indexes, field types and analyzers need to be considered to improve search efficiency. Elasticsearch indexes can be managed using the Java API or CURL commands.
Data upload and query
Uploading data into the Elasticsearch index and performing various queries is one of the core functions of using Elasticsearch. This article will describe how to upload data into an Elasticsearch index and use various queries to retrieve and filter the data.
Upload data to Elasticsearch
1. Use the Java API to upload data
Data can be uploaded into an Elasticsearch index using the Java API. The following is a code example of uploading a document to an index named my_index using a RestHighLevelClient object:
IndexRequest request = new IndexRequest("my_index");
request.id("1");
String jsonString = "{" +
"\"name\":\"John\"," +
"\"age\":30," +
"\"city\":\"New York\"" +
"}";
request.source(jsonString, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
The above code uses an IndexRequest object to upload a document named John, age 30, from New York to an index named my_index.
2. Use the CURL command to upload data
Data can also be uploaded into the Elasticsearch index using the CURL command. Here is an example of uploading a document named John, age 30, from New York to an index named my_index:
curl -XPOST 'localhost:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d'
{
"name": "John",
"age": 30,
"city": "New York"
}
'
Search and filter data
1. Search and filter data using the Java API
Data in an Elasticsearch index can be searched and filtered using the Java API. The following is a code example to perform a simple match_all query using the RestHighLevelClient object:
SearchRequest request = new SearchRequest("my_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
The above code performs a simple match_all query using the SearchRequest object and the SearchSourceBuilder object and returns all documents.
2. Use CURL commands to search and filter data
It is also possible to search and filter data in Elasticsearch indexes using CURL commands. The following is an example of using a match_all query to retrieve all documents in an index named my_index:
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {
}
}
}
'
in conclusion
This article describes how to upload data into an Elasticsearch index and use various queries to retrieve and filter the data. Elasticsearch indexes can be managed using Java API or CURL commands, with flexibility and ease of use. In practical applications, it is necessary to select an appropriate method to upload and query data according to the data model and query requirements.