<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>

                ## 拉取組件 愛你項目的根目錄require組件,執行: composer require elasticsearch/elasticsearch ~2.0 可以看到目前這個`PHP Client for Elasticsearch`組件的版本是`v5.3.0`,為什么我們選擇`v2.0`的版本呢? 因為該項目中使用的Elasticsearch版本是`v2.3.2`,版本號大于`v1.0`并且小于`v5.0`,按照官方文檔,選擇`v2.0`相匹配Elasticsearch版本。 附packagist.org地址: https://packagist.org/packages/elasticsearch/elasticsearch ,有興趣可以直接看英文文檔。 ## 創建客戶端實例 <?php use Elasticsearch\ClientBuilder; require 'vendor/autoload.php'; $client = ClientBuilder::create()->build(); ## 索引一行 代碼: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', 'body' => [ 'martist_2017_field_1' => '123', 'martist_2017_field_2' => '456', 'martist_2017_field_3' => '789', ] ]; $response = $client->index($params); echo "<pre>"; print_r($response); 結果: Array ( [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_version] => 1 [_shards] => Array ( [total] => 2 [successful] => 1 [failed] => 0 ) [created] => 1 ) es_head顯示: ![](https://box.kancloud.cn/75b887cf642ffc8fac86f327d15ab424_2574x608.png) ## 檢索一行數據 代碼: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', ]; $response = $client->get($params); echo "<pre>"; print_r($response); 結果: Array ( [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_version] => 1 [found] => 1 [_source] => Array ( [martist_2017_field_1] => 123 [martist_2017_field_2] => 456 [martist_2017_field_3] => 789 ) ) ## 檢索(只要返回數組內鍵名為‘_source’的數據) 代碼: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', ]; $source = $client->getSource($params); echo "<pre>"; print_r($source); 結果: Array ( [martist_2017_field_1] => 123 [martist_2017_field_2] => 456 [martist_2017_field_3] => 789 ) ## 檢索(按照字段) 代碼: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'body' => [ 'query' => [ 'match' => [ 'martist_2017_field_1' => '123' ] ] ] ]; $response = $client->search($params); echo "<pre>"; print_r($response); 結果: Array ( [took] => 4 [timed_out] => [_shards] => Array ( [total] => 5 [successful] => 5 [failed] => 0 ) [hits] => Array ( [total] => 1 [max_score] => 0.30685282 [hits] => Array ( [0] => Array ( [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_score] => 0.30685282 [_source] => Array ( [martist_2017_field_1] => 123 [martist_2017_field_2] => 456 [martist_2017_field_3] => 789 ) ) ) ) ) ## 刪除(索引) 代碼: $deleteParams = [ 'index' => 'martist_2017_index' ]; $response = $client->indices()->delete($deleteParams); print_r($response); 結果: Array ( [acknowledged] => 1 ) ## 刪除(行) 代碼: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', ]; $response = $client->delete($params); print_r($response); 結果: Array ( [found] => 1 [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_version] => 2 [_shards] => Array ( [total] => 2 [successful]=> 1 [failed] => 0 ) ) 此時觀察es-head ![](https://box.kancloud.cn/b5b74e9206abed75c46a9f912d1cab6e_1830x594.png) ## 創建索引 代碼: $params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ] ]; $response = $client->indices()->create($params); print_r($response); ## 結果: Array ( [acknowledged] => 1 ) ## 使用Mock進行單元測試 代碼: use GuzzleHttp\Ring\Client\MockHandler; use Elasticsearch\ClientBuilder; // The connection class requires 'body' to be a file stream handle // Depending on what kind of request you do, you may need to set more values here $handler = new MockHandler([ 'status' => 200, 'transfer_stats' => [ 'total_time' => 100 ], 'body' => fopen('somefile.json') ]); $builder = ClientBuilder::create(); $builder->setHosts(['somehost']); $builder->setHandler($handler); $client = $builder->build(); // Do a request and you'll get back the 'body' response above
                  <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>

                              哎呀哎呀视频在线观看