ElasticSearch与PHP交互
转眼间我们已经对es有一定的了解,接下来我们来了解es与编程语言的交互,由于es大多数是与Java做交互且小编是PHPer,故在此创建
一篇es与php相关文档。
安装
需满足以下条件
PHP 7.0.0 或更高版本
Composer
ext-curl:PHP 的 Libcurl 扩展(详情查看下方注意事项)
原生 JSON 扩展 (ext-json) 1.3.7或更高版本
安装方式:
way-one:
根据官方文档进行安装
地址:https://www.elastic.co/guide/cn/elasticsearch/php/current/_installation_2.html
way-two:
用composer package 进行安装
composer require elasticsearch/elasticsearch +version
注:
composer 安装自行百度。
es-php版本最好与es本身一致。以免出现意想不到的错误。
语法使用
加载 es-php
使用命名空间加载
引入自动加载(如果集成在框架中不必引入,直接用use即可)
require 'vendor/autoload.php';
引入es-php处理类
use Elasticsearch\ClientBuilder;
创建es实例对象
ClientBuilder::create()
->setHosts()
->setElasticMetaHeader()
->includePortInHostHeader()
->setRetries()
->setLogger()
->build();
设置主机
$host = ['http://1x1.3x.8x.1x8:9200', '1xx.3x.8x.1x8:9200',]
日志,存储地址
$logger = new Logger('es-monolog');
$logger -> pushHandler(new StreamHandler($saveFile, Logger::WARNING));
设置日志文件地址
create() -- 创建
setHosts($host) -- 设置连接主机,多个地址,号隔开
setElasticMetaHeader(false|true) -- 是否显示头部
includePortInHostHeader(false|true) -- 用于启用Host表头中的端口
setRetries(5) -- 设置重连次数
setLogger($logger) -- 自定义文件地址 -- (php -Monolog 自行百度)
build() -- 生成连接器
创建索引
注:由于在第二节,ElasticSearch----语法学习中学习过相应语法,故此处只用PHP简单举例且语法与es-json语法相似
相应解释
索引参数
paramData -- 创建索引的参数
创建索引时使用的方法
$this->_esConnection->indices()->create($paramData)
方法具体含义
api-文档
https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-create-index.html
代码详情可用icode进行点击查看
$paramData = [
'index' => 'my_index', // 索引民
'body' =>[
"settings" => [ // 主分片设置
"number_of_shards" => 1, // 分片存储个数
"number_of_replicas" => 1, // 主片存储个数
// 定制化分词器
"analysis" => [
'char_filter' => [ // 词性预处理
'&_to_end' => [ // 过滤名称
'type' => 'mapping',
'mappings' => ['&=> and'],
],
],
'filter' => [ // 词性过滤器
'my_stopwords' => [ //过滤名称
'type' => 'stop', // 类型
'stopwords' => ['the', 'a'] // 过滤词
],
],
'analyzer' => [ // 自定义分词器
'my_analyzer'=> [ // 分词性名称
'type' => 'custom',
'char_filter' => ['html_strip', '&_to_end'], // 预处理方法
'tokenizer' => 'standard', // 分词类型
'filter' => ['lowercase', 'my_stopwords'] // 过滤次方法
],
],
]
],
"mappings" => [ //字段映射
"properties" => [
"title" => [ // 字段名称
'type' => 'keyword', // 字段类型
]

最低0.47元/天 解锁文章
1201

被折叠的 条评论
为什么被折叠?



