<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
public $timestamps = false;
/**
* Get the index name for the model.
*
* @return string
*/
public function searchableAs()
{
return 'posts_index';
}
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
$array = $this->toArray();
// Customize array...
return $array;
}
/**
* 获取模型主键
*
* @return mixed
*/
public function getScoutKey()
{
return $this->id;
}
}
控制器中
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Models\Product;
class IndexController extends Controller
{
public function index(){
echo "这是首页";
}
/**
* 添加索引
* @return [type]
*/
public function insertSingle(){
$Product = new Post();
$Product->name_zh = '中文名';
$Product->name_zh = '英文名';
$Product->save();
}
/**
* 通过查询添加es
* @return [type]
*/
public function select(){
Post::where('id','<',10)->searchable();
}
/**
* 通过查询删除es
* @return [type]
*/
public function delSelect(){
Post::where('id', '<', 5)->unsearchable();
}
/**
* 数据更新
* @return [type]
*/
public function updateSingle(){
$flight = Post::find(85);
$flight->name_zh = '中文名';
$flight->name_en = '英文名';
$flight->save();
}
/**
* 添加数据自动同步es文档
* http://127.0.0.1:9200/laravel56_shop/posts_index/85
* @return [type]
*/
public function insert(){
$Post = new Post();
$Post->name_zh = '中文名';
$Post->name_en = '英文名';
$Post->save();
}
/**
* es搜索
* @param Request
* @return [type]
*/
public function seacher(Request $request){
$keyword = $request->input('q');
// $posts=Post::search($keyword)->paginate(10)->toArray();
// var_dump($posts);
$info = Product::search($keyword)->where('status',2)->paginate(10)->toArray();
print_r($info);
}
}
elasticsearch在Laravel中的使用以及常用命令Model中<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Model;use Laravel\Scout\Searchable;class Post extends Model{ use Searchable; public $timestamps = false; /** * Get the index nam