原文来自:https://jellybool.com/post/setup-slasticsearch-on-your-websitephp
在个人博客按Shift+S
就能够呼出搜索框,能够直接体验一下现实的Demojava
ElasticSearch
凭借强大的API和不俗的搜索性能,目前在搜索引擎领域的势头貌似愈来愈猛了,处于兴趣缘由,本身就花了点时间将本身的博客搜索插上了ElasticSearch
的翅膀。python
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer
由于ElasticSearch
底层其实就是lucene
,因此,须要java
git
若是以上第一行命令出现
command not found
,采起下面的解决方案,有两个须要注意的地方:github
sudo apt-get install python-software-properties sudo apt-get install software-properties-common
注:若是你想安装OpenJDK,请用如下命令,不过这个我并无亲自测试过。web
sudo apt-get update sudo apt-get install openjdk-8-jre-headless -y
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.7.0.deb sudo dpkg -i elasticsearch-1.7.0.deb
目前ElasticSearch
的最新稳定版为1.7.0
,若是以后版本有升级,请将相应的版本号替换掉上面的1.7.0
。json
你能够到这里查看:oracle
https://www.elastic.co/downloads/elasticsearchapp
安装之:composer
sudo dpkg -i elasticsearch-1.7.0.deb
sudo update-rc.d elasticsearch defaults 95 10 sudo /etc/init.d/elasticsearch start
curl http://localhost:9200
你将看到相似如下的信息:
{ "status" : 200, "version" : { "number" : "1.7.0", "build_timestamp" : "2015-07-16T14:31:07Z", "build_snapshot" : false, "lucene_version" : "4.10.4" }, "tagline" : "You Know, for Search" }
这样其实你已经将ElasticSearch
安装成功了。
固然,这些package
有几个都很不错,而我我的使用的是Bouncy在composer.json里添加该package
信息
"require": { "fadion/bouncy": "dev-l5" }
而后执行,composer update
添加 Service provider
来到config/app.php
,在providers
处添加:
'Fadion\Bouncy\BouncyServiceProvider',
最后执行:
php artisan vendor:publish
这样以后,你在config/
目录就会获得两个个配置文件
config/bouncy.php config/elasticsearch.php
第一个是关于Bouncypackage的简单配置,第二个是关于ElasticSearch
的具体配置,你能够直接打开来看看,若是没有什么必要,你能够目前保持文件不变。
安装完以后,咱们怎么使用呢?其实很简单,Bouncy提供一种很是简单的方式将Laravel的Eloquent Model
与ElasticSearch
关联起来--就只是使用一个trait
而已!好比,我须要用Article Model
做为示例:
use Fadion\Bouncy\BouncyTrait; class Article extends Eloquent { use BouncyTrait; // ...other Eloquent attributes // or methods. }
没错,这样就能够了。
这里咱们在ArticleController
实现索引全部的文章:
public function indexAllArticle() { return Article::all()->index(); }
嗯,就是这么简单的,就像正常使用Eloquent
同样,不过是在后面多使用一个index()
方法。
在这里的使用请确保你的ElasticSearch
服务是在正常运行期间。
并且还有一个好处就是,Bouncy在你开启auto_index
的状况下,会自动在你建立和保存文件的时候自动将文章索引了,至于更多详细的用法,你能够查看详细的文档。
<h1>Type something to search...</h1> {!! Form::open(['url'=>'/search','method'=>'get']) !!} <div class="form-group"> <input class="form-control" autofocus="true" name="query" id="query" type="text"/> </div> {!! Form::close() !!}
咱们在blade文
件中建立一个搜索表单,并指定表单的提交方式为GET
。
Route::get('/search','ArticleController@search');
因为咱们是直接使用GET
的方式来传递参数,因此这里的示例仅是一些简单的代码:
public function search() { if ( isset($_GET['query']) && !empty($_GET['query']) ) { $query = $_GET['query']; } $params = [ 'query' => [ 'match' => [ 'title' => $query ] ], 'highlight' => [ 'fields' => [ 'title' => new \stdClass ] ], 'size' => 20 ]; $articles = Article::search($params); return view('article.search', compact('articles')); }
在这里咱们只是指定了对文章的title
进行匹配,你也能够将content
做为匹配域。而highlight
选项的配置是为了在视图中高亮匹配的内容:
{!! $article->highlight('title') !!}
这样,一个完整的搭建ElasticSearch
的过程就完成了。
在实现的过程当中,貌似ElasticSearch对中文的支持不是那么好,因此接下来会尝试使用一下中文分词器来看看效果,顺利的话会再出一篇文章。
Happy Hacking