ElasticSearch is the game for ElasticSearch

Elasticsearch is a distributed, RESTful search and data analysis engine that addresses a variety of emerging use cases. At the heart of the Elastic Stack, it stores your data centrally, helping you find what you expect and what you don’t expect. Elasticsearch is Java version 7.8 or later and requires jdk1.8 or later.

Elasticsearch is a document-oriented database, where a piece of data is a document. To help you understand this, let’s compare Elasticsearch to relational database mysql

Index in ES is a library, type is a table, Documents is a row.

* Elasticsearch 6.x: you can only have one type under an index. * Elasticsearch 7.x: You can only have one type under an index. * Elasticsearch 7.x: You can only have one type under an index.

1. Install Elasticsearch, download the Windwins package from the official website, and decompress it

Directory description

Bin: bat file used to start the program

Config: indicates the configuration information

Data: stores data

JDK: built-in JDK

Lib: the required JAR package

Logs: log files

Modules: indicates the function module

Plugins: Extended feature packs

Click the elasticSearch. bat file in the bin directory to start the elasticSearch project. Default system access port 9200

usehttp://127.0.0.1:9200/ If the following page is displayed, the service is successfully started

2. Connect to es using javaApi

(1) After a new Maven project is created, pom.xml imports the required package

(2) Create a client connection test using RestHighLevelClient

public class EsClientTest {

@Test public void testConneEs() throws IOException { //1. Create an ES client RestHighLevelClient RestHighLevelClient = new RestHighLevelClient(restClient. builder(new HttpHost("localhost",9200,"http")) ); / / 2. Closing restHighLevelClient connection. The close (); }Copy the code

}

3. Create indexes

4. Obtain index records

Console access

5. Delete indexes

6. Add doc to index

Use Postman to view the doc document that is currently added

7. Update a value of doc

The value of the name field has been updated to Li Si

8. Query doc documents

@Test

public void docSelect() throws IOException { //1. Create an ES client. Connect to RestHighLevelClient RestHighLevelClient = new RestHighLevelClient(restClient. builder(new HttpHost("localhost",9200,"http")) ); GetRequest = new GetRequest(); getRequest.index("user").id("1001"); GetResponse documentFields = resthighLevelClient. get(getRequest, requestOptions.default); System.out.println(documentFields.getSourceAsString()); / / 4. Close the resources restHighLevelClient. Close (); }Copy the code

Console output

9. Delete doc documents

Console Execution result

Query records using postman – Deleted successfully

10. Add doc in batches

Use postman to view all documents under the user index

11. Delete doc files in batches

Use Postman to view documents under the index

12. Batch doc updates

View the document under the index in postman

SQL > query doc

Console prints messages

14. Use query to query doc under an index

15. Use query pagination to query doc under an index

SQL > query doc under index with sort

17. Query doc under an index with query – query some fields

SQL > select * from doc where index = ‘and’;

SQL > select * from doc where index (or);

20. Query doc under an index with query – query by range

21. Use query to query a doc-fuzzy query under an index

22. Use query to query doc under an index – highlight a field

23. Query doc-aggregate query under index (Max, min) with query

24. Use query to query doc- block query under an index

25. Overview of ElasticSearch for Cluster Deployment

Context: A single ElasticSearch server has the maximum load capacity. If the load exceeds this threshold, the server performance deteriorates or becomes unavailable. Therefore, the elasticSearch server runs in a specified server cluster.

Cluster: A cluster is a group of one or more server nodes that hold the entire data and provide indexing and search services together. An Elasticsearch cluster has a unique name identifier and the entire name is Elasticsearch by default. The entire name is important because a node can only be added to a cluster by specifying the name of a cluster.

Node Node: a cluster contains many servers. A Node is one of the servers. As part of a cluster, it stores data and participates in the cluster’s indexing and search functions. A node is also defined by a representation, and a node can be added to a specified cluster by configuring the cluster name

26. Deploy ElasticSearch in a Windows cluster

(1) Create a file for ElasticSearch-cluster, copy three copies of the elasticsearch file, delete the data and logs files, name them node-1001, Node-1002, and Node-1003, respectively, and serve as three nodes

(2) Modify the config/ elasticSearch. yml file of each node in the cluster file directory

Node-1001 Node configuration

The cluster name must be the same among the nodes

cluster.name: my-application

The node name must be unique within the cluster

node.name: node-1001

Can be used as a master node

node.master: true

# Store data

node.data: true

# the IP address

network.host: localhost

HTTP port #

http.port: 1001

TCP listening port for internal communication between nodes

transport.tcp.port: 9301

Address of the host that the nodes communicate with each other

#discovery.seed_hosts: [“host1”, “host2”]

#discovery.zen.fd.ping_timeout:1m

#discovery.zen.fd.ping_retries:5

# Cross-domain configuration

http.cors.enabled: true

http.cors.allow-origin: “*”

At this time, start Node-1001 from bin/ elasticSearch. bat, and the node name and cluster name are displayed on the control console

Use postman to view node startup status (startup) and node number (1).

Node-1002 Node configuration

The cluster name must be the same among the nodes

cluster.name: my-application

The node name must be unique within the cluster

node.name: node-1002

Can be used as a master node

node.master: true

# Store data

node.data: true

# the IP address

network.host: localhost

HTTP port #

http.port: 1002

TCP listening port for internal communication between nodes

transport.tcp.port: 9302

# Host address for the nodes to communicate with – find the primary node

discovery.seed_hosts: [“localhost:9301”]

The timeout for executing the ping command

discovery.zen.fd.ping_timeout: 1m

discovery.zen.fd.ping_retries: 5

# Cross-domain configuration

http.cors.enabled: true

http.cors.allow-origin: “*”

At this time, start Node-1002 from bin/ elasticSearch. bat, and the node name and cluster name are displayed on the console

Use postman to view node startup status (startup) and node number (2).

Node-1003 Node configuration

The cluster name must be the same among the nodes

cluster.name: my-application

The node name must be unique within the cluster

node.name: node-1003

Can be used as a master node

node.master: true

# Store data

node.data: true

# the IP address

network.host: localhost

HTTP port #

http.port: 1003

TCP listening port for internal communication between nodes

transport.tcp.port: 9303

# Host address for the nodes to communicate with – find the primary node

discovery.seed_hosts: [“localhost:9301″,”localhost:9302”]

The timeout for executing the ping command

discovery.zen.fd.ping_timeout: 1m

discovery.zen.fd.ping_retries: 5

# Cross-domain configuration

http.cors.enabled: true

http.cors.allow-origin: “*”

At this time, start Node-1003 from bin/ elasticSearch. bat, and the node name and cluster name are displayed on the console

Use postman to view node startup status (startup) and node number (3)

Well, that’s all for today’s article, hoping to help those of you who are confused in front of the screen