PHPackages                             davin-bao/laravel-xun-search - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. davin-bao/laravel-xun-search

ActiveLibrary[Search &amp; Filtering](/categories/search)

davin-bao/laravel-xun-search
============================

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

124010[1 issues](https://github.com/davin-bao/laravel-xun-search/issues)HTML

Since Mar 30Pushed 9y ago2 watchersCompare

[ Source](https://github.com/davin-bao/laravel-xun-search)[ Packagist](https://packagist.org/packages/davin-bao/laravel-xun-search)[ RSS](/packages/davin-bao-laravel-xun-search/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel 5.1 XunSearch
=====================

[](#laravel-51-xunsearch)

[![Latest Stable Version](https://camo.githubusercontent.com/fb7eb0176a28b141bd9e100664360b83d2bcee246b0932d8696668909ccd0730/68747470733a2f2f706f7365722e707567782e6f72672f646176696e2d62616f2f6c61726176656c2d78756e2d7365617263682f762f737461626c652e706e67)](https://packagist.org/packages/davin-bao/laravel-xun-search)[![Latest Unstable Version](https://camo.githubusercontent.com/6fe1b98781defd39bdb8ad18fbe383c950465bef6f5273025d77b6eae64eb6dd/68747470733a2f2f706f7365722e707567782e6f72672f646176696e2d62616f2f6c61726176656c2d78756e2d7365617263682f762f756e737461626c652e706e67)](https://packagist.org/packages/davin-bao/laravel-xun-search)[![License](https://camo.githubusercontent.com/4764297cb7088f8675edc980d7d2a751b9a6a5ff5cfb5d792eae62d2d8e02186/68747470733a2f2f706f7365722e707567782e6f72672f646176696e2d62616f2f6c61726176656c2d78756e2d7365617263682f6c6963656e73652e706e67)](https://packagist.org/packages/davin-bao/laravel-xun-search)[![Total Downloads](https://camo.githubusercontent.com/d3f83ce918c43774b6a41f4ca33f210873f4e1572c8e8c45def21444d63079ff/68747470733a2f2f706f7365722e707567782e6f72672f646176696e2d62616f2f6c61726176656c2d78756e2d7365617263682f646f776e6c6f616473)](https://packagist.org/packages/davin-bao/laravel-xun-search)

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

Installation
------------

[](#installation)

Require this package in your composer.json and run composer update:

```
{
	"require": {
        "davin-bao/laravel-xun-search": "dev-master"
	}
}
```

After updating composer, add the ServiceProvider to the providers array in `app/config/app.php`

```
'providers' => [
	DavinBao\LaravelXunSearch\ServiceProvider::class,
],
```

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

```
'aliases' => [
	'Search' => DavinBao\LaravelXunSearch\Facade::class,
],
```

Configuration
-------------

[](#configuration)

Publish the config file into your project by running:

```
php artisan vendor:publish --provider="DavinBao\LaravelXunSearch\ServiceProvider"
```

\###Basic In published config file add descriptions for models which need to be indexed, for example:

```
//@see http://www.xunsearch.com/doc/php/guide/ini.guide
"project" => [
    "project.name" => "demo",
    "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"
    ],
    "username" => [
        "type" => "title"
    ],
    "email" => [
        "index" => "both"
    ],
    "last_seen" => [
        "type" => "numeric"
    ],
    "role" => [
        "index" => "both"
    ],
    "uri" => [
        "index" => "both"
    ],
    "action" => [
        "index" => "both"
    ],
],

'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
-----

[](#usage)

### Artisan commands

[](#artisan-commands)

#### Initialize or rebuild search index

[](#initialize-or-rebuild-search-index)

For building of search index run:

```
php artisan search:rebuild --verbose
```

#### Clear search index

[](#clear-search-index)

For clearing of search index run:

```
php artisan search:clear
```

#### Filtering of models in search results

[](#filtering-of-models-in-search-results)

For filtering of models in search results each model's class can implements `SearchableInterface`. For example:

```
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

[](#partial-updating-of-search-index)

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

```
    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

[](#query-building)

Build query in several ways:

#### Using constructor:

[](#using-constructor)

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

##### Simple queries

[](#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

[](#getting-of-results)

For built query are available following actions:

#### Get all found models

[](#get-all-found-models)

```
$models = $query->search();
```

#### Get all found models ID

[](#get-all-found-models-id)

```
$models = $query->getIDList();
```

#### Get count of results

[](#get-count-of-results)

```
$count = $query->count();
```

#### Get limit results with offset

[](#get-limit-results-with-offset)

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

#### Sort

[](#sort)

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

### Highlighting of matches

[](#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.

```
$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
-------

[](#license)

Package licenced under the MIT license.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d4bcfd83f5b742d5aeddbc12fdcd74806a7b79d770b613a707fe49bc74cd3d9?d=identicon)[davin-bao](/maintainers/davin-bao)

---

Top Contributors

[![davin-bao](https://avatars.githubusercontent.com/u/5162830?v=4)](https://github.com/davin-bao "davin-bao (6 commits)")

### Embed Badge

![Health badge](/badges/davin-bao-laravel-xun-search/health.svg)

```
[![Health](https://phpackages.com/badges/davin-bao-laravel-xun-search/health.svg)](https://phpackages.com/packages/davin-bao-laravel-xun-search)
```

###  Alternatives

[ruflin/elastica

Elasticsearch Client

2.3k50.4M203](/packages/ruflin-elastica)[opensearch-project/opensearch-php

PHP Client for OpenSearch

15024.3M65](/packages/opensearch-project-opensearch-php)[mailerlite/laravel-elasticsearch

An easy way to use the official PHP ElasticSearch client in your Laravel applications.

934529.3k2](/packages/mailerlite-laravel-elasticsearch)[massive/search-bundle

Massive Search Bundle

721.4M13](/packages/massive-search-bundle)[outl1ne/nova-multiselect-filter

Multiselect filter for Laravel Nova.

45802.7k3](/packages/outl1ne-nova-multiselect-filter)[handcraftedinthealps/zendsearch

a general purpose text search engine written entirely in PHP 5

39921.0k35](/packages/handcraftedinthealps-zendsearch)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
