Elasticsearch PHP examples
This project is a collection of examples on how to program Elasticsearch in PHP.
All the examples use elastic/elasticsearch-php, the official PHP client from Elastic.
Install the examples
In order to run the examples you need to install the dependencies using composer. After installing composer in your machine and run the following command:
composer install
This will create a vendor
folder with all the libraries needed to execute the examples.
How to run Elasticsearch
You need to have an Elasticsearch instance to execute the examples. You can run a single node
instance of Elasticsearch running at localhost:9200
, using the following command:
composer run-script es-run
This command will execute Elasticsearch 7.15.1 on localhost:9200
using Docker.
If you want to stop the Elasticsearch instance you can use the following command:
composer run-script es-stop
Data set
All the examples are based on the the all_stocks_5yr.csv file containing 5 years of stock prices of 500 Fortune companies, starting from February 2013.
The first two lines of this file contain the following data:
date,open,high,low,close,volume,name
2013-02-08,15.07,15.12,14.63,14.75,8407500,AAL
The information reported are the date (2013-02-08), the open value (15.07), the high value (15.12), the low value (14.63), the close value (14.75), the volume of stock exchanges (8,407,500) and the name of the stock (AAL = American Airlines Group).
Index all the data set
Almost all the examples reported in src/
uses an Elasticsearch index called stocks
.
To insert all the stock prices reported in all_stocks_5yr.csv
you can use the bulk example.
List of examples
Async index
This is the asynchronous version of the Index example. The elasticsearch-php
library offers a Future mode
for performing any endpoints using an async call.
To execute an asynchronous call you need to add the following client
value in the $params
input array:
'client' => [
'future' => 'lazy'
]
This returns a future, rather than the actual response. A future represents a future computation and acts like a placeholder. You can pass a future around your code like a regular object. When you need the result values, you can resolve the future. The src/async_index.php script stores 3 stock documents in Elasticsearch using an asynchronous approach.
Bulk
The src/bulk.php script stores 619,041
stock values in the stocks
index.
The script collects 5,000
document at time and send it using the Bulk API.
It performs 124 HTTP requests instead of 619,041, needed using the Index API.
We tested this script using an Intel Core i9 CPU with PHP 7.4, it required 8 seconds
and 166 MB RAM for the execution.
Custom mapping
The src/custom_mapping.php script configure a custom mapping
for the stocks
index. It changes the type of the name
field in keyword
.
This update can be useful to perform aggregation of the stock prices.
Delete index
The src/delete_index.php script deletes the stocks
index.
Get HTTP request and response
The src/get_http_request_and_response.php script
is an example of how to get the HTTP request and response using elasticsearch-php
.
The example execute an info()
API and returns the information about the HTTP request and
response using the function getLastConnection()
from the Transport.
Get mapping
The src/get_mapping.php script returns the mapping of the stocks
index. You can change the mapping using the src/custom_mapping.php
script example.
Index
This is the most basic API to store a single (JSON) document in Elasticsearch.
The src/index.php script stores a stock document in Elasticsearch using
the stocks
index.
Info
The src/info.php script returns the information about the Elasticsearch instance, like the version number, the cluster name, etc.
Logging
The src/logging.php script is an example of how to enable the logging
feature of elasticsearch-php
library. You can use any PSR-3
logger library. In the example we used monolog.
The log will be created using log/elasticsearch-php.log
file.
Schema on read
The src/schema_on_read.php script uses the schema on read feature available since Elasticsearch 7.11.
The schema on read can be used to create runtime fields that are not stored in the index, they are created only in the HTTP response.
Runtime fields let you define and evaluate fields at query time, which opens a wide range of new use cases. If you need to adapt to a changing log format or fix an index mapping, use runtime fields to change the schema on the fly without reindexing your data. Or if you are indexing new data and don’t have intimate knowledge of what it contains, you can use runtime fields to discover this data and define your schema without impacting others.
In the example we created an average
field with the following painless
script:
emit((double)(doc['high'].value + doc['low'].value)/2)
Search aggregation
The src/search_aggregation.php script is used to aggregate the
stock prices using the stock name
field.
The example retrievs retrieving maximum 1000 values ordering the results by name.
Search filter
The src/search_filter.php script search for all the AAL
stock
prices using a filter search term.
Search fuzzy
The src/search_fuzzy.php script executes a Fuzzy query. A fuzzy search returns the results that contain terms similar to the search term, as measured by a Levenshtein edit distance.
In the example we used the name AAL
as query that will returns also AAP
values.
Search highlight
The src/search_highlight.php script provide a query search using the highlighting feature of Elasticsearch to put evidence on the results.
In the example we search for the name AAL
and the results will contain the <em>AAL</em>
tag that you can use to configure a CSS style.
Search iterator
Search match_all
Search match page
Search match
Update mapping
References
You can watch a recorded presentation about "Programming Elasticsearch with PHP" provided by Enrico Zimuel at PHP Conference Japan 2022. Here the slides of the presentation.
License
All the code is released under the Apache 2.0 license.
Copyright (c) Elasticsearch B.V (https://www.elastic.co)