This is the 27th day of my participation in the August More Text Challenge

I am not exaggerating the title, it will work, there is also a combination of code and graphics for each step, currently working on Elasticsearch, is there anything else I don’t need to learn? Numb numb. Go down, come up again, I think we’ll be different. 😁

About the cover: nice pictures by nice people

Akik: Stupid kid.

Docker install Elasticsearch

1.1. Pull the Elasticsearch image

Docker pull elasticsearch: 7.7.0Copy the code

View the image command: Docker images

Docker rmi < image name: version > or docker rmI < image id>

1.2. Preparation before startup

Create a folder for storing data and configuration files and mount it at startup.

mkdir -p  /home/elasticsearch/data/ 
mkdir -p  /home/elasticsearch/config/
Copy the code

Explanation:

  • -p: parameter to create multi-level folders
  • Tree: displays file directories in a tree structure

Writing configuration files

echo 'http.host: 0.0.0.0 http.coron. enabled: true http.coron. allow-origin: "*" '>>/home/elasticsearch/config/elasticsearch.yml
Copy the code

Explanation:

  • echo >>Echo >> : uses Shell programming syntax, using the echo >> command to append content to the file, the original content will be saved
    • Using the > command overwrites the original contents of the file and re-enters the contents, or creates the file if it does not exist.
  • cat: Displays the file content.
  • http.cors.enabled: truehttp.cors.allow-origin: "*" The configuration is designed to address cross-domain issues, as the visualization tools will be downloaded later.

Modifying folder permissions

chmod -R 777 /home/elasticsearch/
ls -l View file permissions
Copy the code

1.3. Start the Elasticseach mirror

docker run --name elasticsearch -p 9200:9200 \
 -p 9300:9300 \
 -e "discovery.type=single-node" \
 -e ES_JAVA_OPTS="-Xms64m -Xmx128m"\ -v /home/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v /home/elasticsearch/data:/usr/share/elasticsearch/data \ -v /home/elasticsearch/plugins:/usr/share/elasticsearch/plugins Elasticsearch \ - d: 7.7.0Copy the code

Parameter Description:

--name elasticSearch: name elasticSearch. -p 9200:9200: map port 9200 of the container to port 9200 of host. -p 9300:9300: Map port 9300 of the container to port 9300 of the host machine to enable the cluster to communicate with each other -e"discovery.type=single-node": Singleton mode -e ES_JAVA_OPTS="-Xms64m -Xmx128m": configure the memory size - v/mydata/elasticsearch/config/elasticsearch yml: / usr/share/elasticsearch/config/elasticsearch. Yml: Mount the configuration file to the host machine - v/mydata/elasticsearch/data: / usr/share/elasticsearch/data: Will mount data files to the host machine - v/mydata/elasticsearch/plugins: / usr/share/elasticsearch/plugins: the plugin directory mounted to the host machine (to restart) - d elasticsearch: 7.7.0: Run the container in the background and return the container IDCopy the code

Running results:

Check whether the startup is successful:

The next step is to access the browser.

Enter server IP address :9200 in the browser

1.4. Possible problems

  • If the startup succeeds but the access fails, check whether the mapped port in the security group is enabled. If it is a local virtual machine, may be associated with the firewall (I use the cloud server, there is no test, if there is a problem, still need you to go to baidu | | Google).

  • -e ES_JAVA_OPTS=” -xMS64m -XMx128m “: The configured memory size is incorrect. (Can try to adjust, Docker can play well, don’t panic)

  • Finally, there may be a problem in the configuration file.

    echo 'http.host: 0.0.0.0 http.coron. enabled: true http.coron. allow-origin: "*" '>>/home/elasticsearch/config/elasticsearch.yml
    Copy the code

    HTTP. Host: 0.0.0.0 with a space after the colon. The configuration file is in YML format, so be sure to write in strict YML format.

Install ik word splitter for Elasticsearch

2.1. What is IK word segmentation?

Participle that divide a Duan Zhongwen or else into keywords, we will put their information in the search time for word segmentation, the database or index to participle in the library data, and then a matching operation, the default of Chinese word segmentation is to each word as a word, such as “I love technology” will be divided into “I”, “love”, “technology”, “art” .

Results:

{
    "tokens": [{"token": "我"."start_offset": 0."end_offset": 1."type": "<IDEOGRAPHIC>"."position": 0
        },
        {
            "token": "Love"."start_offset": 1."end_offset": 2."type": "<IDEOGRAPHIC>"."position": 1
        },
        {
            "token": "Skill"."start_offset": 2."end_offset": 3."type": "<IDEOGRAPHIC>"."position": 2
        },
        {
            "token": "Art"."start_offset": 3."end_offset": 4."type": "<IDEOGRAPHIC>"."position": 3}}]Copy the code

This obviously does not meet the requirements, so we need to install the Chinese word segmentation IK to solve this problem

IK provides two word segmentation algorithms: IK_smart and IK_max_word

Ik_smart is the least segmentation and IK_MAX_WORD is the most fine-grained segmentation

2.2. Install IK word dividers

The elasticSearch container is displayed.

docker exec -it elasticsearch /bin/bash
Copy the code

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.0/elasticsearch-analysis-ik-7.7.0.zip
Copy the code

This problem will occur during the download process, so just type Y and press Enter.

Once the download is complete, exit and restart the ElasticSearch container.

Docker restart docker restartRestart the container command
Copy the code

2.3 test whether the IK word divider is installed successfully

Once the installation is complete, let’s test it again to see what happens.

Postman Post request word segmentation test: http://server IP address :9200/_analyze

{
	"tokenizer":"ik_smart".// Depending on the participle, there will be some changes in this area, which may need to be learned.
	"text":"I love technology"
}
Copy the code

Results:

{
    "tokens": [{"token": "我"."start_offset": 0."end_offset": 1."type": "CN_CHAR"."position": 0
        },
        {
            "token": "Love"."start_offset": 1."end_offset": 2."type": "CN_CHAR"."position": 1
        },
        {
            "token": "Technology"."start_offset": 2."end_offset": 4."type": "CN_WORD"."position": 2}}]Copy the code

3, installation Elasticsearch visualization tools | mobz/Elasticsearch – head: 5

Elasticsearch visual chemistry has a variety of options, you can choose according to your preferences, I’m using mobz/ Elasticsearch head:5.

3.1. Pull mirror

docker pull mobz/elasticsearch-head:5
Copy the code

3.2. Start the image

docker run -d --name elasticsearch-head -p 9100:9100 mobz/elasticsearch-head:5
Copy the code

3.3, tests,

We visit http:// server IP address :9100/ in the browser

3.4 Possible problems

The following cross-domain configuration is missing from the elasticSearch configuration file elasticSearch.yml.

http.cors.enabled: true 
http.cors.allow-origin: "*"
Copy the code

🌈 talk to yourself

Create elasticSearch cluster for elasticSearch

Hello, I am ning Zhichun, a blogger, a small seed on the way of Learning Java. I also hope that one day I can take root and grow into a tree in the sky.

Recently in the continuous update, if you think the article is helpful to you, also interested in the words, follow me.

Let’s study and discuss together.

Hope to share with you 😁

We: when we meet, we have achieved something.