2017 © Pedro Peláez
 

library laravel-search

A search package for Laravel 5.

image

mmanos/laravel-search

A search package for Laravel 5.

  • Thursday, June 29, 2017
  • by mmanos
  • Repository
  • 30 Watchers
  • 302 Stars
  • 38,890 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 62 Forks
  • 24 Open issues
  • 18 Versions
  • 4 % Grown

The README.md

Search Package for Laravel 5

This package provides a unified API across a variety of different full text search services. It currently supports drivers for Elasticsearch, Algolia, and ZendSearch (good for local use)., (*1)

Installation Via Composer

Add this to you composer.json file, in the require object:, (*2)

"mmanos/laravel-search": "dev-master"

After that, run composer install to install the package., (*3)

Add the service provider to app/config/app.php, within the providers array., (*4)

'providers' => array(
    // ...
    'Mmanos\Search\SearchServiceProvider',
)

Add a class alias to app/config/app.php, within the aliases array., (*5)

'aliases' => array(
    // ...
    'Search' => 'Mmanos\Search\Facade',
)

Laravel 4

Use the 0.0 branch or the v0.* tags for Laravel 4 support., (*6)

Configuration

Publish the default config file to your application so you can make modifications., (*7)

$ php artisan vendor:publish

Dependencies

The following dependencies are needed for the listed search drivers:, (*8)

  • ZendSearch: zendframework/zendsearch
  • Elasticsearch: elasticsearch/elasticsearch
  • Algolia: algolia/algoliasearch-client-php

Default Index

This package provides a convenient syntax for working with a "default" index. Edit the default_index field in the config file to change this value. If you need to work with more than one index, see Working With Multiple Indicies below., (*9)

Indexing Operations

Indexing is very easy with this package. Simply provide a unique identifier for the document and an associative array of fields to index., (*10)

The index will be created automatically if it does not exist the first time you access it., (*11)

Index A Document

Add a document to the "default" index with an id of "1"., (*12)

Search::insert(1, array(
    'title' => 'My title',
    'content' => 'The quick brown fox...',
    'status' => 'published',
));

Note: id may be a string or an integer. This id is used to delete records and is also returned in search results., (*13)

Store Extra Parameters With A Document

You may store extra parameters with a document so they can be retrieved at a later point from search results. This can be useful for referencing timestamps or other record identifiers., (*14)

Search::insert(
    "post-1",
    array(
        'title' => 'My title',
        'content' => 'The quick brown fox...',
        'status' => 'published',
    ),
    array(
        'created_at' => time(),
        'creator_id' => 5,
    )
);

Note: Extra parameters are not indexed but are stored in the index for future retrieval., (*15)

Delete A Document

Delete a document from the "default" index with an id of "1":, (*16)

Search::delete(1);

Delete An Index

Search::deleteIndex();

Search Operations

Search For A Document

Search the "default" index for documents who's content field contains the word "fox":, (*17)

$results = Search::search('content', 'fox')->get();

Search More Than One Field

$results = Search::search(array('title', 'content'), 'fox')->get();

Search All Fields

$results = Search::search(null, 'fox')->get();

Perform a fuzzy search to find results with similar, but not exact, spelling. For example, you want to return documents containing the word "updates" by searching for the word "update":, (*18)

$results = Search::search('content', 'update', array('fuzzy'=>true))->get();

Note: You may also pass a numeric value between 0 and 1 for the fuzzy parameter, where a value closer to 1 requires a higher similarity. Defaults to 0.5., (*19)

Apply A Filter To Your Query

You can apply filters to your search queries as well. Filters attempt to match the value you specify as an entire "phrase"., (*20)

$results = Search::search('content', 'fox')
    ->where('status', 'published')
    ->get();

Note: Filters do not guarantee an exact match of the entire field value if the value contains multiple words., (*21)

Geo-Search

Some drivers support location-aware searching:, (*22)

$results = Search::search('content', 'fox')
    ->whereLocation(36.16781, -96.023561, 10000)
    ->get();

Where the parameters are latitude, longitude, and distance (in meters)., (*23)

Note: Currently, only the algolia driver supports geo-searching. Ensure each indexed record contains the location information: _geoloc => ['lat' => 1.23, 'lng' => 1.23]., (*24)

Limit Your Result Set

$results = Search::search('content', 'fox')
    ->where('status', 'published')
    ->limit(10) // Limit 10
    ->get();

$results = Search::search('content', 'fox')
    ->where('status', 'published')
    ->limit(10, 30) // Limit 10, offset 30
    ->get();

Paginate Your Result Set

You can also paginate your result set using a Laravel paginator instance., (*25)

$paginator = Search::search('content', 'fox')->paginate(15);

Limit The Fields You Want Back From The Response

$results = Search::select('id', 'created_at')
    ->search('content', 'fox')
    ->get();

Chain Multiple Searches And Filters

$results = Search::select('id', 'created_at')
    ->where('title', 'My title')
    ->where('status', 'published')
    ->search('content', 'fox')
    ->search('content', 'quick')
    ->limit(10)
    ->get();

Note: Chained filters/searches are constructed as boolean queries where each must provide a match., (*26)

Delete All Documents That Match A Query

Search::search('content', 'fox')->delete();

Working With Multiple Indicies

If you need to work with more than one index, you may access all of the same methods mentioned above after you specify the index name., (*27)

Add a document to an index called "posts":, (*28)

Search::index('posts')->insert(1, array(
    'title' => 'My title',
    'content' => 'The quick brown fox...',
    'status' => 'published',
));

Search the "posts" index for documents who's content field contains the word "fox" and who's status is "published":, (*29)

$results = Search::index('posts')->search('content', 'fox')
    ->where('status', 'published')
    ->get();

Delete a document from the "posts" index with an id of "1":, (*30)

Search::index('posts')->delete(1);

Delete the entire "posts" index:, (*31)

Search::index('posts')->deleteIndex();

Advanced Query Callbacks

If you need more control over a search query you may add a callback function which will be called after all conditions have been added to the query but before the query has been executed. You can then make changes to the native query instance and return it to be executed., (*32)

$results = Search::index('posts')->select('id', 'created_at')
    ->search('content', 'fox')
    ->addCallback(function ($query) {
        // Make changes to $query...
        return $query;
    })
    ->get();

Since each driver has it's own native $query object/array, you may only want to execute your callback for one of the drivers:, (*33)

$results = Search::index('posts')->select('id', 'created_at')
    ->search('content', 'fox')
    ->addCallback(function ($query) {
        // Adjust pagination for an elasticsearch query array.
        $query['from'] = 0;
        $query['size'] = 20;
        return $query;
    }, 'elasticsearch')
    ->get();

Note: You may also pass an array of drivers as the second parameter., (*34)

The Versions

29/06 2017

dev-master

9999999-dev

A search package for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

29/06 2017

v1.0.5

1.0.5.0

A search package for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

29/06 2017

0.0.x-dev

0.0.9999999.9999999-dev

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

29/06 2017

v0.1.9

0.1.9.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

22/05 2016

v1.0.4

1.0.4.0

A search package for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

26/06 2015

v0.1.8

0.1.8.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

26/06 2015

v1.0.3

1.0.3.0

A search package for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

24/06 2015

v0.1.7

0.1.7.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

22/06 2015

v0.1.6

0.1.6.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

12/05 2015

v0.1.5

0.1.5.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

14/04 2015

v1.0.2

1.0.2.0

A search package for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

12/04 2015

v1.0.1

1.0.1.0

A search package for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

12/04 2015

v0.1.4

0.1.4.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene algolia zendsearch full

12/03 2015

v1.0.0

1.0.0.0

A search package for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene zendsearch full

11/03 2015

v0.1.3

0.1.3.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mark Manos

laravel search elasticsearch text lucene zendsearch full

14/08 2014

v0.1.2

0.1.2.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

by Mark Manos

laravel search elasticsearch text lucene zendsearch full

13/05 2014

v0.1.1

0.1.1.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

by Mark Manos

laravel search elasticsearch text lucene zendsearch full

10/05 2014

v0.1.0

0.1.0.0

A search package for Laravel 4.

  Sources   Download

MIT

The Requires

 

by Mark Manos

laravel search lucene