编程

Manticore search 的官方 PHP 客户端

1190 2024-01-04 01:33:00

Manticore Search 是一款基于 C++ 的轻量级搜索引擎,号称比 Elasticsearch 快 10 倍,是 Elasticsearch 的替代品! 本文将介绍 Manticore search 的官方 PHP 客户端的安装与使用。

特性

  • 一对一匹配 HTTP API
  • 具有可插拔选择策略的连接池。默认为静态循环 
  • 插件式 PSR/Log 接口
  • 插件式传输协议
  • 持久连接

要求

PHP 7.1 以下,配置原生 JSON 扩展。默认传输 handler 使用 cURL 扩展。

支持的 Manticore Search 最低版本为 2.5.1,启用 HTTP 协议。

Manticore Searchmanticoresearch-phpPHP
>= 6.2.03.1.x>= 7.4, >=8.0
>= 6.2.03.0.x>= 7.4, >=8.0
>= 6.0.43.0.x>= 7.1, >=8.0
>= 4.2.12.0.x, 2.1.x, 2.2.x>= 7.1, >=8.0
>= 4.0.21.7.x, 1.8.x>= 7.1, >=8.0
>= 3.5.21.6.x>= 7.1, >=8.0
>= 3.5.01.5.x>= 7.1, <8.0
>= 3.5.01.4>= 7.1, <8.0
<= 3.4.x1.3>= 7.1, <8.0
>= 2.5.1, <3.4.01.2>= 7.1, <8.0

文档

Manticore Search 服务器文档: https://manual.manticoresearch.com/.

开始

使用 Composer 包管理器安装 Manticore Search PHP 客户端:

composer require manticoresoftware/manticoresearch-php

初始化 index:

require_once __DIR__ . '/vendor/autoload.php';

$config = ['host'=>'127.0.0.1','port'=>9308];
$client = new \Manticoresearch\Client($config);
$index = $client->index('movies');

创建 index:

$index->create([
    'title'=>['type'=>'text'],
    'plot'=>['type'=>'text'],
    'year'=>['type'=>'integer'],
    'rating'=>['type'=>'float']
    ]);

添加 document:

$index->addDocument([
        'title' => 'Star Trek: Nemesis',
        'plot' => 'The Enterprise is diverted to the Romulan homeworld Romulus, supposedly because they want to negotiate a peace treaty. Captain Picard and his crew discover a serious threat to the Federation once Praetor Shinzon plans to attack Earth.',
        'year' => 2002,
        'rating' => 6.4
        ],
    1);

一次性添加多个 documents:

$index->addDocuments([
        ['id'=>2,'title'=>'Interstellar','plot'=>'A team of explorers travel through a wormhole in space in an attempt to ensure humanity\'s survival.','year'=>2014,'rating'=>8.5],
        ['id'=>3,'title'=>'Inception','plot'=>'A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O.','year'=>2010,'rating'=>8.8],
        ['id'=>4,'title'=>'1917 ','plot'=>' As a regiment assembles to wage war deep in enemy territory, two soldiers are assigned to race against time and deliver a message that will stop 1,600 men from walking straight into a deadly trap.','year'=>2018,'rating'=>8.4],
        ['id'=>5,'title'=>'Alien','plot'=>' After a space merchant vessel receives an unknown transmission as a distress call, one of the team\'s member is attacked by a mysterious life form and they soon realize that its life cycle has merely begun.','year'=>1979,'rating'=>8.4]
    ]); 

执行搜索:

$results = $index->search('space team')->get();

foreach($results as $doc) {
   echo 'Document:'.$doc->getId()."\n";
   foreach($doc->getData() as $field=>$value)
   {   
        echo $field.": ".$value."\n";
   }
}

结果:

Document:2
year: 2014
rating: 8.5
title: Interstellar
plot: A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.

使用属性过滤器(filter)进行文本搜索:

$results = $index->search('space team')
                 ->filter('year','gte',2000)
                 ->filter('rating','gte',8.0)
                 ->sort('year','desc')
                 ->get();

foreach($results as $doc) {
    echo 'Document:'.$doc->getId()."\n";
    foreach($doc->getData() as $field=>$value)
    {   
        echo $field.": ".$value."\n";
    }
}

更新 document:

通过 document id:

$index->updateDocument(['year'=>2019],4);

通过查询:

$index->updateDocument(['year'=>2019],['match'=>['*'=>'team']]);

获取 index schema:

$index->describe();

删除 index:

$index->drop();

如果 index 不存在,则上述操作将失败。要绕过此问题,请传递一个参数 true,这将使得失败时保持静默。

$index->drop(true);

License

Manticore Search PHP Client 是一款 MIT 许可证下的开源软件