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

新增

<?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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值