php [elasticsearch php] elasticsearch php api

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [elasticsearch php] elasticsearch php api相关的知识,希望对你有一定的参考价值。

<?php
/*
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html
*/
use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

/**
	document
*/
//To index a document, need to specify: index, type, id and a document body
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];

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

//get a document
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

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

//search for a document
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

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

//delete a document
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

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

/**
	Index
*/
//delete a index
$deleteParams = [
    'index' => 'my_index'
];
$response = $client->indices()->delete($deleteParams);

//create an index
$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 2,
            'number_of_replicas' => 0
        ]
    ]
];

$response = $client->indices()->create($params);

以上是关于php [elasticsearch php] elasticsearch php api的主要内容,如果未能解决你的问题,请参考以下文章

php [elasticsearch php] elasticsearch php api

Elasticsearch-PHP 概述

Elasticsearch-PHP 安装

PHP操作Elasticsearch7.6

php elasticsearch连接配置 --- 2022-04-02

PHP 中的 Elasticsearch 映射类型