First, install the Java SE environment

Install the Java JDK and configure the JAVA_HOME environment variable before installing the Elasticsearch engine:

1, from www.oracle.com/technetwork… Download and install the Java SE development kit. I downloaded the latest version as shown below:

ElasticSearch is sensitive to the JRE version. An incorrect version will cause ElasticSearch to fail to run. To download the Archive JRE, download the appropriate version from the Java Archive.

2. After the Java SE development package is installed, create the JAVA_HOME environment variable on the server

Environment variable JAVA_HOME, set the variable value to: C:\Program Files\Java\ JDK-11.0.2

On Windows, the “% environment variable name %” is used to obtain the value of the specified environment variable. The JAVA_HOME environment variable is used because the Java SE development package is required for installing ElasticSearch.

Install ElasticSearch

www.elastic.co/downloads/p… Download the corresponding version I downloaded 5.4.0

* Use the version of PHP that corresponds to ElasticSearch!

The ElasticSearch service is enabled

Bat file to install elasticSearch. Wait for a moment, open your browser, enter http://localhost:9200, and display the following screen: ES is successfully installed.

Install Chinese participle IK

Download the ik github.com/medcl/elast…

My ES version is 5.4.0IK select 5.4.0 must find the corresponding version !!!!!!

Download it and unzip it into a folder



Create an ik folder for elasticSearch-5.4.0plugins \ik

Restart ElasticSearch after success ok!! ElasticSearch service installed successfully!!

Install ElasticSearch within the project

composer require elasticsearch/elasticsearch
Copy the code

The appropriate version will be automatically loaded! Version 5.4.0 for ElasticSearch

Now elasticSearch and PHP support are installed!

Easy to use in ThinkPHP5

The methods below are some of the official sample integrations

<? php namespace app\index\controller; require '.. /vendor/autoload.php'; use Elasticsearch\ClientBuilder; class Search { private $client; Public function __construct() {$params = array('127.0.0.1:9200'); $params = array('127.0.0.1:9200'); $this->client = ClientBuilder::create()->setHosts($params)->build(); Public function index() {$r = $this->delete_index(); $r = $this->create_index(); $r = $this->create_mappings(); $r = $this->get_mapping(); $docs = []; $docs [] = [' id '= > 1,' name '= >' Ming ', 'profile' = > 'UI strong invincible. I do', 'age' = > 23]. $docs [] = [' id '= > 2,' name '= >' zhang ', 'profile' = > 'my PHP code invulnerable.', 'age' = > 24]; $docs [] = [' id '= > 3,' name '= >' wang ', 'profile' = > 'C life, happy every day.', 'age' = > 29]. $docs [] = [' id '= > 4,' name '= >' xiao zhao ', 'profile' = > 'no I can't do the front end of the page.', 'age' = > 26]; $docs [] = [' id '= > 5,' name '= >' xiao wu ', 'profile' = > 'PHP is one of the best language.', 'job' = > 21]. $docs [] = [' id '= > 6,' name '= >' small weng ', 'profile' = > 'don't bother me, I'm knock bug!', 'age' = > 25]; $docs [] = [' id '= > 7,' name '= >' xiao Yang ', 'profile' = > ', do not delete library to run road ', 'age' = > 27]. foreach ($docs as $k => $v) { $r = $this->add_doc($v['id'],$v); } $r = $this->search_doc($this->search_doc); Public function create_index($params = ['index' => $index_name) {$params = ['index' => $index_name);  'body' => [ 'settings' => [ 'number_of_shards' => 5, 'number_of_replicas' => 0 ] ] ]; try { return $this->client->indices()->create($params); } catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { $msg = $e->getMessage(); $msg = json_decode($msg,true); return $msg; Public function delete_index($index_name = 'test_ik') {$params = ['index' => $index_name]; $response = $this->client->indices()->delete($params); return $response; Public function create_mappings($type_name = 'users',$index_name = 'test_ik') {$params = ['index' => $index_name, 'type' => $type_name, 'body' => [ $type_name => [ '_source' => [ 'enabled' => true ], 'properties' = > [' id' = > [' type '= >' integer ', / / integer 'index' = > 'not_analyzed'], 'name' = > [' type '= >' string ', / / string type 'index' = > 'analyzed, / / full-text search' analyzer '= >' ik_max_word '], 'profile' = > [' type '= >' string ', 'index' => 'analyzed', 'analyzer' => 'ik_max_word' ], 'age' => [ 'type' => 'integer', ], ] ] ] ]; $response = $this->client->indices()->putMapping($params); return $response; Public function get_mapping($type_name = 'users',$index_name = 'test_ik') {$params = ['index' => $index_name, 'type' => $type_name ]; $response = $this->client->indices()->getMapping($params); return $response; Public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => $doc ]; $response = $this->client->index($params); return $response; Public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->exists($params); return $response; Public function get_doc($id = 1,$type_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->get($params); return $response; Public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') $params = [' index '= > $index_name,' type '= > $the type_name,' id '= > $id,' body '= > [' doc' = > [' name '= >' king ']]]. $response = $this->client->update($params); return $response; } public function delete_doc($id = 1,$type_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name, 'type' => $type_name, 'id' => $id ]; $response = $this->client->delete($params); return $response; } // Query documents (paging, sorting, weight, Public function search_doc($index_name = "test_ik",$type_name = "users",$from = 0,$size = 2) { $params = [ 'index' => $index_name, 'type' => $type_name, 'body' => [ 'query' => [ 'bool' => [ 'should' => [ [ 'match' => [ 'profile' => [ 'query' => $keywords, 'boost' => 3, / / heavy]]], [' match '= > [' name' = > [' query '= > $keywords,' boost '= > 2,]]],]],], 'sort' => ['age'=>['order'=>'desc']] , 'from' => $from, 'size' => $size ] ]; $results = $this->client->search($params); return $results; }}Copy the code

Step 1,2,3,4 to import the database data.

Search successful!