Elasticsearch-PHP(三) - 文档的增删改查

版权声明:转载需附上本文地址 https://blog.csdn.net/weikaixxxxxx/article/details/86495029

新增

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'id' => '1', // 如果不提供id,es会自动生成
    'body' => ['first_name' => 'abc']
];


$response = $client->index($params);

在这里插入图片描述
多字段

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'body' => ['first_name' => 'abc', 'age' => '100']
];

在这里插入图片描述
查询
id的方式

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'id' => '1'
];


$response = $client->get($params);// /my_index2/my_type/1
var_dump($response);

在这里插入图片描述
字段的方式

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'first_name' => 'abc'
            ]
        ]
    ]
];

$results = $client->search($params);
var_dump($results);

在这里插入图片描述
使用json

$json = '{
    "query" : {
        "match" : {
            "first_name" : "abc"
        }
    }
}';

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'body' => $json
];

$results = $client->search($params);
var_dump($results);

bool查询

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    [ 'match' => [ 'first_name' => 'abc' ] ],
                    [ 'match' => [ 'age' => '100' ] ],
                ]
            ]
        ]
    ]
];

$results = $client->search($params);
var_dump($results);

这里类似mysql的and
在这里插入图片描述
更新

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
    ->setHosts($hosts)
    ->build();

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'id' => '1',
    'body' => [
        'doc' => [
            'first_name' => 'abc2',
            'age' => '123'
        ]
    ]
];

$response = $client->update($params);

删除

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
    ->setHosts($hosts)
    ->build();

$params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'id' => '1'
];

$response = $client->delete($params);

猜你喜欢

转载自blog.csdn.net/weikaixxxxxx/article/details/86495029