PHP code example of kiwfy / simple-elasticsearch-php

1. Go to this page and download the library: Download kiwfy/simple-elasticsearch-php 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/ */

    

kiwfy / simple-elasticsearch-php example snippets


use SimpleElasticsearch\SimpleElasticsearch;
$host = 'http://localhost:9200/';
$elastic = new SimpleElasticsearch($host);
$elastic->setConnectionOptions([
    'connect_timeout' => 5,
    'timeout' => 5,
]);

...
$isConnected = $elastic->isConnected();
var_dump($isConnected);

...
$indexName = 'test';
$index = $elastic->putIndex(
    $indexName
);
var_dump($index);

...
$indexName = 'test';
$mapping = [
    'properties' => [
        'name' => [
            'type' => 'keyword',
        ],
        'email' => [
            'type' => 'keyword',
        ],
        'gender' => [
            'type' => 'byte',
        ]
    ]
];
$newMapping = $elastic->putMapping(
    $indexName,
    $mapping
);
var_dump($newMapping);

...
$documentName = 'document';
$template = [
    'index_patterns' => [
        'document*'
    ],
    'mappings' => [
        '_source' => [
            'enabled' => true,
        ],
        'properties' => [
            'name' => [
                'type' => 'keyword',
            ],
            'created' => [
                'type'=> 'date',
                'format' => 'yyyy-MM-dd HH:mm:ss',
            ],
        ]
    ]
];
$newTemplate = $elastic->putTemplate(
    $documentName,
    $template
);
var_dump($newTemplate);

...
$indexName = 'test';
$getIndex = $elastic->getIndex(
    $indexName
);
var_dump($getIndex);

...
$indexName = 'test';
$getMapping = $elastic->getMapping(
    $indexName
);
var_dump($getMapping);

...
$documentName = 'document';
$getTemplate = $elastic->getTemplate(
    $documentName
);
var_dump($getTemplate);

...
$documentName = 'document';
$dataTemplate = [
    'name' => 'document1',
    'created' => date('Y-m-d H:i:s'),
];
$postDocumentTemplate = $elastic->postDocument(
    $documentName,
    $dataTemplate
);
var_dump($postDocumentTemplate);

...
$documentName = 'document';
$indexName = 'test';
$data = [
    'name' => 'user',
    'email' => '[email protected]',
    'gender' => 0,
];
$id = '01HDRQRB0VPDDB9HWHX3MGY6XG';
$postDocument = $elastic->postDocument(
    $indexName,
    $data,
    $id
);
var_dump($postDocument);

...
$indexName = 'test';
$id = '01HDRQRB0VPDDB9HWHX3MGY6XG';
$getDocument = $elastic->getDocument(
    $indexName,
    $id
);
var_dump($getDocument);

...
$indexName = 'test';
$id = '01HDRQRB0VPDDB9HWHX3MGY6XG';
$deleteDocument = $elastic->deleteDocument(
    $indexName,
    $id
);
var_dump($deleteDocument);

...
$indexName = 'test';
$dslQuery =  [
    'term' => [
        'email' => [
            'value' => '[email protected]',
            'boost' => 1,
        ],
    ],
];
$searchDocuments = $elastic->searchDocuments(
    $indexName,
    $dslQuery
);
var_dump($searchDocuments);

...
$indexName = 'test';
$listDocuments = $elastic->listDocuments(
    $indexName
);
var_dump($listDocuments);

...
$indexName = 'test';
$page = 2;
$listDocumentsPaginated = $elastic->listDocuments(
    $indexName,
    $page
);
var_dump($listDocumentsPaginated);

...
$query = "SELECT * FROM test WHERE email LIKE '%[email protected]' ORDER BY email DESC";
$sqlResponse = $elastic->sql(
    $query
);
var_dump($sqlResponse);

...
// var $sql has data returned from previous query with the cursor
$sqlCursorResponse = $elastic->sqlCursor(
    $sql['cursor']
);
var_dump($sqlCursorResponse);

...
$query = "SELECT * FROM test WHERE email LIKE '%[email protected]' ORDER BY email DESC";
$translate = $elastic->translate(
    $query
);
var_dump($translate);

...
$documentName = 'document';
$deleteTemplate = $elastic->deleteTemplate(
    $documentName
);
var_dump($deleteTemplate);

...
$indexName = 'test';
$deleteIndex = $elastic->deleteIndex(
    $indexName
);
var_dump($deleteIndex);

...
$indexName = 'test';
$dslAgregate = [
    'genders' => [
        'terms' => [
            'field' => 'gender',
        ]
    ]
];
$dslQueryAggregate =  [
    'wildcard' => [
        'email' => [
            'wildcard' => '*[email protected]',
            'boost' => 1,
        ],
    ],
];
$aggregateDocuments = $elastic->aggregateDocuments(
    $indexName,
    $dslAgregate,
    $dslQueryAggregate
);
var_dump($aggregateDocuments);
sh
php sample/elastic-sample.php