PHP中使用Elasticsearch的方法

來源:互聯網
上載者:User
這篇文章主要介紹了關於PHP中使用Elasticsearch的方法,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

PHP中使用Elasticsearch

composer require elasticsearch/elasticsearch

會自動載入合適的版本!我的php是5.6的,它會自動載入5.3的elasticsearch版本!

Using version ^5.3 for elasticsearch/elasticsearch./composer.json has been updatedLoading composer repositories with package informationUpdating dependencies (including require-dev)Package operations: 4 installs, 0 updates, 0 removals  - Installing react/promise (v2.7.0): Downloading (100%)           - Installing guzzlehttp/streams (3.0.0): Downloading (100%)           - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%)           - Installing elasticsearch/elasticsearch (v5.3.2): Downloading (100%)         Writing lock fileGenerating autoload files

簡單使用

<?phpclass MyElasticsearch{    private $es;    // 建構函式    public function __construct()    {        include('../vendor/autoload.php');        $params = array(            '127.0.0.1:9200'        );        $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build();    }    public function search() {        $params = [            'index' => 'megacorp',            'type' => 'employee',            'body' => [                'query' => [                    'constant_score' => [ //非評分模式執行                        'filter' => [ //過濾器,不會計算相關度,速度快                            'term' => [ //精確尋找,不支援多個條件                                'about' => '譚'                            ]                        ]                    ]                ]            ]        ];        $res = $this->es->search($params);        print_r($res);    }}
<?phprequire "./MyElasticsearch.php";$es = new MyElasticsearch();$es->search();

執行結果

Array(    [took] => 2    [timed_out] =>     [_shards] => Array        (            [total] => 5            [successful] => 5            [skipped] => 0            [failed] => 0        )    [hits] => Array        (            [total] => 1            [max_score] => 1            [hits] => Array                (                    [0] => Array                        (                            [_index] => megacorp                            [_type] => employee                            [_id] => 3                            [_score] => 1                            [_source] => Array                                (                                    [first_name] => 李                                    [last_name] => 四                                    [age] => 24                                    [about] => 一個PHP程式員,熱愛編程,譚康很帥,充滿激情。                                    [interests] => Array                                        (                                            [0] => 英雄聯盟                                        )                                )                        )                )        ))

下面是官方的一些範例:

初始化

require '../vendor/autoload.php';use Elasticsearch\ClientBuilder;$client = ClientBuilder::create()->build();

增加配置

$hosts = [    '127.0.01:9200',         // IP + Port];$client = ClientBuilder::create()           // Instantiate a new ClientBuilder->setHosts($hosts)      // Set the hosts->build();              // Build the client object

$hosts = [    '127.0.01:9200',         // IP + Port];$clientBuilder = ClientBuilder::create();   // Instantiate a new ClientBuilder$clientBuilder->setHosts($hosts);           // Set the hosts$client = $clientBuilder->build();          // Build the client object

插入一個文檔

// Index 一個文檔$params = [    'index' => 'my_index',    'type' => 'my_type',    'id' => 'my_id',    'body' => ['testField' => 'abc']];$response = $client->index($params);print_r($response);

擷取一個文檔

$params = [    'index' => 'my_index',    'type' => 'my_type',    'id' => 'my_id'];$response = $client->get($params);print_r($response);

查詢一個文檔

$params = [    'index' => 'my_index',    'type' => 'my_type',    'body' => [        'query' => [            'match' => [                'testField' => 'abc'            ]        ]    ]];$response = $client->search($params);print_r($response);

刪除一個文檔

$params = [    'index' => 'my_index',    'type' => 'my_type',    'id' => 'my_id'];$response = $client->delete($params);print_r($response);

結果如下

Array(    [_index] => my_index    [_type] => my_type    [_id] => my_id    [_version] => 3    [result] => deleted    [_shards] => Array        (            [total] => 2            [successful] => 1            [failed] => 0        )    [_seq_no] => 2    [_primary_term] => 1)

刪除一個索引

$deleteParams = [    'index' => 'my_index'];$response = $client->indices()->delete($deleteParams);print_r($response);

建立一個索引

$params = [    'index' => 'my_index',    'body' => [        'settings' => [            'number_of_shards' => 2,            'number_of_replicas' => 0        ]    ]];$response = $client->indices()->create($params);print_r($response);

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.