前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch-PHP简易使用(注:只做笔记-简易手册使用)

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

作者头像
93年的老男孩
发布2019-12-18 17:30:42
5790
发布2019-12-18 17:30:42
举报

一.composer安装

composer require elasticsearch/elasticsearch

二.引入elasticsearch-php

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

三.实例化

//配置方式一

$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);

可读样式

代码语言:javascript
复制
//创建
$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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档