PHP code example of taxusorg / xunsearch-laravel
1. Go to this page and download the library: Download taxusorg/xunsearch-laravel library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
taxusorg / xunsearch-laravel example snippets
'driver' => env('SCOUT_DRIVER', 'xunsearch'),
'server_host' => env('XUNSEARCH_SERVER_HOST', '127.0.0.1'),
'server_index_host' => env('XUNSEARCH_SERVER_INDEX_HOST', null),
'server_index_port' => env('XUNSEARCH_SERVER_INDEX_PORT', '8383'),
'server_search_host' => env('XUNSEARCH_SERVER_SEARCH_HOST', null),
'server_search_port' => env('XUNSEARCH_SERVER_SEARCH_PORT', '8384'),
'default_charset' => env('XUNSEARCH_DEFAULT_CHARSET', 'utf-8'),
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Blog extends Model
{
use Searchable;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use Taxusorg\XunSearchLaravel\XunSearchModelInterface;
class Blog extends Model implements XunSearchModelInterface
{
use Searchable;
public function xunSearchFieldsType()
{
return [
'id' => [
'type'=>self::XUNSEARCH_TYPE_NUMERIC,
],
'title' => [
'type'=>self::XUNSEARCH_TYPE_TITLE,
],
'body' => [
'type'=>self::XUNSEARCH_TYPE_BODY,
],
'field' => [
'tokenizer'=>self::XUNSEARCH_TOKENIZER_XLEN,
'tokenizer_value'=>2,
],
'created_at' => [
'type'=>self::XUNSEARCH_TYPE_DATE,
'index'=>self::XUNSEARCH_INDEX_NONE,
],
];
}
}
$builder = Blog::search();
$builder->getXSSearch()->addRange('id', 1, 50);
$blogs = $builder->get();
$results = Blog::search('test')->raw();
foreach ($results as $document) {
// XSDocument $document
}
$blogs = Blog::search('test')->get();
//------
$results = Blog::search('test')->raw();
$blogs = $results->getModels();
$results = Blog::search('word')->raw();
$blogs = $results->getModels(function ($query) {
/** @var \Illuminate\Database\Eloquent\Builder $query */
// $query->where(); // 添加查询条件
});
// 再次转换不添加查询条件
$blogs = $results->getModels();
$callback = function ($query) {
/** @var \Illuminate\Database\Eloquent\Builder $query */
// $query->where(); // 添加查询条件
}
$blogs = Blog::search('word')->query($callback)->get();
$result = Blog::search('word')->query($callback)->raw();
$blogs = $result->getModels();
// 以上都会调用 $callback。下边的形式 $callback 被临时覆盖,只调用后边传入的闭包。
$blogs = $result->getModels(function ($query) {
// $query->where(); // 添加查询条件
});
$callback = function ($query) {
// $query->where(); // 添加查询条件
}
$result = Blog::search('word')->query($callback)->raw();
// 传入闭包进行替换,且永久生效
$blogs = $result->query(function ($query) {
// $query->where(); // 添加查询条件
})->getModels();
use Illuminate\Database\Eloquent\Model;
use Taxusorg\XunSearchLaravel\XunSearchModelInterface;
use Taxusorg\XunSearchLaravel\XunSearchTrait;
class Blog extends Model implements XunSearchModelInterface
{
use XunSearchTrait;
public function xunSearchFieldsType()
{
// ...
}
}
Blog::search('word')->addRange('id', 20, 60)->get();
shell script
php artisan vendor:publish --provider="Taxusorg\XunSearchLaravel\XunSearchServiceProvider"