欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2.elasticsearch-php

程序员文章站 2022-07-14 22:30:24
...

1.在国内下载网址下载elasticsearch
2.解压运行

bin\elasticsearch.bat

3.打开浏览器输入

http://localhost:9200/

2.elasticsearch-php
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
)

相关标签: ES