brokerexchange/elasticscout

Laravel Scout Driver for Elasticsearch 6 and 7

v10.1.0 2019-08-16 13:54 UTC

README

Latest Stable Version Latest Unstable Version Total Downloads License composer.lock

ElasticScout

Elasticsearch 6 的 Laravel Scout 驱动

概述

ElasticScout 是一个与 Elasticsearch 6 兼容的 Laravel Scout 引擎。它对旧的 Elasticseach Scout Engine 进行了关键性的修改,并增加了新功能。

ElasticScout 引擎包括一个 Elasticsearch Query Builder,可用于创建复杂的自定义查询和聚合,允许在 Laravel/Scout 体系结构内充分利用 Elasticsearch。

许可

ElasticScout 在 MIT 开源许可证下发布,https://open-source.org.cn/licenses/MIT

版权

ElasticScout © Broker Exchange Network 2018

安装

  • 运行 composer require 命令 composer require brokerexchange/elasticscout
  • 配置 Elasticsearch 主机(默认:localhost:9200)
   ELASTICSEARCH_HOST='elastic1.host.com:9200,elastic2.host.com:9200'
  • 将特质添加到所需模型中
   use ElasticScout\Searchable;

用法

   //create search/query object
   $search = $article->search()
       ->boolean()
       ->should(DSL::match('title',$request->input('query')))
       ->should(DSL::match('body',$request->input('query')))
       ->highlight(['body','title'])
       ->filter(DSL::term('published', 1))
       ->aggregate(Agg::terms('categories', 'category.name'));
       
   //fire the search
   $articles = $search->paginate();
       
   //retrieve aggregation results
   $categories = $search->aggregation('categories');
   
   //retrieve highlight results for title field of first result article
   $firstArticleTitleHighlights = $articles->first()->highlight('title');
   

映射

您可以通过在模型上定义 "mapping" 方法来设置自定义映射。

   public function mappings()
   {
       return [
           $this->searchableType() => [
               'properties' => [
                   'name' => [
                       'type' => 'text',
                       'fields' => [
                           'keyword' => [
                               'type' => 'keyword',
                           ],
                           'autocomplete' => [
                               'type' => 'text',
                               'analyzer' => 'autocomplete',
                               'search_analyzer' => 'autocomplete_search',
                           ],
                       ],
                   ],
               ]
           ]
       ]
   }

设置

您可以在模型上创建 "settings" 方法来创建自定义设置和分析器。

   public function settings()
   {
     return [
         'index' => [
             'analysis' => [
                 'analyzer' => [
                     'autocomplete' => [
                         'tokenizer' => 'autocomplete',
                         'filter' => [
                             'lowercase',
                         ],
                     ],
                     'autocomplete_search' => [
                         'tokenizer' => 'lowercase',
                     ],
                 ],
                 'tokenizer' => [
                     'autocomplete' => [
                         'type' => 'edge_ngram',
                         'min_gram' => 1,
                         'max_gram' => 15,
                         'token_chars' => [
                             'letter'
                         ]
                     ]
                 ]
             ],
         ],
     ];
   }