Use the LaravelSearch extension package to implement full-text search based on Elasticsearch, Algolia, and ZendSearch

Source: Internet
Author: User
Using the LaravelSearch extension package based on Elasticsearch, Algolia, and ZendSearch, the Laravel Search extension package provides unified APIs for different full-text Search services, currently, ElasticSearch, Algolia, and ZendSearch are supported.

1. Installation

We use Composer to install this dependency package:

composer require mmanos/laravel-search dev-master

After the installation is complete, register the service provider in config/app. php to the providers array:

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

Then add the facade to the aliases array:

'aliases' => array(    // ...    'Search' => Mmanos\Search\Facade::class,)
2. configuration

Release the configuration file that comes with the extension package to the application configuration Directory:

php artisan vendor:publish
Dependency

The following are the dependent packages required by the search driver:

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

The extension package provides a concise syntax to process default indexes: edit default_index in the configuration file to modify this value.

3. index operations

Using this extension package to process the index is very simple. you only need to provide a unique identifier for the document and specify an associated array to the index.

If you access this index for the first time, the index is automatically created.

Index a document

Add a default index with id 1 to the document:

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

Note: The id may be a string or an integer. this id can be used to delete records or return results in search results.

Store additional parameters in the document

You can store additional parameters in the document so that they can be obtained in subsequent search results. this feature is useful when referencing timestamps or other identity records.

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

Note: Additional parameters are not indexed but stored in the index for later retrieval.

Delete a document

You can delete a document by id from the index:

Search::delete(1);
Delete an index
Search::deleteIndex();
4. search operations Search Documents

Search for all records whose content contains "fox" in the document corresponding to the default index:

$results = Search::search('content', 'fox')->get();
Search multiple fields
$results = Search::search(array('title', 'content'), 'fox')->get();
Search all fields
$results = Search::search(null, 'fox')->get();
Perform fuzzy search
$results = Search::search('content', 'update', array('fuzzy'=>true))->get();

Note: You can also pass a value between 0 and 1 to the fuzzy parameter. the closer this value is to 1, the more similar it is. the default value is 0.5.

Use filters in queries

You can also use a filter in the query to try to match the entire statement you specified:

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

Note: if the specified value contains multiple words, the filter cannot accurately match the entire field value.

(Geo-Search) Location Search

Some drivers support location-based search:

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

The preceding parameters indicate the latitude, longitude, and distance (meter ).

Note: Currently, only the algolia driver supports location-based search. In addition, make sure that each index record contains local information, such as _ geoloc => ['lat' => 1.23, 'lng '= & gt; 1.23].

Limit 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();
Pagination of result sets
$paginator = Search::search('content', 'fox')->paginate(15);
Limit the number of returned fields
$results = Search::select('id', 'created_at')    ->search('content', 'fox')    ->get();
Multiple search and filter chains
$results = Search::select('id', 'created_at')    ->where('title', 'My title')    ->where('status', 'published')    ->search('content', 'fox')    ->search('content', 'quick')    ->limit(10)    ->get();
Delete all documents that match a query
Search::search('content', 'fox')->delete();
5. process multiple indexes

If you need to process multiple indexes, you can use the above method after the specified index name, just like a single index. Add the document to the index named "posts:

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

Query posts whose content contains fox and whose status is published:

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

Delete a document with id 1 from posts:

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

Delete all posts:

Search::index('posts')->deleteIndex();
6. Advanced Query callback

If you want to have more control over the query, you can add a callback function before the query execution after all the conditions are added to the query:

$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 its own $ query object/array, you can perform a callback for only one driver:

$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 can also pass a driver array as the second parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.