2.elasticsearch-php

1.在國內下載網址下載elasticsearch
2.解壓運行

bin\elasticsearch.bat

3.打開瀏覽器輸入

http://localhost:9200/

在這裏插入圖片描述
4.創建composer.json

{
    "require": {
        "elasticsearch/elasticsearch": "~6.0"
    }
}

5.在composer.json文件的目錄下運行

composer update elasticsearch/elasticsearch

6.創建index.php文件(名字隨便起)

<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;

// 實例化一個客戶端
$client = ClientBuilder::create()->build();

7.索引一個文檔(創建一條數據)

// 索引一個文檔(創建一條數據)
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);

打印結果

Array
(
    [_index] => my_index		//創建的index名
    [_type] => my_type		//創建的tupe名
    [_id] => my_id		//創建的id名
    [_version] => 1
    [result] => created
    [_shards] => Array
        (
            [total] => 2
            [successful] => 1
            [failed] => 0
        )

    [_seq_no] => 0
    [_primary_term] => 1
)

8.獲取一個文檔(查詢一條記錄)

// 獲取一個文檔(查詢一條記錄)
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];
$response = $client->get($params);
print_r($response);

打印結果

Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 1
    [_seq_no] => 0
    [_primary_term] => 1		//匹配的條數
    [found] => 1
    [_source] => Array
        (
            [testField] => abc		//剛纔設置的內容
        )

)

9.搜索一個文檔(搜索)

// 搜索一個文檔(搜索)
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);

打印結果

Array
(
    [took] => 0
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 1
            [successful] => 1
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => Array
                (
                    [value] => 1
                    [relation] => eq
                )

            [max_score] => 0.2876821
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => my_index
                            [_type] => my_type
                            [_id] => my_id
                            [_score] => 0.2876821
                            [_source] => Array
                                (
                                    [testField] => abc
                                )

                        )

                )

        )

)

10.刪除一個文檔(刪除指定id)

// 刪除一個文檔(刪除指定id)
$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] => 2
    [result] => deleted
    [_shards] => Array
        (
            [total] => 2
            [successful] => 1
            [failed] => 0
        )

    [_seq_no] => 1
    [_primary_term] => 1
)

11.刪除一個索引

// 刪除一個索引
// 創建第一個文檔時會自動創建一個索引,同事將setting裏面的參數設置爲默認值
$deleteParams = [
    'index' => 'my_index'
];
$response = $client->indices()->delete($deleteParams);
print_r($response);

打印結果


Array
(
    [acknowledged] => 1
)

12.創建一個索引同時設置setting

// 創建一個索引 同事設置setting
$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
    [shards_acknowledged] => 1
    [index] => my_index
)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章