PHP code example of shufo / laravel-opensearch
1. Go to this page and download the library: Download shufo/laravel-opensearch library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shufo / laravel-opensearch example snippets
// search
>>> OpenSearch::search([
'index' => 'example',
'body' => [
'query' => [
'match' => [
'id' => '123'
]
]
]
])
=> [
"took" => 1,
"timed_out" => false,
"_shards" => [
"total" => 1,
"successful" => 1,
"skipped" => 0,
"failed" => 0,
],
"hits" => [
"total" => [
"value" => 1,
"relation" => "eq",
],
"max_score" => 0.6931471,
"hits" => [
[
"_index" => "example",
"_type" => "_doc",
"_id" => "1",
"_score" => 0.6931471,
"_source" => [
"id" => "123",
"body" => "test",
],
],
],
],
]
// create index
OpenSearch::indices()->create([
'index' => 'example',
'body' => [
'mappings' => [
'properties' => [
'id' => [
'type' => 'long',
],
'text' => [
'type' => 'text',
]
]
]
],
])
// add document to index
OpenSearch::index([
"id" => "123",
"body" => [
"id" => "123",
"text" => "foo",
],
"index" => "example",
]);
// delete index
OpenSearch::indices()->delete(['index' => 'example']);
// SQL (currently it's available only select operation)
>>> OpenSearch::sql()->query(["query" => "select * from example", "fetch_size" => 1])
=> [
"schema" => [
[
"name" => "body",
"type" => "text",
],
[
"name" => "id",
"type" => "keyword",
],
],
"cursor" => "d:eyJhIjp7fSwicyI6IkZHbHVZMngxWkdWZlkyOXVkR1Y0ZEY5MWRXbGtEWEYxWlhKNVFXNWtSbVYwWTJnQkZrTmZVamR0VEc1ZlUwSmxOM2h4U2w5bFRWQjRaMUVBQUFBQUFBQUFxaFpqUjFGckxVRm9YMUl6Vnpkc2NXaHlabkk1VFZGbiIsImMiOlt7Im5hbWUiOiJib2R5IiwidHlwZSI6InRleHQifSx7Im5hbWUiOiJpZCIsInR5cGUiOiJrZXl3b3JkIn1dLCJmIjoxLCJpIjoiZXhhbXBsZSIsImwiOjF9",
"total" => 2,
"datarows" => [
[
"test",
"123",
],
],
"size" => 1,
"status" => 200,
]
use OpenSearch\ClientBuilder;
$data = [
'body' => [
'testField' => 'abc'
],
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
];
$client = ClientBuilder::create()->build();
$return = $client->index($data);
sh
php artisan vendor:publish --provider="Shufo\LaravelOpenSearch\ServiceProvider"
bash
$ vim config/opensearch.php