elasticsearch con php en windows

requisitos

1) tener instalado elasticsearch

instalar

descargar el zip desde

https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html

extraer here

ir a la carpeta bin y ejecutar el archivo de lotes: elasticsearch.bat

para verificar que esta arriba el servicio, abrir en el browser

http://localhost:9200/

deberia mostrar algo asi:

// 20190310223923
// http://localhost:9200/

{
  "name": "6QTn43S",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "JELK0mCSRfarxHyCvTDGqQ",
  "version": {
    "number": "6.6.1",
    "build_flavor": "default",
    "build_type": "zip",
    "build_hash": "1fd8f69",
    "build_date": "2019-02-13T17:10:04.160291Z",
    "build_snapshot": false,
    "lucene_version": "7.6.0",
    "minimum_wire_compatibility_version": "5.6.0",
    "minimum_index_compatibility_version": "5.0.0"
  },
  "tagline": "You Know, for Search"
}

nota: en el momento de escribir esta entrada la version de ES era 6.6.1

2) crear un proyecto web
en xampp/htdocs crear un directorio p.e llamado elastic_php
editar el archivo xampp/apache/conf/httpd-vhosts.conf

agregar

<VirtualHost *:80>
  ServerName elastic_php.local
  DocumentRoot "C:/xampp729/htdocs/elastic_php"
</VirtualHost>

luego editar

C:\Windows\System32\drivers\etc\hosts

y agregar
127.0.0.1      elastic_php.local

asi podemos podemos llamar a la aplicacion con la sgt url

http://elastic_php.local



2)instalar el cliente php de elasticsearch

( https://github.com/elastic/elasticsearch-php )



crear en la raiz del proyecto creado el archivo

composer.json

con el sgt contenido

{
 "require": {
 "elasticsearch/elasticsearch": "~6.0"
 }
}

desde la consola ejecutar

composer install


3)crear un documento

crear en la raiz del proyecto el archivo index.php

<?php
require 'vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];

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

la salida seria algo como:

Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 1
    [result] => created
    [_shards] => Array
        (
            [total] => 2
            [successful] => 1
            [failed] => 0
        )

    [_seq_no] => 0
    [_primary_term] => 1
)


los capos obligatorios son index, type y body.
si no se envia id, este se generara

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => ['nombre' => 'juan']
];

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

esta seria la salida:

Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => BlmuamkB863YvXXYJ9xk
    [_version] => 1
    [result] => created
    [_shards] => Array
        (
            [total] => 2
            [successful] => 1
            [failed] => 0
        )

    [_seq_no] => 2
    [_primary_term] => 1
)

4) obtener un documento

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

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


obtendriamos algo asi:

Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 2
    [_seq_no] => 1
    [_primary_term] => 1
    [found] => 1
    [_source] => Array
        (
            [testField] => abc
        )

)

5)
obtener solo los datos del documento (sin la metadata)

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

obtendriamos algo asi:

Array
(
    [testField] => abc
)

6) Buscar un documento

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'nombre' => 'israel'
            ]
        ]
    ]
];

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

Array
(
    [took] => 5
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 0.2876821
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => my_index
                            [_type] => my_type
                            [_id] => 1
                            [_score] => 0.2876821
                            [_source] => Array
                                (
                                    [nombre] => israel
                                )

                        )

                )

        )

)

linux ubuntu mint actualizar chrome

 desde una terminal: $ sudo apt update $ sudo apt install google-chrome-stable