2017 © Pedro Peláez
 

library sherlock

PHP Client for Elasticsearch

image

sherlock/sherlock

PHP Client for Elasticsearch

  • Monday, July 7, 2014
  • by polyfractal
  • Repository
  • 11 Watchers
  • 124 Stars
  • 136,095 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 34 Forks
  • 25 Open issues
  • 19 Versions
  • 4 % Grown

The README.md

Sherlock

Sherlock is a PHP (>=5.3.9) client for ElasticSearch. Sherlock can be used to search and manage ElasticSearch clusters., (*1)

Sherlock is Unmaintained! DO NOT USE!

Sherlock is no longer maintained and should not be considered a usable Elasticsearch client for anything remotely resembling a production environment. It is not compatible with Elasticsearch 1.0+, nor does it have complete API coverage of Elasticsearch 0.90.x, (*2)

Please use the official Elasticsearch client (which I wrote, and maintaine): https://github.com/elasticsearch/elasticsearch-php, (*3)

The official client is actively maintained and well-tested against ES and PHP versions., (*4)

If you are interested in updating Sherlock to use the official client, please open a ticket and let me know! I'd love to see the syntax of Sherlock on top of the new client...but I just don't have time myself., (*5)

Features

  • One-to-one mapping with ElasticSearch's API and query DSL.
  • Option to use powerful ORM-like interface, associative arrays or raw JSON
  • Concurrent request streaming with RollingCurl and cURL multi-handle
  • Autodetection of cluster nodes and subsequent round-robin requests to the entire cluster
  • Configurable logging capabilities

Disclaimer

Sherlock should be considered Alpha status. Use in production at your own risk! The current 0.1 branch is in maintenance mode - I'm only fixing bugs and accepting PRs., (*6)

The new 0.2 branch that is coming will break backwards compatibility in places. The new 0.2 branch, however, should be a lot easier to maintain and I'll likely be promoting it to true 1.0 status soon after release (along with associated SemVer, backwards compatibility, etc)., (*7)

Resources

Installation via Composer

The recommended method to install Sherlock is through Composer., (*8)

  1. Add sherlock/sherlock as a dependency in your project's composer.json file:, (*9)

    {
        "require": {
            "sherlock/sherlock": "~0.1.0"
        }
    }

    Note: If you would like to use the latest stable development version, specify dev-master in your composer file instead of 0.1.*. Master is kept at the most recent, stable development version but may not be tagged and pushed to Packagist yet. Unstable dev versions are kept secluded in the develop branch., (*10)

  2. Download and install Composer:, (*11)

    curl -s http://getcomposer.org/installer | php
  3. Install your dependencies:, (*12)

    php composer.phar install
  4. Require Composer's autoloader, (*13)

    Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process:, (*14)

    <?php
    require 'vendor/autoload.php';

You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at getcomposer.org., (*15)

Manual Installation

Sherlock can be installed even if you don't use Composer. Download Sherlock and include the following in your index.php or equivalent, (*16)

        <?php
        require 'Sherlock/Sherlock.php';
        \Sherlock\Sherlock::registerAutoloader();

Usage

The library interface is still under flux...this section will be updated once Sherlock has been fleshed out a bit more., (*17)

   require 'vendor/autoload.php';
   use \Sherlock\Sherlock;

   //The Sherlock object manages cluster state, builds queries, etc
   $sherlock = new Sherlock();

   //Add a node to our cluster.  Sherlock can optionally autodetect nodes given one starting seed
   $sherlock->addNode('localhost', 9200);

   //Build a new search request
   $request = $sherlock->search();

   //Set the index, type and from/to parameters of the request.
   //The query is at the end of the chain, although it could be placed anywhere
   $request->index("test")
            ->type("tweet")
            ->from(0)
            ->size(10)
            ->query(Sherlock::query()->Term()->field("message")
                                              ->term("ElasticSearch"));

   //Execute the search and return results
   $response = $request->execute();

   echo "Took: ".$response->took."\r\n";
   echo "Number of Hits: ".count($response)."\r\n";

   //Iterate over the hits and print out some data
   foreach($response as $hit)
   {
      echo $hit['score'].' - '.$hit['source']['message']."\r\n";
   }


   //Let's try a more advanced query now.
   //Each section is it's own variable to help show how everything fits together
   $must = Sherlock::query()->Term()->field("message")
                                     ->term("ElasticSearch");

   $should = Sherlock::query()->Match()->field("author")
                                        ->query("Zachary Tong")
                                        ->boost(2.5);

   $must_not = Sherlock::query()->Term()->field("message")
                                           ->term("Solr");

   $bool = Sherlock::query()->Bool->must($must)
                                   ->should($should)
                                   ->must_not($must_not);
   $request->query($bool);
   $request->execute();

Other types of queries

You can use Sherlock with every type of query listed in the elasticsearch docs. E.g. if you'd like to use a fuzzy like this (flt) query, you can build your query like this:, (*18)

    $sherlock = new Sherlock();
    $sherlock->addNode('localhost', 9200);
    $request = $sherlock->search();

    $request->index('test')
            ->type('tweet')
            ->query(Sherlock::queryBuilder()
                ->FuzzyLikeThis()
                ->fields( array('description', 'tags', 'name') )
                ->like_text( $query )
                ->min_similarity( 0.6 )
            );

    $response = $request->execute();

Filters

Building filters is identical to building queries, but requires the use of filterBuilder() instead of queryBuilder(). Again, a simple example would be:, (*19)

    $request->index('test')
        ->type('tweet')
        ->filter(Sherlock::filterBuilder()
            ->Term()
            ->field($type)
            ->term($value)
        );

    $response = $request->execute();

Non-ORM style

Not a fan of ORM style construction? Don't worry, Sherlock supports "raw" associative arrays, (*20)

    //Build a new search request
    $request = $sherlock->search();

    //We can compose queries using hashmaps instead of the ORM.
    $manualData = array("field" => "field1", "term" => "town");

    $request->query(Sherlock::query()->Term($manualData));

Need to consume and use raw JSON? No problem, (*21)

    //Build a new search request
    $request = $sherlock->search();

    //We can compose queries using hashmaps instead of the ORM.
    $json = '{ "term" : { "field1" : "town" } }';

    $request->query(Sherlock::query()->Raw($json));

(There will be a RawQuery method soon, that lets you construct entirely arbitrary queries with arrays or JSON), (*22)

For more examples check out the Quickstart Guide, (*23)

Philosophy

Sherlock aims to continue the precendent set by ElasticSearch: work out of the box with minimal configuration and provide a simple interface., (*24)

Sherlock's API uses a "fluent" interface that relies heavily on method chaining and type hinting for brevity. The developer should never need to stop and look up a class name to instantiate, or dig through docs to remember which class accepts which arguments., (*25)

Secondary to the interface comes Sherlock developer sanity: reduce code as much as possible. Rather than write a million getter/setter functions to expose ElasticSearch's various parameters, Sherlock relies heavily upon templates, auto-generated class stubs, magic methods and PHPdoc., (*26)

The Versions

07/07 2014

dev-master

9999999-dev https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

26/10 2013

v0.1.16

0.1.16.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

10/07 2013

v0.1.15

0.1.15.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

10/07 2013

v0.1.14

0.1.14.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

11/06 2013

v0.1.13

0.1.13.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

16/04 2013

v0.1.12

0.1.12.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

05/04 2013

v0.1.11

0.1.11.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

03/04 2013

v0.1.10

0.1.10.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

03/04 2013

v0.1.9

0.1.9.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

24/03 2013

v0.1.8

0.1.8.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

16/03 2013

v0.1.7

0.1.7.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

12/03 2013

v0.1.6

0.1.6.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

12/03 2013

v0.1.5

0.1.5.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

08/03 2013

v0.1.4

0.1.4.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

08/03 2013

v0.1.3

0.1.3.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

25/02 2013

v0.1.2

0.1.2.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

25/02 2013

v0.1.1

0.1.1.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client

24/02 2013

v0.1.0

0.1.0.0 https://sherlockphp.org

PHP Client for Elasticsearch

  Sources   Download

MIT

The Requires

 

The Development Requires

search elasticsearch client