<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 背景 電商業務在過去的幾年中呈現出快速增長的趨勢,這導致了數據量的增加,同時也帶來了數據處理和搜索的挑戰。傳統的關系型數據庫在處理大量數據和高并發訪問時,可能會遇到性能瓶頸和查詢速度問題。因此,需要一種高性能、高可擴展性的搜索引擎來滿足電商業務的需求。 ## 簡介 Elasticsearch 是一個基于 Lucene 構建的開源、分布式、RESTful 接口的全文搜索引擎,其每個字段均可被索引,且能夠橫向擴展至數以百計的服務器存儲以及處理 TB 級的數據,其可以在極短的時間內存儲、搜索和分析大量的數據。 作為一款基于RESTful API的分布式服務,Elasticsearch可以快速地、近乎于準實時地存儲、查詢和分析超大數據集,通常被用來作為構建復雜查詢特性和需求強大應用的基礎引擎或技術。 由于其強大的搜索能力、靈活的擴展性和豐富的功能特性,Elasticsearch在數據分析和搜索領域非常受歡迎。 ## 基礎知識 ### 概念 * 節點和集群:理解Elasticsearch中的節點(Node)和集群(Cluster)的概念,以及它們如何協同工作。 * 索引(Index):索引是Elasticsearch中的核心概念,與關系型數據庫中的數據庫相似,但有其特殊性。 * 類型(Type)和文檔(Document):在Elasticsearch 7.x之后,一個索引中只允許有一個類型,到Elasticsearch 8.x之后類型已經被移除,但了解其歷史概念對理解Elasticsearch的發展有幫助。 * 分片(Shard)和副本(Replica):理解分片和副本的概念對于掌握Elasticsearch的數據分布和容錯機制至關重要。 ### 數據索引 * 文檔映射(Mapping):學習如何定義文檔的字段類型、分析器等屬性。 * 索引管理:包括創建索引、更新映射、刪除索引等操作。 ### 搜索技術 * 查詢DSL:Elasticsearch提供了一種豐富的查詢DSL(Domain Specific Language),用于執行復雜的搜索操作。 * 聚合(Aggregations):聚合提供了從數據中提取洞察力的能力,類似于SQL中的GROUP BY操作。 ### 性能優化 * 分片策略:學習如何根據數據量和查詢模式來合理分配分片。 * 緩存和刷新:理解Elasticsearch中的各種緩存機制以及如何通過合理配置提高搜索性能。 ### 集群管理 * 監控和診斷:學習如何監控Elasticsearch集群的健康狀況和性能指標。 * 擴展和故障轉移:了解如何在不中斷服務的情況下擴展集群和處理節點故障。 ## ElasticSearch與傳統搜索引擎 ElasticSearch與傳統搜索引擎的主要區別在于數據存儲和查詢方式。傳統搜索引擎通常采用基于文件系統的數據存儲,并使用自然語言處理技術進行查詢。而ElasticSearch則采用基于內存的數據存儲,并使用Lucene庫進行查詢。這使得ElasticSearch具有更高的查詢速度和可擴展性。 ## 索引和查詢的基本原理 ElasticSearch的核心原理是基于Lucene庫的索引和查詢機制。索引是將文檔存儲在磁盤上的過程,查詢是從索引中搜索文檔的過程。 ### 索引的基本原理 索引的過程包括以下步驟: 1. 文檔解析:將文檔中的字段和值解析成一個內部表示。 2. 分析:將文本字段通過分析器轉換為索引用的形式。 3. 存儲:將解析和分析后的內容存儲到磁盤上的索引文件中。 ### 查詢的基本原理 查詢的過程包括以下步驟: 1. 解析:將查詢請求解析成一個查詢對象。 2. 搜索:根據查詢對象從索引中搜索匹配的文檔。 3. 排序:將搜索出的文檔按照排序規則進行排序。 4. 高亮:將查詢關鍵詞標注為高亮顯示。 ### 具體操作步驟 1. 安裝和配置:安裝ElasticSearch并配置相關參數。 2. 創建索引:創建一個索引,用于存儲文檔。 3. 添加文檔:將文檔添加到索引中。 4. 查詢文檔:根據查詢條件搜索文檔。 5. 更新文檔:更新文檔的內容。 6. 刪除文檔:刪除文檔。 7. 查詢分析:查看查詢的分析結果。 ## 最佳實踐 ### 安裝和配置 這里的ElasticSearch搜索服務直接使用阿里云Elasticsearch。阿里云Elasticsearch是基于開源Elasticsearch構建的全托管Elasticsearch云服務,在100%兼容開源功能的同時,支持開箱即用。 ![](https://img.kancloud.cn/16/29/1629c348fc7167fd428d595a7315c8f8_805x211.png) **客戶端SDK安裝** 這里使用Elasticsearch PHP客戶端。 > 更多:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html ``` composer require elasticsearch/elasticsearch ``` ### 配置Elasticsearch 案例客戶端`ElasticSearchCLient` ``` <?php /** * @desc ElasticSearchCLient * @author Tinywan(ShaoBo Wan) */ declare(strict_types=1); namespace search; use Elasticsearch\Client; use Elasticsearch\ConnectionPool\SimpleConnectionPool; class ElasticSearchCLient { /** * @var Client|null */ protected static ?Client $instance = null; private function __construct() { echo 'I am Constructed'; } /** * @desc: 實例 * @return Client|null */ public static function getInstance(): ?Client { if (is_null(self::$instance)) { static::$instance = \Elasticsearch\ClientBuilder::create() ->setHosts([ [ 'host' => '127.0.0.1', 'port' => '9200', 'scheme' => 'http', 'user' => 'elastic', 'pass' => 'elastic' ] ]) ->setConnectionPool(SimpleConnectionPool::class) ->setRetries(10) ->build(); } return static::$instance; } } ``` ### 文檔模型 定義一個簡單的文檔模型 #### 創建一個文檔索引 ``` /** * @desc 創建一個索引 * @return Response * @author Tinywan(ShaoBo Wan) */ public function createIndex(): Response { $client = ElasticSearchCLient::getInstance(); $indexParams = [ 'index' => 'resty_product_test_index', // 索引名稱 ]; $indexResponse = $client->indices()->create($indexParams); return response_json('success',0,$indexResponse); } ``` #### 添加文檔到ES ``` /** * @desc 添加文檔到Elasticsearch * @return Response * @author Tinywan(ShaoBo Wan) */ public function addDocument(): Response { $client = ElasticSearchCLient::getInstance(); $data = [ 'id' => 20245, 'title' => '開源技術小棧20245直播間', 'content' => '開源技術小棧-騰訊云開發者社區,開源技術小棧20245直播間', ]; $indexParams = [ 'index' => 'resty_product_test_index', // 索引名稱 'id' => $data['id'], // 文檔ID 'body' => $data, 'client' => [ 'timeout' => 10, 'connect_timeout' => 10 ] ]; $indexResponse = $client->index($indexParams); return response_json('success',0,$indexResponse); } ``` 響應結果 ```json { "code": 0, "msg": "success", "data": { "_index": "resty_product_test_index", "_type": "_doc", "_id": "20245", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 4, "_primary_term": 1 } } ``` #### 搜索文檔 ``` /** * @desc 搜索文檔 * @return Response * @author Tinywan(ShaoBo Wan) */ public function searchDocument(): Response { $client = ElasticSearchCLient::getInstance(); $query = '開源技術小棧'; $indexParams = [ 'index' => 'resty_product_test_index', 'body' => [ 'query' => [ 'multi_match' => [ 'query' => $query, 'fields' => ['title', 'content'], ] ] ] ]; $indexResponse = $client->search($indexParams); return response_json('success',0,$indexResponse); } ``` 搜索結果 ``` { "took": 12, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 1.5399661, "hits": [ { "_index": "resty_product_test_index", "_type": "_doc", "_id": "2024", "_score": 1.5399661, "_source": { "id": 2024, "title": "開源技術小棧20240724直播間", "content": "開源技術小棧-騰訊云開發者社區,開源技術小棧20240724直播間" } }, { "_index": "resty_product_test_index", "_type": "_doc", "_id": "20245", "_score": 1.5399661, "_source": { "id": 20245, "title": "開源技術小棧20245直播間", "content": "開源技術小棧-騰訊云開發者社區,開源技術小棧20245直播間" } } ] } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看