Today’s goal
Installation, configuration and integration of elasticsearch and elasticsearch head in PHP laravel project
1. Selection of ES version
Before using ES, we must first choose an appropriate version. Choosing the latest version is always an infallible choice. At present, the latest version of ES official website is 8.0.0-alpha 2, which is published in September 17, 2021 (2021 / 9 / 17). It is an internal beta version. It has only been one month. Because there are few instructions on its use on the network and there may be no corresponding PHP extension package, which is not conducive to learning, we can choose 7.0 Learn from version X. 7.15.1 is the latest release version, which was released on October 15, 2021. It is only a few days ago. It is best to choose 7.15.1 for learning.
2. Download and installation of ES
I usually use Windows system for local debugging and learning, and Linux is used for production environment, so the download and installation of ES under both operating systems will be used.
Download link on the official website:https://www.elastic.co/cn/downloads/elasticsearch#ga-release(it’s a Chinese node, and the download speed is very fast. You can download it in about 3 minutes)
Under Windows:
Select the Windows version to download and unzip elasticsearch-7.15.1-windows-x86_ 64.zip, double-click to run elasticsearch.zip under the bin directory Bat (anti-virus software may intercept, and you can allow it)
Under Linux
mkdir /usr/local/software/es
cd /usr/local/software/es
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.1-linux-x86_64.tar.gz
tar -xvf elasticsearch-7.15.1-darwin-x86_64.tar.gz
./elasticsearch-7.15.1-darwin-x86_64/bin/elasticsearch
Test installation:
If publish appears in the command line prompt_ address {127.0.0.1:9200}, bound_ Addresses {127.0.0.1:9200}, {[:: 1]: 9200} indicates successful installation or direct accesshttp://localhost:9200View es specific information
{
"name" : "PS2019WRLCRMZY",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "LcZNPNroRDOdQ8f6hkw12Q",
"version" : {
"number" : "7.15.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "83c34f456ae29d60e94d886e455e6a3409bba9ed",
"build_date" : "2021-10-07T21:56:19.031608185Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
3. Es configuration file
Solve CORS
Find elasticsearch. In the config folder YML, add the following at the end of the file:
http.cors.enabled: true
http.cors.allow-origin: “*”
4. Install the ES visualization plug-in elasticsearch head
-
Elasticsearch head is a client plug-in used to monitor elasticsearch status, including data visualization, adding, deleting, modifying and querying operations
-
Install node js
Download address:https://nodejs.org/en/download/Download the corresponding MSI according to your own system and double-click install. -
Install the head plug-in
https://github.com/mobz/elasticsearch-headDownload zip file
Extract to the ES directory -
Modify configuration to solve cross domain problems
-
elasticsearch-head-master\Gruntfile. JS 97 line adds a new hostname: ‘*’,
connect: {
server: {
options: {
hostname: '*',
port: 9100,
base: '.',
keepalive: true
}
}
}
- elasticsearch-head-master_ site\app. JS 3795 line added | |“http://localhost:9200“;
_node_handler: function(data) {
if(data) {
this.prefs.set("app-base_uri", this.cluster.base_uri) || "http://localhost:9200";
if(data.version && data.version.number)
this.cluster.setVersion(data.version.number);
}
},
-
elasticsearch-head-master\package.json “license”: “Apache2” => “license”: “Apache-2.0”,
-
function
-
Run in ES / es head directory
- npm install
- NPM run start (every time in the future). If it is not successful, execute NPM install again
-
Run in browserhttp://localhost:9100/
image.png
5. Es cluster health value
Under normal conditions, elasticsearch cluster health status can be divided into three types:
Green is the healthiest state, indicating that all fragments, including backups, are available; In this case, all primary and replica partitions of the elasticsearch cluster have been allocated, and the elasticsearch cluster is 100% available.
Yellow basic fragmentation is available, but backup is not available (or there is no backup); In this case, all the primary partitions of elasticsearch cluster have been partitioned, but at least one copy is missing. There will be no data loss, so the search results are still complete. However, your high availability has been weakened to some extent. If more pieces disappear, you will lose data. Think of yellow as a warning that needs to be investigated in time.
The partition of red part is available, indicating that part of the partition is damaged. At this time, some data can still be found when executing the query. In this case, it is better to solve it as soon as possible; In this case, at least one primary partition (and all its replicas) of the elasticsearch cluster is missing. This means that you are missing data: the search can only return part of the data, and the write request allocated to this partition will return an exception.
Problems encountered and Solutions
We have encountered some problems in the selection of expansion packages today. For example, we chose elasticsearch PHP, which ranks first. However, after the installation of composer, we found that it did not provide the corresponding config file for configuration, which is not very friendly. Therefore, in the use of class library encapsulation API, we can also encapsulate several corresponding methods or instantiate classes, In order to facilitate the large-scale modification of the code when other class libraries may need to be replaced in the future.
PS: finally, the manual configuration of configuration files and single instance instantiation of ES are realized through laravel’s container injection
Tomorrow’s goal
Elasticsearch PHP basic API usage
summary
Here, the installation, configuration and simple use of elasticsearch are completed. We can set index as the database and type as the table name to manage the index of the data table.