michelmelo/meilisearch-php

MeiliSearch API 的 PHP 封装

dev-main 2023-03-18 16:54 UTC

This package is not auto-updated.

Last update: 2024-09-15 21:59:06 UTC


README

MeiliSearch-PHP

MeiliSearch PHP

MeiliSearch | 文档 | Slack | 路线图 | 网站 | 常见问题解答

Latest Stable Version Test License Bors enabled

⚡ 为 PHP 编写的 MeiliSearch API 客户端 🐘

MeiliSearch PHP 是为 PHP 开发者提供的 MeiliSearch API 客户端。

MeiliSearch 是一个开源搜索引擎。 了解 MeiliSearch 是什么!

目录

📖 文档

查看我们的 文档 或我们的 API 参考

🔧 安装

要开始,只需使用 Composer 引入项目。
您还需要安装提供 psr/http-client-implementationpsr/http-factory-implementation 的包。
兼容的 HTTP 客户端和客户端适配器的列表可在 php-http.org 上找到。

如果您不知道要使用哪个 HTTP 客户端,我们建议使用 Guzzle 7:

composer require meilisearch/meilisearch-php guzzlehttp/guzzle http-interop/http-factory-guzzle:^1.0

以下是一个使用 symfony/http-client 的安装示例

composer require meilisearch/meilisearch-php symfony/http-client nyholm/psr7:^1.0

💡 更多与此软件包兼容的 HTTP 客户端安装方法可在 本节 中找到。

运行 MeiliSearch

有几种简单的方法可以 下载和运行 MeiliSearch 实例

例如,如果您使用 Docker

docker pull getmeili/meilisearch:latest # Fetch the latest version of MeiliSearch image from Docker Hub
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey

NB: 您还可以从 HomebrewAPT 下载 MeiliSearch。

🚀 入门

添加文档

<?php

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

use MeiliSearch\Client;

$client = new Client('http://127.0.0.1:7700', 'masterKey');

# An index is where the documents are stored.
$index = $client->index('books');

$documents = [
    ['book_id' => 123,  'title' => 'Pride and Prejudice', 'author' => 'Jane Austen'],
    ['book_id' => 456,  'title' => 'Le Petit Prince', 'author' => 'Antoine de Saint-Exupéry'],
    ['book_id' => 1,    'title' => 'Alice In Wonderland', 'author' => 'Lewis Carroll'],
    ['book_id' => 1344, 'title' => 'The Hobbit', 'author' => 'J. R. R. Tolkien'],
    ['book_id' => 4,    'title' => 'Harry Potter and the Half-Blood Prince', 'author' => 'J. K. Rowling'],
    ['book_id' => 42,   'title' => 'The Hitchhiker\'s Guide to the Galaxy', 'author' => 'Douglas Adams, Eoin Colfer, Thomas Tidholm'],
];

# If the index 'books' does not exist, MeiliSearch creates it when you first add the documents.
$index->addDocuments($documents); // => { "updateId": 0 }

使用 updateId,您可以使用 更新端点 检查您文档添加的状态(enqueuedprocessedfailed)。

基本搜索

// MeiliSearch is typo-tolerant:
$hits = $index->search('harry pottre')->getHits();
print_r($hits);

输出

Array
(
    [0] => Array
        (
            [id] => 4
            [title] => Harry Potter and the Half-Blood Prince
        )
)

自定义搜索

所有支持选项均在文档的 搜索参数 部分中描述。

💡 有关 search() 方法的更多信息,请参阅 Wiki

$index->search(
    'prince',
    [
        'attributesToHighlight' => ['*'],
        'filters' => 'book_id > 10'
    ]
)->getRaw(); // Return in Array format

JSON 输出

{
    "hits": [
        {
            "book_id": 456,
            "title": "Le Petit Prince"
        }
    ],
    "offset": 0,
    "limit": 20,
    "processingTimeMs": 10,
    "query": "prince"
}

🤖 与 MeiliSearch 的兼容性

本软件包仅保证与 MeiliSearch 的 版本 v0.20.0 兼容。

💡 更多信息

以下部分可能对您感兴趣

🧰 HTTP 客户端兼容性

您可以使用任何兼容PSR-18的客户端与该SDK一起使用。无需额外配置。
兼容的HTTP客户端和客户端适配器列表可以在php-http.org找到。

如果您想使用此meilisearch-php

  • guzzlehttp/guzzle(Guzzle 7)一起使用,请运行
composer require meilisearch/meilisearch-php guzzlehttp/guzzle http-interop/http-factory-guzzle:^1.0
  • php-http/guzzle6-adapter(Guzzle < 7)一起使用,请运行
composer require meilisearch/meilisearch-php php-http/guzzle6-adapter:^2.0 http-interop/http-factory-guzzle:^1.0
  • symfony/http-client一起使用,请运行
composer require meilisearch/meilisearch-php symfony/http-client nyholm/psr7:^1.0
  • php-http/curl-client一起使用,请运行
composer require meilisearch/meilisearch-php php-http/curl-client nyholm/psr7:^1.0
  • kriswallsmith/buzz一起使用,请运行
composer require meilisearch/meilisearch-php kriswallsmith/buzz nyholm/psr7:^1.0

自定义 HTTP 客户端

出于某种原因,您可能需要向自己的HTTP客户端传递自定义配置。
在初始化MeiliSearch客户端时,请确保您有一个兼容PSR-18的客户端。

按照入门部分的示例,使用Guzzle HTTP客户端

new Client('http://127.0.0.1:7700', 'masterKey', new GuzzleHttpClient(['timeout' => 2]));

⚙️ 开发工作流程和贡献

欢迎对这个项目有任何新的贡献!

如果您想了解更多关于开发工作流程或想做出贡献,请访问我们的贡献指南以获取详细说明!

MeiliSearch提供并维护了许多像这样的SDK和集成工具。我们希望为每个人提供任何类型项目的出色的搜索体验。如果您想做出贡献、提出建议或只是了解目前的情况,请访问我们的集成指南存储库。