manticoresoftware/manticoresearch-php

Manticore Search 的 PHP 客户端

3.1.0 2023-09-15 10:43 UTC

README

Build Status Scrutinizer Code Quality codecov.io Latest Stable Version License Slack

Total Downloads Monthly Downloads Daily Downloads composer.lock

GitHub Code Size GitHub Repo Size GitHub Last Commit GitHub Activity GitHub Issues

Manticore Search 的官方 PHP 客户端。

❗ 注意:这是客户端的开发版本。最新版本的说明文档位于 https://github.com/manticoresoftware/manticoresearch-php/tree/3.1.0

特性

  • 与 HTTP API 一对一映射
  • 具有可插拔选择策略的连接池。默认为静态轮询
  • 可插拔 PSR/Log 接口
  • 可插拔传输协议。
  • 持久连接

要求

需要 PHP 7.1 或更高版本以及本机 JSON 扩展。默认传输处理程序使用 cURL 扩展。

最低 Manticore Search 版本为 2.5.1,并启用了 HTTP 协议。

文档

完整文档可在 docs 文件夹中找到。

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

入门指南

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

composer require manticoresoftware/manticoresearch-php

初始化索引

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

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

创建索引

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

添加文档

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

一次性添加多个文档

$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.

带有属性过滤器的文本搜索

$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";
    }
}

更新文档

通过文档 ID

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

通过查询

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

获取索引模式

$index->describe();

删除索引

$index->drop();

如果索引不存在,上述操作将失败。为了解决这个问题,请传递一个为 true 的参数,这将使失败变为静默。

$index->drop(true);

许可证

Manticore Search PHP 客户端是开源软件,许可协议为 MIT 许可证