2017 © Pedro Peláez
 

library tipask-xunsearch

Laravel 5 package for full-text search over Eloquent models based on XunSearch.

image

sdfsky/tipask-xunsearch

Laravel 5 package for full-text search over Eloquent models based on XunSearch.

  • Friday, June 1, 2018
  • by sdf_sky
  • Repository
  • 1 Watchers
  • 1 Stars
  • 602 Installations
  • HTML
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 0 Open issues
  • 3 Versions
  • 26 % Grown

The README.md

Laravel 5.1 XunSearch

Latest Stable Version Latest Unstable Version License Total Downloads, (*1)

Laravel 5.1 package for full-text search over Eloquent models based on XunSearch., (*2)

Installation

Require this package in your composer.json and run composer update:, (*3)

{
    "require": {
        "sdfsky/tipask-xunsearch": "dev-master"
    }
}

After updating composer, add the ServiceProvider to the providers array in app/config/app.php, (*4)

'providers' => [
    Sdfsky\TipaskXunSearch\ServiceProvider::class,
],

If you want to use the facade to search, add this to your facades in app/config/app.php:, (*5)

'aliases' => [
    'Search' => Sdfsky\TipaskXunSearch\Facade::class,
],

Configuration

Publish the config file into your project by running:, (*6)

php artisan vendor:publish --provider="Sdfsky\TipaskXunSearch\ServiceProvider"

Basic

In published config file add descriptions for models which need to be indexed, for example:, (*7)


//@see http://www.xunsearch.com/doc/php/guide/ini.guide "project" => [ "project.name" => "tipask", "project.default_charset" => "utf-8", "server.index" => "127.0.0.1:8383", "server.search" => "127.0.0.1:8384", //remember change FIELD_LABEL_DEFAULT_SEARCH_PK value in Config.php "primary_key" => [ "type" => "id" ], //remember change FIELD_LABEL_DEFAULT_CLASS_ID value in Config.php "class_uid" => [ "index" => "both" ], //remember change FIELD_LABEL_DEFAULT_DB_PK value in Config.php "id" => [ "type" => "numeric" ], "subject" => [ "type" => "title" ], "status" => [ 'type' => "numeric" ], "content" => [ "type" => "body" ] ], 'index' => [ // ... namespace\FirstModel::class => [ 'fields' => [ 'name', 'full_description', // fields for indexing ], 'primary_key' => 'id' //primary_key name in DB, default 'id' ], namespace\SecondModel::class => [ 'fields' => [ 'name', 'short_description', // fields for indexing ] ], // ... ],

Usage

Artisan commands

Initialize or rebuild search index

For building of search index run:, (*8)

php artisan search:rebuild --verbose

Clear search index

For clearing of search index run:, (*9)

php artisan search:clear

Filtering of models in search results

For filtering of models in search results each model's class can implements SearchableInterface. For example:, (*10)


use Illuminate\Database\Eloquent\Model; use DavinBao\LaravelXunSearch\Model\SearchableInterface; class Dummy extends Model implements SearchableInterface { // ... /** * Get id list for all searchable models. */ public static function searchableIds() { return self::wherePublish(true)->lists('id'); } // ... }

Partial updating of search index

For register of necessary events (save/update/delete) use DavinBao\LaravelXunSearch\Model\SearchTrait in target model:, (*11)


use Illuminate\Database\Eloquent\Model; use DavinBao\LaravelXunSearch\Model\SearchableInterface; use DavinBao\LaravelXunSearch\Model\SearchTrait; class Dummy extends Model implements SearchableInterface { use SearchTrait; // ... }

Query building

Build query in several ways:, (*12)

Using constructor:

By default, queries which will execute search in the phrase entirely are created., (*13)

Simple queries
$query = Model::getSearch()->addQuery("clock"); // search by all fields.
// or 
$query = Model::getSearch()->addQuery('name:clock'); // search by 'name' field.
// or
$query = Model::getSearch()->addQuery('name:clock'); // filter by 'short_description' field.

$Ids = Model::getSearch()->addQuery('name:clock')->getIDList(); // filter by 'short_description' field.

Getting of results

For built query are available following actions:, (*14)

Get all found models

$models = $query->search();

Get all found models ID

$models = $query->getIDList();

Get count of results

$count = $query->count();

Get limit results with offset

$models = $query->limit(5, 10)->get(); // Limit = 5 and offset = 10

Sort

$query = $query->setSort('chrono', true);

Highlighting of matches

Highlighting of matches is available for any html fragment encoded in utf-8 and is executed only for the last executed request., (*15)


$docs = $search->setQuery('测试')->setLimit(5)->search(); foreach ($docs as $doc) { $subject = $search->highlight($doc->subject); // 高亮处理 subject 字段 $message = $search->highlight($doc->message); // 高亮处理 message 字段 echo $doc->rank() . '. ' . $subject . " [" . $doc->percent() . "%] - "; echo date("Y-m-d", $doc->chrono) . "\n" . $message . "\n"; }

#

License

Package licenced under the MIT license., (*16)

The Versions

01/06 2018

dev-master

9999999-dev

Laravel 5 package for full-text search over Eloquent models based on XunSearch.

  Sources   Download

MIT

The Requires

 

by Avatar sdf_sky

laravel xunsearch tipask

01/06 2018

v1.0.1

1.0.1.0

Laravel 5 package for full-text search over Eloquent models based on XunSearch.

  Sources   Download

MIT

The Requires

 

by Avatar sdf_sky

laravel xunsearch tipask

07/05 2018

v1.0.0

1.0.0.0

Laravel 5 package for full-text search over Eloquent models based on XunSearch.

  Sources   Download

MIT

The Requires

 

by Avatar sdf_sky

laravel xunsearch tipask