1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      輕松集成新版Elasticsearch7.9中文搜索到Laravel7項目

      下面由Laravel教程欄目給大家介紹如何輕松集成新版Elasticsearch7.9中文搜索到Laravel7項目,希望對需要的朋友有所幫助!

      阜康網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。成都創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)

      只需五步驟:

      1.啟動 集成ik中文分詞插件的Elasticsearch7.9 Docker鏡像
      2.Laravel7 配置 Scout
      3.配置 Model模型
      4.導(dǎo)入數(shù)據(jù)
      5.搜索

      演示地址

      www.ar414.com/search?query=php%E5%...

      搜索范圍

      文章內(nèi)容標(biāo)題標(biāo)簽

      結(jié)果權(quán)重

      出現(xiàn)關(guān)鍵詞數(shù)量出現(xiàn)關(guān)鍵詞次數(shù)

      搜索頁面

      高亮顯示分詞顯示結(jié)果分頁前言

      主要是博客剛好想做個搜索,順便就整理成文章

      Laravel + Elasticsearch 很多前輩都寫過教程和案例,但是隨著Elasticsearch和laravel的版本升級 以前的文章很多都不適用新版本的,建議大家使用任何開源項目時應(yīng)該過一遍文檔以當(dāng)前使用的版本文檔為主,教程為輔

      Elasticsearch 7.9Laravel 7elasticsearch-analysis-ik v7.9參考ik 中文分詞插件elasticsearch 官方文檔使用集成ik中文分詞插件的Elasticsearch拉取docker
      $ docker pull ar414/elasticsearch-7.9-ik-plugin
      創(chuàng)建日志和數(shù)據(jù)存儲目錄

      本地映射到docker容器內(nèi),防止docker重啟數(shù)據(jù)丟失

      $ mkdir -p /data/elasticsearch/data
      $ mkdir -p /data/elasticsearch/log
      $ chmod -R 777 /data/elasticsearch/data
      $ chmod -R 777 /data/elasticsearch/log
      運行
      docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v /data/elasticsearch/data:/var/lib/elasticsearch -v /data/elasticsearch/log:/var/log/elasticsearch ar414/elasticsearch-7.9-ik-plugin
      驗證
      $ curl http://localhost:9200{
        "name" : "01ac21393985",  "cluster_name" : "docker-cluster",  "cluster_uuid" : "h8L336qcRb2i1aydOv04Og",  "version" : {
          "number" : "7.9.0",    "build_flavor" : "default",    "build_type" : "docker",    "build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",    "build_date" : "2020-08-11T21:36:48.204330Z",    "build_snapshot" : false,    "lucene_version" : "8.6.0",    "minimum_wire_compatibility_version" : "6.8.0",    "minimum_index_compatibility_version" : "6.0.0-beta1"
        },  "tagline" : "You Know, for Search"}
      測試中文分詞
      curl -X POST "http://localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
      {
        "analyzer": "ik_max_word",
        "text":     "laravel天下無敵"
      }
      '{
        "tokens" : [
          {
            "token" : "laravel",      "start_offset" : 0,      "end_offset" : 7,      "type" : "ENGLISH",      "position" : 0    },    {
            "token" : "天下無敵",      "start_offset" : 7,      "end_offset" : 11,      "type" : "CN_WORD",      "position" : 1    },    {
            "token" : "天下",      "start_offset" : 7,      "end_offset" : 9,      "type" : "CN_WORD",      "position" : 2    },    {
            "token" : "無敵",      "start_offset" : 9,      "end_offset" : 11,      "type" : "CN_WORD",      "position" : 3    }
        ]}
      Laravel 項目中使用 Elasticsearch


      Elasticsearch官方有提供 SDK,在 Laravel 項目中可以更加優(yōu)雅快速的接入 Elasticsearch,Laravel 本身有提供 Scout全文搜索 的解決方案,我們只需將默認(rèn)的 Algolia 驅(qū)動 替換成ElasticSearch驅(qū)動

      安裝laravel/scoutmatchish/laravel-scout-elasticsearch
      $ composer require laravel/scout
      $ composer require matchish/laravel-scout-elasticsearch
      配置

      生成 Scout 配置文件(config/scout.php)

      $ php artisan vendor:publish --provider="Laravel\\Scout\\ScoutServiceProvider"Copied File [\\vendor\\laravel\\scout\\config\\scout.php] To [\\config\\scout.php]Publishing complete.

      指定 Scout 驅(qū)動

      第一種:在.env文件中指定(建議)
      SCOUT_DRIVER=Matchish\\ScoutElasticSearch\\Engines\\ElasticSearchEngine
      第二種:在config/scout.php直接修改默認(rèn)驅(qū)動
      'driver' => env('SCOUT_DRIVER', 'algolia')改為'driver' => env('SCOUT_DRIVER', 'Matchish\\ScoutElasticSearch\\Engines\\ElasticSearchEngine')

      指定Elasticsearch服務(wù)IP端口

      如果使用docker部署則使用docker0的IP,Linux通過ifconfig查看

      .env中配置

      ELASTICSEARCH_HOST=172.17.0.1:9200

      注冊服務(wù)
      config/app.php

      'providers' => [
       // Other Service Providers
       \\Matchish\\ScoutElasticSearch\\ElasticSearchServiceProvider::class],

      清除配置緩存

      $ php artisan config:clear

      至此 laravel 已經(jīng)接入 Elasticsearch

      實際業(yè)務(wù)中使用需求

      通過博客右上角的搜索框可以搜索到與關(guān)鍵詞相關(guān)的文章,從以下幾點匹配

      文章內(nèi)容文章標(biāo)題文章標(biāo)簽

      涉及到2張 Mysql表 以及字段

      articletitletagsarticle_contentcontent為文章配置 Elasticsearch 索引

      創(chuàng)建索引配置文件(config/elasticsearch.php)

      $ touch config/elasticsearch.php

      elasticsearch.php 配置字段映射

       [
           'mappings' => [
               'blog-articles' => [
                   "properties"=>  [
                       "content"=>  [
                           "type"=>  "text",
                           "analyzer"=>  "ik_max_word",
                           "search_analyzer"=>  "ik_smart"
                       ],
                       "tags"=>  [
                           "type"=>  "text",
                           "analyzer"=>  "ik_max_word",
                           "search_analyzer"=>  "ik_smart"
                       ],
                       "title"=>  [
                           "type"=>  "text",
                           "analyzer"=>  "ik_max_word",
                           "search_analyzer"=>  "ik_smart"
                       ]
                   ]
               ]
           ]
       ],];
      analyzer:字段文本的分詞器search_analyzer:搜索詞的分詞器根據(jù)具體業(yè)務(wù)場景選擇(顆粒小占用資源多,一般場景analyzer使用ik_max_word,search_analyzer使用ik_smart):ik_max_word:ik中文分詞插件提供,對文本進(jìn)行數(shù)量分詞
      laravel天下無敵-> laravel天下無敵,天下,無敵ik_smart: ik中文分詞插件提供,對文本進(jìn)行最小數(shù)量分詞
      laravel天下無敵-> laravel天下無敵配置文章模型

      建議先看一遍 Laravel Scout 使用文檔

      引入Laravel Scout

      namespace App\\Models\\Blog;
      
       use Laravel\\Scout\\Searchable;
      
       class Article extends BlogBaseModel
       {
           use Searchable;
       }

      指定索引(剛剛配置文件中的elasticsearch.indices.mappings.blog-articles)

      /**
        * 指定索引
        * @return string
        */
       public function searchableAs()
       {
           return 'blog-articles';
       }

      設(shè)置導(dǎo)入索引的數(shù)據(jù)字段

      /**
        * 設(shè)置導(dǎo)入索引的數(shù)據(jù)字段
        * @return array
        */
       public function toSearchableArray()
       {
           return [
               'content' => ArticleContent::query()
                   ->where('article_id',$this->id)
                   ->value('content'),
               'tags'    => implode(',',$this->tags),
               'title'   => $this->title
           ];
       }

      指定 搜索索引中存儲的唯一ID

      /**
        * 指定 搜索索引中存儲的唯一ID
        * @return mixed
        */
       public function getScoutKey()
       {
           return $this->id;
       }
      
       /**
        * 指定 搜索索引中存儲的唯一ID的鍵名
        * @return string
        */
       public function getScoutKeyName()
       {
           return 'id';
       }
      數(shù)據(jù)導(dǎo)入

      其實是將數(shù)據(jù)表中的數(shù)據(jù)通過Elasticsearch導(dǎo)入到Lucene
      Elasticsearch 是 Lucene 的封裝,提供了 REST API 的操作接口

      一鍵自動導(dǎo)入: php artisan scout:import導(dǎo)入指定模型: php artisan scout:import ${model}
      $ php artisan scout:import "App\\Models\\Blog\\Article"Importing [App\\Models\\Blog\\Article]Switching to the new index
      5/5 [????????????????????????????] 100%[OK] All [App\\Models\\Blog\\Article] records have been imported.

      導(dǎo)入失敗,常見原因:

      Unresolvable dependency resolving [Parameter #0 [ integer $retries ]] in class Elasticsearch\\Transport解決: 修改配置后,沒有清除配置緩存invalid_index_name_exception解決: searchableAs配置錯誤,為索引創(chuàng)建別名后,指定別名檢查索引是否正確
      $ curl -XGET http://localhost:9200/blog-articles/_mapping?pretty{
        "blog-articles_1598362919" : {
          "mappings" : {
            "properties" : {
              "__class_name" : {
                "type" : "text",          "fields" : {
                  "keyword" : {
                    "type" : "keyword",              "ignore_above" : 256            }
                }
              },        "content" : {
                "type" : "text",          "analyzer" : "ik_max_word",          "search_analyzer" : "ik_smart"
              },        "tags" : {
                "type" : "text",          "analyzer" : "ik_max_word",          "search_analyzer" : "ik_smart"
              },        "title" : {
                "type" : "text",          "analyzer" : "ik_max_word",          "search_analyzer" : "ik_smart"
              }
            }
          }
        }}
      測試

      創(chuàng)建一個測試命令行

      $ php artisan make:command ElasticTest

      代碼

      getPreciseTimestamp(3);
              $articles = Article::search($this->argument('query'))->get()->toArray();
              $userTime = Carbon::now()->getPreciseTimestamp(3) - $startTime;
              echo "耗時(毫秒):{$userTime} \\n";
      
              //content在另外一張表中,方便觀察測試 這里輸出
              if(!empty($articles)) {
                  foreach($articles as &$article) {
                      $article = ArticleContent::query()->where('article_id',$article['id'])->value('content');
                  }
              }
      
              var_dump($articles);
      
          }}
      測試
      $ php artisan elasticsearch 周杰倫

      復(fù)雜查詢
      例如:自定義高亮顯示
      //ONGR\\ElasticsearchDSL\\Highlight\\Highlight ArticleModel::search($query,function($client,$body) {
               $higlight = new Highlight();
               $higlight->addField('content',['type' => 'plain']);
               $higlight->addField('title');
               $higlight->addField('tags');
               $body->addHighlight($higlight);
               $body->setSource(['title','tags']);
               return $client->search(['index' => (new ArticleModel())->searchableAs(), 'body' => $body->toArray()]);
           })->raw();

      復(fù)雜自定義查詢回調(diào)中的$client和$body,可根據(jù)這兩個包進(jìn)行靈活操作

      $client 官方 elasticsearch/elasticsearch package(https://packagist.org/packages/elasticsearch/elasticsearch)

      $body ongr/elasticsearch-dsl package(https://packagist.org/packages/ongr/elasticsearch-dsl)


      本文標(biāo)題:輕松集成新版Elasticsearch7.9中文搜索到Laravel7項目
      URL網(wǎng)址:http://www.ef60e0e.cn/article/choehg.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        兖州市| 淮阳县| 承德市| 通江县| 康马县| 曲麻莱县| 甘南县| 株洲市| 班戈县| 仙居县| 惠东县| 临安市| 微山县| 浮山县| 腾冲县| 都昌县| 德令哈市| 界首市| 吉隆县| 永靖县| 天峻县| 巴中市| 靖西县| 五寨县| 清河县| 威宁| 尼玛县| 嘉黎县| 开封县| 朝阳区| 汉川市| 盐亭县| 鹰潭市| 永德县| 胶南市| 商城县| 宁河县| 卫辉市| 临朐县| 潜江市| 陆丰市|