jsidorenko / RedCard-Autocomplete by jsidorenkko
forked from rishair/php-redis-autocomplete

Autocomplete implementation using PHP and Redis
17
0
2
Package Data
Maintainer Username: jsidorenkko
Maintainer Contact: azza@jcaks.net (Aaron Florey)
Package Create Date: 2016-03-07
Package Last Update: 2016-04-04
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2025-01-28 15:16:19
Package Statistics
Total Downloads: 17
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

RedCard Autocomplete

Autocomplete implementation using PHP+Redis.

Inspired by https://github.com/seatgeek/soulmate

This library handles a basic implementation of autocomplete with sorted results (according to "scores") as well as arbitrary metadata for results. Also has the ability to separate different autocomplete databases in to "bins" (e.g. have separate bins "users" and another for "videos" so when querying against "users" it doesn't show results from "videos")

Installation

Add mochaka/redcard as a requirement to composer.json:

{
    "require": {
        "jsidorenko/redcard": "dev-master"
    }
}

Update your packages with composer update or install with composer install.

Integration

You will need to create a Predis Client Instance and provide it to the autocomplete class.

    $redis = new Predis\Client(array(
        'scheme' => 'tcp',
        'host'   => 'localhost',
        'port'   => 6379,
    ));

    $autocomplete = new JSidorenko\RedCard\RedisAutocomplete( $redis );
    or
    $autocomplete = new JSidorenko\RedCard\RedisAutocomplete( $redis, "yourDomainPrefix" );

    instead of "yourDomainPrefix" you can write something like "users" or "locations"

Basic Usage

To store data you must have a unique ID for an item and the phrase that should be searchable.


$autocomplete->store(2, "cat");
$autocomplete->store(3, "care");
$autocomplete->store("MYCRAZYID", "caress");
$autocomplete->store(55, "cars");
$autocomplete->store(6, "camera");

$results = $autocomplete->find("car");

var_dump($results)

Bins

Different types of data can be distinguished from one another through bins. Each bin has its own name and when searching and removing they will not conflict with one another.


$autocomplete->store(2, "Mary", "users");
$autocomplete->store(3, "Sally", "users");
$autocomplete->store(4, "Leo", "users" );

$autocomplete->store(5, "Mary Had A Litte Lamb", "blog-title");
$autocomplete->store(6, "Redis Rocks, A Life Story", "blog-title");

$results = $autocomplete->find("Mary", "users");

// Will only return Mary instead of "Mary Had A Litte Lamb"

Interface

The basic functions that you need to be aware of to utilize RedCard.

  • store: store a new item to autocomplete

       store($id, $phrase, $bin = '', $score = 1, $data = NULL)
    ``
    
    xample
    ``php
       $autocomplete->store('id123', "Clockwork Orange", "Books", 3, array('author'=>'Anthony Burgess'))
    ``
    
    
  • find: find an item. Searches are cached in a seperate hash.

       find($phrase, $bin = '', $count = 10, $isCaching = true)
    ``
    
    xample
    ``php
       $autocomplete->find("Clock", "Books", 1, true)
    ``
    
    
  • find by ID: find an item by provided ID. Returns null if nothing found.

       findByID($id, $bin = '')
    ``
    
    xample
    ``php
       $autocomplete->findByID('id123')
    ``
    
    
  • remove: remove an item from a bin. Searches are cached in a separate hash.

       remove($id, $bin = '')
    ``
    
    xample
    ``php
       $autocomplete->remove('id123', 'Books')
    ``
    
    
    
  • clear: clear all items.

       clear()
    ``
    
    xample
    ``php
       $autocomplete->clear()
    ``
    
    
    
    

License

RedCard Autocomplete is licensed under the MIT License. Original Copyright (c) 2011 Rishi Ishairzay, released under the MIT license