Monday, May 11, 2015

PHP Elastic Driver

Welcome back.

Today we will be looking at how to link Elastic Search into your PHP project. Lucky for us Elastic already has an well put together composer project. This project with all its documentation can be obtained here.

So for the SQL boys that have not played with No-SQL databases before I will go through some of the basic queries.

First lets start with the simple SELECT with a LIKE or a MATCH AGAINST in mysql that is.

In Elastic I have a index with the following fields

Index: TestDB
Type: TestRecords
Mappings: id,name,description,datecreated

In Mysql terms
Database: TestDB
Table: TestRecords
Columns: id,name,description,datecreated

Now that is the very basic breakdown if you want to go into this further you can start looking at how the mappings are analyzed for better performance.

Now the PHP part as we all know how to do the MySql SELECT.

Start a connection to Elastic
$elastic = Elasticsearch\ClientBuilder::create()->setHosts(['yourip:9200'])->build();

Set the search params
$params['index'] = 'TestDB';
$params['index'] = 'TestRecords';
$params['body']['query']['match']['description'] = 'phrase search';

This will return a set of results that  will be list in terms of score. The data record that matches the best will have the highest score. This is very fast and allows you to order the data so your client sees the most relevant items first.

I will say that the documentation is pretty go but if you have any questions with regard to ranges or aggregations just drop a comment.

No comments:

Post a Comment