ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP-elasticsearch/elasticsearch包分词功能实现及源码解析

2022-01-19 11:34:16  阅读:201  来源: 互联网

标签:index endpoint 源码 elasticsearch indices params PHP php


安装

直接使用 composer 安装 ES 包就可以了,这里使用官方的 elasticsearch/elasticsearch 这个包。

composer require elasticsearch/elasticsearch

安装好以后,创建一个客户端。hosts如果是多个节点的集群,那么可以配置一个二维数组。

$hosts = [
            'host' => '127.0.0.1',
            'port' => '9200',
            'scheme' => 'http',
            'user' => '',
            'pass' => ''
        ];
$client = ClientBuilder::create()  //创建客户端
                        ->setHosts($hosts) //hosts连接地址
                        ->build();

如果想跳过ssl证书校验,可以添加一些curl的参数放进客户端

$curlParams = [//不校验ssl
                CURLOPT_SSL_VERIFYPEER => 0,
                CURLOPT_SSL_VERIFYHOST => 0,
            ];
$client = ClientBuilder::create()
            ->setConnectionParams(['client' => ['curl' => $curlParams]]) //设置curl参数
            ->setHosts($hosts)
            ->build();

分词

简单的增删改查在 elasticsearch 的文档中有介绍了,就不说了,可以看(https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html)[文档]。

分词的话隐藏的比较深,文档中没有介绍,他放在了indices这个 namespace 下面。如果看源码,可以在Endpoints/Indices 目录下面发现 Analyze.php 文件,当然了,除了分词,这里面还有其他功能,可以自己看。

这个文件也很简单啊,只有几个函数,就是设置请求的API地址,参数这些。

使用起来是这样的,我们用上面创建好的客户端。

$parmas['index'] = 'test' //这个是分词的index,也可以不加,加了请求的API就是 $index/_analyze
//请求体 这个就和你直接写DSL没区别了,参数啥的都一样,可以在 kibana里面试试参数
$parmas['body'] = [
  'text' => 'php开发', //要分词的文字
  'analyzer' => 'ik_smart', //分词器,可以不写
]; 
$client->indices()->analyze($params);

扩展

indices函数返回的就是 indices 文件夹的 namespace,对应文件在namespace/IndicesNamespace.php,然后后面的函数就相当于文件名,她会拼接在 indices 后面,像我们上面请求的文件就是indices/Analyze.php。具体的拼接就是在 namespace/IndicesNamespace.php 这个里面做的,有一个函数如下

/**
  * $params['index'] = (string) The name of the index to scope the operation
  * $params['body']  = (array) Define analyzer/tokenizer parameters and the text on which the analysis should be performed
  *
  * @param array $params Associative array of parameters
  * @return array
  * @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-analyze.html
  */
public function analyze(array $params = [])
{
    //获取$params['index'], $params['body']
    $index = $this->extractArgument($params, 'index');
    $body = $this->extractArgument($params, 'body');

    //设置endpoints处理类
    $endpointBuilder = $this->endpoints;
    //$endpoint 就相当于 indices/Analyze.php 这个文件了
    $endpoint = $endpointBuilder('Indices\Analyze');
    //设置参数,index, 请求体
    $endpoint->setParams($params);
    $endpoint->setIndex($index);
    $endpoint->setBody($body);
    //发起请求
    return $this->performRequest($endpoint);
}

$this->endpoints 是一个函数,外面传进来的,函数内容如下

$this->endpoint = function ($class) use ($serializer) {
  //拼接处理类
  $fullPath = '\\Elasticsearch\\Endpoints\\' . $class;
  //反射获取
  $reflection = new ReflectionClass($fullPath);
  $constructor = $reflection->getConstructor();
  //执行
  if ($constructor && $constructor->getParameters()) {
      return new $fullPath($serializer);
  } else {
      return new $fullPath();
  }
};

标签:index,endpoint,源码,elasticsearch,indices,params,PHP,php
来源: https://blog.csdn.net/Thepatterraining/article/details/122576959

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有