Official repository of the Sajari SDK for PHP
PHP Shell
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
src/Sajari
.gitignore
LICENSE.md
README.md
composer.json
generateProto.sh
sami.cfg.php

README.md

Sajari PHP SDK

Packagist license

The Sajari PHP SDK enables use of the Sajari platform from PHP.

We recommend using the Generated search interface, Javascript or React SDKs if you're serving up search results in a web browser:

  • Avoids backend integration.
  • Minimises latency by sending queries directly to our servers instead of routing via your infrastructure.
  • Provides automatic real-time learning using user interactions and other metrics.

Table of Contents

Setup

Requires PHP 5.5+, 7.0+.

  1. Get Composer.
  2. Get the gRPC extension by running sudo pecl install grpc.
  3. Add extension=grpc.so to your php.ini file.
  4. Run php composer.phar install.

Note A more complete guide to installing the gRPC extension can be found in the gRPC PHP README.

Using with Composer

Add sajari/sajari-sdk-php to your composer.json:

{
  "require": {
    "sajari/sajari-sdk-php": "v2.0.1"
  }
}

Getting Started

Here are a few simple code snippets that will help get you up and running. See the full documentation for a more complete overview.

Creating a Client

To start we need to create a client to make calls to the API:

$client = new Client('your-project', 'your-collection', [
    new WithKeyCredentials('your-key-id', 'your-key-secret')
]);

Adding a record

A record can be added to a collection using the add method:

$record = [
    "title" => "The Three Musketeers",
    "slug" => "the-three-musketeers",
    "author" => "Alexandre Dumas",
    "price" => 10.00,
    "qty" => 7,
];
$key = $client->add($record);

An exception will be thrown if an error occurred.

If the add is successful, a $key (instance of the class Key) is returned which uniquely defines the newly inserted record. This can be used in calls to get, delete and mutate to operate on that record in the collection. Keys can be defined on any unique field. Each collection has the unique field _id which is set by the system when records are added. Unique fields can also be created using the API.

Adding multiple records

Multiple records can be added in one call. It's easy to retrieve the keys from each of the add operations (and check that they succeeded).

$records = [
    [
        "title" => "The Three Musketeers",
        "slug" => "the-three-musketeers",
        "author" => "Alexandre Dumas",
        "price" => 10.00,
        "qty" => 7,
    ],
    [
        "title" => "The Remains of the Day",
        "slug" => "the-remains-of-the-day",
        "author" => "Kazuo Ishiguro",
        "price" => 8.00,
        "qty" => 10,
    ],
    [
        "title" => "1984",
        "slug" => "1984",
        "author" => "George Orwell",
        "price" => 15.00,
        "qty" => 0,
    ]
];

$resps = $client->addMulti($records);

foreach($resps as $resp) {
    if ($resp->isError()) {
       echo "error adding record: " . $resp->getStatus() . "\n";
       continue;
    }
    echo $resp->getKey() . "\n";
}

Getting a record

A record can be retrieved from a collection using a Key.

$client->get($client->key("slug", "the-three-musketeers"));

An exception will be thrown if an error occurred.

Getting multiple records

Multiple records can also be fetched in one call using keys.

$keys = $client->keys("slug", [
    "the-three-musketeers",
    "the-remains-of-the-day",
    "1984",
]);

$resps = $client->getMulti($keys);

foreach($resps as $resp) {
    if ($resp->isError()) {
       echo "error fetching record: " . $resp->getStatus() . "\n";
       continue;
    }
    print_r($resp->getRecord());
}

Deleting a record

A record can be deleted from a collection using a Key.

$client->delete($client->key("slug", "1984"));

An exception will be thrown if an error occurred.

Deleting multiple records

$keys = $client->keys("slug", [
    "the-three-musketeers",
    "the-remains-of-the-day",
    "1984",
]);

$resps = $client->deleteMulti($keys);

foreach($resps as $resp) {
    if ($resp->isError()) {
       echo "error deleting record: " . $resp . "\n";
    }
}

Editing a record

A record can be edited using a Key and an associative array of field-value pairs to overwrite existing field values.

$client->edit(
    $client->key("slug", "the-remains-of-the-day"),
    [ "qty" => 10 ]
);

Editing multiple records

$keys = $client->keys("slug", [
    "the-three-musketeers",
    "the-remains-of-the-day",
    "1984",
]);

$setFields = [
    ["title" => "The Three Musketeers (Original French)"],
    ["qty" => 10],
    ["title" => "George Orwell's 1984"],
];

$resps = $client->editMulti($keys, $setFields);

foreach($resps as $resp) {
    if ($resp->isError()) {
       echo "error editing record: " . $resp . "\n";
    }
}

Retrieving a collection schema

$client->schema()->getFields()

Querying

Pipelines

Pipelines are the recommended way to query your collection. They wrap up lots of our more complex functionality into a simple interface. We offer a few standard pipelines for specific purposes, eg website for querying website collections. If you created your collection using the "custom" option in the console, use the raw pipeline.

$results = $client->pipeline("books")->search([
    "q" => "musketeers"
]);

Raw search API

It's also possible to run queries using the raw query API.

$client->Search(new Request("1984"))

Documentation

The full documentation is available online.

It can also be built locally using Sami:

  1. Move to the sajari-sdk-php root directory and run curl -O http://get.sensiolabs.org/sami-v3.3.0.phar
  2. Run php sami-v3.3.0.phar update sami.cfg.php

This will generate the documentation and put it in build/.

License

We use the MIT License.