Code Monkey home page Code Monkey logo

ppshobi / psonic Goto Github PK

View Code? Open in Web Editor NEW
128.0 7.0 9.0 157 KB

Sonic is a super fast auto suggestion engine built by the team at crisp.chat, a customer engagement platform. its built in Rust and they officially support a javascript client, but if you want to use sonic via php, this is the library that you can look for. Completely unit tested, and adheres to modern coding standards, and offers a clean api to interact with sonic.

License: MIT License

PHP 100.00%
sonic php autocomplete rust-lang

psonic's Introduction

Psonic - PHP client for sonic auto suggestion engine

codecov

.github/workflows/tests.yml

Summary

Sonic is a super fast auto-suggestion engine built by the team at crisp.chat, a customer engagement platform. it is built in Rust and they officially support a javascript client, but if you want to use sonic via PHP, this is the library that you can look for. Completely unit tested, and adheres to modern coding standards, and offers a clean API to interact with sonic.

Installation & Usage

you need a running sonic instance (locally or in the cloud, the port 1491 should be accessible) php7+ and composer to make this library work. Read more on installing sonic

  • goto your project directory
  • execute composer require ppshobi/psonic once the installation is completed you should be able to use the library as follows

Api Documentaion

Full API Documentaion

Usage

once you have psonic in place, you have access to the Client and Channel classes, Each channel instance requires a separate new client instance since sonic doesn't allow channel switching within the same connection. Read more about sonic concepts below

Indexing

To index few objects into the sonic use the following sample code, make sure you have a running instance of sonic on port 1491

$ingest  = new Psonic\Ingest(new Psonic\Client('localhost', 1491, 30));
$control = new Psonic\Control(new Psonic\Client('localhost', 1491, 30));
$ingest->connect('SecretPassword1');
$control->connect('SecretPassword1');
echo $ingest->push('messagesCollection', 'defaultBucket', "1234","hi Shobi how are you?")->getStatus(); // OK
echo $ingest->push('messagesCollection', 'defaultBucket', "1235","hi are you fine ?")->getStatus(); //OK
echo $ingest->push('messagesCollection', 'defaultBucket', "1236","Jomit? How are you?")->getStatus(); //OK

echo $control->consolidate(); // saves the data to disk

$ingest->disconnect();
$control->disconnect();

Searching/AutoSuggestions

To search on the index using the following sample code

$search = new Psonic\Search(new Psonic\Client('localhost', 1491, 30));
$search->connect('SecretPassword1');
var_dump($search->query('messagesCollection', 'defaultBucket', "are")); // you should be getting an array of object keys which matched with the term "are"
$search->disconnect();

To get autosuggestions/autocomplete for a term from the index use the following sample code

$search = new Psonic\Search(new Psonic\Client('localhost', 1491, 30));
$search->connect('SecretPassword1');
var_dump($search->suggest('messagesCollection', 'defaultBucket', "sho")); // you should be getting an array of terms which matched the term "sho". Considering previous example and it should output "shobi"
$search->disconnect();

Basic sonic Concepts

Sonic is more of an identifier index than a document index. Meaning, if the query matches some records it will be giving you the identifier of the matched object, than the object itself. Probably you will have to query the actual data store again with those keys. Check Basic Terminology used in sonic below as well. Read more on sonic repository

Channels

Sonic doesn't offer an HTTP endpoint as of now, rather it offers a TCP endpoint like Redis (They call it RESP protocol), and we call it channel. There are 3 kind of channels

  • Ingest (Typically offers data indexing (index), deindexing (pop) and flushing operations)
  • Search (Offers Query and Suggest operations)
  • Control (Offers the collection control operations such as data consolidation)

Basic Terminology

Consider you are storing the chats of your customers from an e-commerce website.

  • collection - which contains all your messages/products etc...
  • bucket - you might need to store messages specific to a user so that a collection can contain one or more user buckets, so the search can be more specific, or according to your use case, you can put all your messages in one bucket itself and name it default or generic etc..
  • object - The object is the key of the actual data that you have in the database, usually, the object key will be an identifier/primary_key from the source of truth, as the primary key from the messages table, this will be returned when you query matches some records from the sonic index.
  • terms - This is the actual text data you save in the sonic. Read more on the sonic documentation

Testing & Contribution

To run sonic in local, the best way is to use docker

Run below command in terminal. you should have a sonic instance running.

$ docker run -d -p 1491:1491 -v /path/to/sonic.cfg:/etc/sonic.cfg -v /path/to/sonic/data/store:/var/lib/sonic/store/ valeriansaliou/sonic:v1.1.9

Then do a git clone of this project

$ cd ~ && git clone https://github.com/ppshobi/psonic.git

then cd into the project directory and do a composer install

$ cd psonic && composer install

Use phpunit to run tests.

Feel free to send pull requests.

psonic's People

Contributors

dependabot[bot] avatar forgetmiss avatar lens0021 avatar manavo avatar ppshobi avatar skluck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

psonic's Issues

Migrate to PHP v7.4

I moved to new version of PHP (v.7.4) and the library is failing with the changes of the new api. In particular in line psonic/src/Commands/Command.php#20. This is due to the recent change in the implode() method, so now the arguments are backwards.

The only change to apply in this case is the code return $this->command . " " . implode($this->parameters, " ") . "\n"; move it to return $this->command . " " . implode(" ", $this->parameters) . "\n";

That should solve the issue while the library is being adapted to new version. Could you post a new release with this fix?

Drop support for php 7.x

as all php 7.x version are currently out of support. I suggest offering support only for PHP 8.x. Some progress is already made here in this PR #38. People with php7.x will still be able to use the existing psonic 2.x releases. Before providing the support only to PHP 8.x, please comment if anyone has a different opinion.

Expecting to release the new version by March 31, 2023

Sonic Password still not working

Hi

Still getting errors when setting a password on the channel connection.
In this case:

    /**
     * @param string $collection
     * @param string $bucket
     * @param string $key
     * @param string $text
     * @return array
     */
    private function splitString(string $collection, string $bucket, string $key, string  $text): array
    {
        return str_split($text, ($this->bufferSize - (strlen($key . $collection . $bucket) + 20)));
    }
}

str_split(): The length of each segment must be greater than zero

I thought at the beginning it was a malconfiguration on the client but when I set the password back to the default one (SecretPassword) worked like a charm. Do you have an idea what it can be?

Why can't search some records ?

$ingest  = new Psonic\Ingest(new Psonic\Client('localhost', 1491, 30));
$control = new Psonic\Control(new Psonic\Client('localhost', 1491, 30));
$ingest->connect('SecretPassword1');
$control->connect('SecretPassword1');
echo $ingest->push('messagesCollection', 'defaultBucket', "1234","hi Shobi how are you?")->getStatus(); // OK
echo $ingest->push('messagesCollection', 'defaultBucket', "1235","hi are you fine ?")->getStatus(); //OK
echo $ingest->push('messagesCollection', 'defaultBucket', "1236","Jomit? How are you?")->getStatus(); //OK

echo $control->consolidate(); // saves the data to disk

$ingest->disconnect();
$control->disconnect();


$search = new Psonic\Search(new Psonic\Client('localhost', 1491, 30));
$search->connect('SecretPassword1');
var_dump($search->query('messagesCollection', 'defaultBucket', "are")); // you should be getting an array of object keys which matched with the term "are"
$search->disconnect();

Why are there only 1235 and 1236, not 1234?

PHP Fatal error: Uncaught ValueError: str_split(): Argument #2 ($length) must be greater than 0

Brew package installation.

Sample code as per instructions.

Stack trace:
#0 /Users/...: str_split('hi Shobi how ar...', -55)
#1 /Users/..../src/Ingest.php(55): Psonic\Ingest->splitString('messagesCollect...', 'defaultBucket', '1234', 'hi Shobi how ar...')
#2 /Users/.../test.php(10): Psonic\Ingest->push('messagesCollect...', 'defaultBucket', '1234', 'hi Shobi how ar...')
#3 {main}
thrown in /..../psonic/src/Ingest.php on line 146

Without $password for Psonic/Client

Inside Api-Docs

<?php

$search = new Psonic/Search(new Psonic/Client($host, $port, $password, $timeout));
echo $search->connect();

But in Client.php, this construct signature.

public function __construct($host = 'localhost', $port = 1491, $timeout = 30)

Some specific reason for help in this case ?

PHP 8 support

Hi,

Just wondering if the library will be updated to support PHP 8.
It probably works out of the box, but the composer package is set to require PHP ~7.0.

Thanks for the library!

Password set on Ingest/Control connect()

I am trying to set a custom password different from the default one 'SecretPassword', however, everytime the connection fails with 'fwrite' error.

Is it really possible to set that password?

Server response is not complete

When I set a big query limitation,I found the result of the response is not complete.
I check the code:

./Client.php:
56: $message = stream_get_line($this->resource, 2048, "\r\n");

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.