PHP code example of legalthings / elasticsearch-php

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

    

legalthings / elasticsearch-php example snippets


use LegalThings/Elasticsearch;

$config = ['hosts' => 'elasticsearch.example.com:9200'];

$es = new Elasticsearch($config);

$info = $es->client->info();

[
    'hosts' => ['localhost:9200'],
    'retries' => 2
]

use LegalThings/Elasticsearch;

$es = new Elasticsearch($config);

$index = 'books';
$type = 'ancient';
$text = 'My book';
$fields = ['name'];
$filter = [
    'id' => '0001',
    'updated(max)' => '2017-01-01T00:00:00',
    'year(min)' => 1973,
    'published' => false
];
$sort = ['^year'];
$limit = 15;
$offset = 0;

$result = $es->search($index, $type, $text, $fields, $filter, $sort, $limit, $offset);

use LegalThings/Elasticsearch;

$es = new Elasticsearch($config);
        
$index = 'books';
$type = 'ancient';
$id = '0001';
$data = [
    'id' => '0001',
    'updated' => '2017-01-01T00:00:00',
    'year' => 1980,
    'published' => false,
    'name' => 'My book two'
];

$result = $es->index($index, $type, $id, $data);

use LegalThings/Elasticsearch;

$es = new Elasticsearch($config);
        
$index = 'books';
$type = 'ancient';
$id = '0001';
$data = [
    'name' => 'My book three'
];

$result = $es->update($index, $type, $id, $data);

use LegalThings/Elasticsearch;

$es = new Elasticsearch($config);
        
$index = 'books';
$type = 'ancient';
$id = '0001';

$result = $es->get($index, $type, $id);

use LegalThings/Elasticsearch;

$es = new Elasticsearch($config);
        
$index = 'books';
$type = 'ancient';
$id = '0001';

$result = $es->delete($index, $type, $id);

use LegalThings/ElasticFilter;

$filter = [
    'id' => '0001',
    'authors' => ['John', 'Jane'],
    'deleted' => null,
    'start_date(min)' => '2017-01-01T00:00:00',
    'end_date(max)' => '2018-01-01T00:00:00',
    'age(min)' => 25,
    'tags(not)' => ['foo', 'bar'],
    'published(not)' => null,
    'colors(any)' => ['blue', 'green'],
    'colors(none)' => ['red'],
    'category(all)' => ['A', 'B', 'C']
];

$ef = new ElasticFilter($filter);
$query = $ef->transform();

$this->assertEquals([
    'bool' => [
        'must' => [
            [ 'term' => [ 'id' => '0001' ] ],
            [ 'terms' => [ 'authors' => ['John', 'Jane'] ] ],
            [ 'missing' => [ 'field' => 'deleted' ] ],
            [ 'range' => [ 'start_date' => [ 'gte' => '2017-01-01T00:00:00' ] ] ],
            [ 'range' => [ 'end_date' => [ 'lte' => '2018-01-01T00:00:00' ] ] ],
            [ 'range' => [ 'age' => [ 'gte' => 25 ] ] ],
            [ 'terms' => [ 'colors' => [ 'blue', 'green' ] ] ],
            [ 'term' => [ 'category' => [ 'A', 'B', 'C' ] ] ]
        ],
        'must_not' => [
            [ 'terms' => [ 'tags' => [ 'foo', 'bar' ] ] ],
            [ 'missing' => [ 'field' => 'published' ] ],
            [ 'term' => [ 'colors' => [ 'red' ] ] ]
        ]
    ]
], $query);

use LegalThings/ElasticFilter;

$ef = new ElasticFilter();

$query = $ef->addDefaultFilter('id', '0001')
    ->addDefaultFilter('authors', ['John', 'Jane'])
    ->addDefaultFilter('deleted', null)
    ->addMinFilter('start_date', '2017-01-01T00:00:00')
    ->addMaxFilter('end_date', '2018-01-01T00:00:00')
    ->addMinFilter('age', 25)
    ->addNotFilter('tags', ['foo', 'bar'])
    ->addNotFilter('published', null)
    ->addAnyFilter('colors', ['blue', 'green'])
    ->addNoneFilter('colors', ['red'])
    ->addAllFilter('category', ['A', 'B', 'C'])
    ->transform();

$this->assertEquals([...], $query); // same output as first example

use LegalThings/ElasticMap;

$es = new Elasticsearch($config);

$result = $es->client->indices()->create([
    'index' => 'my_index',
    'body' => ElasticMap::getFullTextSearchMapping()
]);