Elasticsearch-PHP简易使用(注:只做笔记-简易手册使用)

  • 2019 年 12 月 18 日
  • 笔记

一.composer安装

composer require elasticsearch/elasticsearch

二.引入elasticsearch-php

require 'vendor/autoload.php';

use ElasticsearchClientBuilder;

三.实例化

//配置方式一

$hosts = [

// '192.168.1.1:9200',   // IP + Port

// '192.168.1.2',   // Just IP

// 'mydomain.server.com:9201', // Domain + Port

// 'mydomain2.server.com',   // Just Domain

// 'https://localhost',   // SSL to localhost

// 'https://192.168.1.3:9200'  // SSL to IP + Port

];

//配置方式二

$hosts = [

[

"host" => "localhost",

"port" => "9200",

// "scheme" => "http",

// "user" => "",

// "pass" => "",

],

];

//实例化

$client = ClientBuilder::create()->setHosts($hosts)->build();

所有操作基于$client

四.笔记说明

官方文档:https://www.elastic.co/guide/cn/elasticsearch/php/current/index.html

//创建

$params = ["index" => "table","type" => "database","id" => "id","body" => ["field1" => "value1","field2" => "value2"]];

$response = $client->index($params);

//获取

$params = ["index" => "table","type" => "database","id" => "id"];

$response = $client->get($params);

//更新

$params = ["index" => "table","type" => "database","id" => "id","body" => ["doc" => ["field1" => "new value1","field2" => "new value2"] ]];

$response = $client->update($params);

//删除

$params = ["index" => "table","type" => "database","id" => "id"];

$response = $client->delete($params);

//match 查询 (全文索引 汉字基于 单个文字 全文搜索,英文 以空格分出的单词  全文搜索)

$params = ["index" => "table","type" => "database","body" => ["query" => ["match" =>["field1" => "value1"]]]];

$response = $client->search($params);

//可以使用json字符串替代body

$json = '{"query":{"match":{"field1":"value1"}}}';

$params = ["index" => "table","type" => "database","body" => $json];

$response = $client->search($params);

//match_phrase 查询 (类似sql %like%)

$params = ["index" => "table","type" => "database","body" => ["query" => ["match_phrase" =>["field1" => "value1"]]]];

$response = $client->search($params);

//bool 查询 

// must 查询 类似 and

$params = ["index" => "table","type" => "database","body" => ["query" => ["bool" => ["must" => [["match" =>["field1" => "value1"]],["match" =>["field2" => "value2"]]]]]]];

$response = $client->search($params);

// should 查询 类似 or

$params = ["index" => "table","type" => "database","body" => ["query" => ["bool" => ["should" => [["match" =>["field1" => "value1"]],["match" =>["field2" => "value2"]]]]]]];

$response = $client->search($params);

可读样式

//创建  $params = [     "index" => "table",     "type" => "database",     "id" => "id",     "body" => [       "field1" => "value1",       "field2" => "value2"     ]  ];  $response = $client->index($params);  //获取  $params = [     "index" => "table",     "type" => "database",     "id" => "id"  ];  $response = $client->get($params);  //更新  $params = [     "index" => "table",     "type" => "database",     "id" => "id",     "body" => [       "doc" => [         "field1" => "new value1",         "field2" => "new value2"       ]     ]  ];  $response = $client->update($params);  //删除  $params = [     "index" => "table",     "type" => "database",     "id" => "id"  ];  $response = $client->delete($params);  //match 查询 (全文索引 汉字基于 单个文字 全文搜索,英文 以空格分出的单词  全文搜索)  $params = [     "index" => "table",     "type" => "database",     "body" => [       "query" => [         "match" =>[           "field1" => "value1"         ]       ]     ]  ];  $response = $client->search($params);  $json = '{     "query":{       "match":{         "field1":"value1"       }     }  }';  $params = [     "index" => "table",     "type" => "database",     "body" => $json  ];  $response = $client->search($params);  //match_phrase 查询 (类似sql %like%)  $params = [     "index" => "table",     "type" => "database",     "body" => [       "query" => [         "match_phrase" =>[           "field1" => "value1"         ]       ]     ]  ];  $response = $client->search($params);  //bool查询  // must 查询 类似 and  $params = [     "index" => "table",     "type" => "database",     "body" => [       "query" => [         "bool" => [           "must" => [             [               "match" =>[                 "field1" => "value1"               ]             ],             [               "match" =>[                 "field2" => "value2"               ]             ]           ]         ]       ]     ]  ];  $response = $client->search($params);  // should 查询 类似 or  $params = [     "index" => "table",     "type" => "database",     "body" => [       "query" => [         "bool" => [           "should" => [             [               "match" =>[                 "field1" => "value1"               ]             ],             [               "match" =>[                 "field2" => "value2"               ]             ]           ]         ]       ]     ]  ];

$response = $client->search($params);

官方文档:https://www.elastic.co/guide/cn/elasticsearch/php/current/index.html