PHP code example of kiyonori / elasticsearch-fluent-query-builder
1. Go to this page and download the library: Download kiyonori/elasticsearch-fluent-query-builder 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/ */
kiyonori / elasticsearch-fluent-query-builder example snippets
use Kiyonori\ElasticsearchFluentQueryBuilder\Builders\Query;
use Kiyonori\ElasticsearchFluentQueryBuilder\Builders\MustQuery;
use Kiyonori\ElasticsearchFluentQueryBuilder\Builders\ShouldQuery;
$query = app(Query::class);
$result = $query
->bool(function (MustQuery $must) {
$must
->bool(function (ShouldQuery $should) {
$should
->match('title', 'elasticsearch')
->match('content', 'search engine');
}, minimumShouldMatch: 1)
->term('status', 'published')
->range('created_at', gte: '2024-01-01', lte: '2024-12-31');
})
->toArray();
$query = app(Query::class);
$result = $query
->bool(function (MustQuery $must) {
$must
->bool(function (ShouldQuery $should) {
$should
->match('field_1', 'value1')
->match('field_2', 'value 2');
}, minimumShouldMatch: 1)
->bool(function (ShouldQuery $should) {
$should
->match('field_3', 'value 333')
->match('field_4', 'value四')
->match('field_5', 'value5');
}, minimumShouldMatch: 2);
})
->toArray();
$must
->term('field', 'value')
->match('field', 'text')
->range('field', gte: 10, lte: 100)
->bool(function (ShouldQuery $should) { /* ... */ });
$should
->term('field', 'value')
->match('field', 'text')
->range('field', gte: 10, lte: 100)
->bool(function (MustQuery $must) { /* ... */ });
$filter
->term('field', 'value')
->match('field', 'text')
->range('field', gte: 10, lte: 100);
$mustNot
->term('field', 'value')
->match('field', 'text')
->range('field', gte: 10, lte: 100);
use Kiyonori\ElasticsearchFluentQueryBuilder\ApplyMapping;
// JSONファイルからマッピングを適用(エイリアス付きでゼロダウンタイム)
$newIndexName = app(ApplyMapping::class)->execute(
jsonFilePath: 'path/to/mapping.json'
);
use Kiyonori\ElasticsearchFluentQueryBuilder\StoreDocument;
$response = app(StoreDocument::class)->execute(
indexName: 'my_index',
documentId: 'doc_123',
body: [
'title' => 'Sample Document',
'content' => 'This is a sample document',
'created_at' => '2024-01-01T00:00:00Z'
]
);
use Kiyonori\ElasticsearchFluentQueryBuilder\GetDocument;
$document = app(GetDocument::class)->execute(
indexName: 'my_index',
documentId: 'doc_123'
);
use Kiyonori\ElasticsearchFluentQueryBuilder\DeleteIndex;
$deletedIndices = app(DeleteIndex::class)->execute(
indexName: 'my_index',
suppressNotFoundException: true
);
use Kiyonori\ElasticsearchFluentQueryBuilder\BulkStoreDocuments;
$response = app(BulkStoreDocuments::class)->execute(
indexName: 'my_index',
documents: [
['id' => 'doc_1', 'body' => ['title' => 'Document 1']],
['id' => 'doc_2', 'body' => ['title' => 'Document 2']],
]
);
return [
'hosts' => [
'http://elasticsearch:9200',
],
'ssl_verification' => false,
'user_name' => null,
'password' => null,
];
return [
'hosts' => [
'http://elasticsearch:9200',
],
'ssl_verification' => false,
'user_name' => null,
'password' => null,
];