Use the Laravel Search extension package for full-text search based on Elasticsearch, Algolia, and Zendsearch

Source: Internet
Author: User
The Laravel Search extension package provides a unified API for different full-text search services, and the currently supported search services include ElasticSearch, Algolia, and Zendsearch.

1. Installation

We install this dependency package via composer:

Composer require Mmanos/laravel-search Dev-master

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

' providers ' = = Array (    //...    Mmanos\search\searchserviceprovider::class,)

Then add the façade to the aliases array:

' aliases ' = = Array (    //...    ') Search ' = ' mmanos\search\facade::class,)

2. Configuration

Publish the configuration file that comes with the extension package to the app configuration directory:

PHP Artisan Vendor:publish

Dependent

The following are the dependent packages required for the related search driver:

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

Default Index

The extension package provides a concise syntax to handle the default index: Edit the Default_index in the configuration file to modify this value.

3. Index Operation

Using this extension package to process the index is simple, just provide a unique identifier for the document and specify an associative array to that index.

If your first access to this index does not exist, the index is created automatically.

Index a document

Add a default index of 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 integer data that can be used to delete records or can be returned in search results.

storing additional parameters in the document

You can store additional parameters in the document for subsequent search results, which 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: The additional parameters are not indexed but are stored in the index for subsequent acquisition.

Delete a document

You can delete a document from the index by ID:

Search::d elete (1);

Delete an index

Search::d eleteindex ();

4. Search Operation

Search for documents

Search the default index for all records in the document that contain "Fox" in the content:

$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 a 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 the value is to 1, the more similar, the default is 0.5

using filters in Queries

You can also use filters in queries, and filters try to match the entire statement you specify:

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

Note: If you specify a value that contains more than one word, the filter does not guarantee the exact match of 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 above parameters represent latitude, longitude, and distance (meters), respectively.

Note: Currently only Algolia drivers support location-based search, and it is also necessary to ensure that each index record contains local information, such as: _ Geoloc = [' lat ' = = 1.23, ' LNG ' + 1.23].

restricting result sets

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

paging through the result set

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

limit the number of fields returned

$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 (Ten)    Get ();

Delete all documents that match a query

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

5. Processing Multiple Indexes

If you need to work with multiple indexes, you can use the above method as a single index after specifying the index name. 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 content contains Fox and the status of published posts:

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

Remove the 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 more control over the query, you can add a callback function before the query executes after all the criteria have been 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 callbacks 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 '] =;        return $query;    }, ' Elasticsearch ')    ->get ();

Note: You can also pass an array of drives as the second parameter.

  • Related Article

    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.