Giter Site home page Giter Site logo

kirankarki / elasticsearcher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from madewithlove/elasticsearcher

0.0 0.0 0.0 170 KB

Agnostic lightweight package on top of the Elasticsearch PHP client. Its main goal is to allow for easier structuring of queries and indices in your application.

License: MIT License

Shell 3.13% PHP 96.87%

elasticsearcher's Introduction

elasticsearcher

Circle CI

This agnostic package is a lightweight wrapper on top of the Elasticsearch PHP client. Its main goal is to allow for easier structuring of queries and indices in your application. It does not want to hide or replace functionality of the Elasticsearch PHP client.

Installation

Installation of the latest version is easy via composer:

composer require madewithlove/elasticsearcher

Versions

Elasticsearch Elasticsearcher
>= 5.0 >= 0.5
>= 2.0 >= 0.4
>= 1.0, < 2.0 0.3

Features

Query class

Structure queries inside a class for clearer oversight in your application.

class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');

		// Full notation
		$body = [
			'query' => [
				'bool' => [
					'filter' => [
						'term' => ['year' => 2014]
					]
				]
			]
		];
		$this->setBody($body);

		// Short (dotted) notation
		$this->set('query.bool.filter.term.year', 2014);

		$this->paginate(2, 20);
		$this->sortBy('name', 'asc');
	}
}

// Usage
$query = new MoviesFrom2014Query($this->getElasticSearcher());
$query->run();

Query with custom/re-usable fragments

Move re-occuring or complex fragments of your query or index to a separate class.

class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');

		$this->set('query.bool.filter', [new YearFilter(2014)]);
	}
}

Query with custom result parsing

Perform actions on the response from Elasticsearch before the Query returns the results. It can be used for converting the Elasticsearch documents into models/entities from your ORM. Re-use it in multiple queries.

class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');
		$this->parseResultsWith(new MoviesResultParser());

		$body = array(...);

		$this->setBody($body);
	}
}

// Usage
$query = new MoviesFrom2014Query($this->getElasticSearcher());
$result = $query->run();
foreach ($result->getResults() as $movie) {
	var_dump($movie->title, $movie->id, $movie->year);
}

Indices management

$searcher->indicesManager()->exists('listings');
$searcher->indicesManager()->existsType('suggestions', 'movies');
$searcher->indicesManager()->create('suggestions');
$searcher->indicesManager()->update('suggestions');
$searcher->indicesManager()->delete('suggestions');
$searcher->indicesManager()->deleteType('suggestions', 'movies');

Documents management

$manager->index('suggestions', 'movies', $data);
$manager->bulkIndex('suggestions', 'movies', [$data, $data, $data]);
$manager->update('suggestions', 'movies', 123, ['name' => 'Fight Club 2014']);
$manager->updateOrIndex('suggestions', 'movies', 123, ['name' => 'Fight Club 2014']);
$manager->delete('suggestions', 'movies', 123);
$manager->exists('suggestions', 'movies', 123);
$manager->get('suggestions', 'movies', 123);

Cluster Health

Sometimes when you're re-indexing your ES data, you might have some issues between your index recreation and indexing your data. That's because ES can take a bit longer to recreate your indexes, causing your reindex task to fail - we are talking about microseconds here. You can find some references here and here.

In order to avoid this, we built this helper in the ElasticSearcher class to check the cluster health. You can use like this:

<?php
/** @var \ElasticSearcher\ElasticSearcher $searcher */
while (!$searcher->isHealthy()) { sleep(1); }

Place this in your reindex command between your index creation and the data indexing.

Attention: If you're running a single ES node, you'll need to configure your elasticsearch number_of_replicas setting to 0. However, we highly recommend you to use at least 2 nodes.

Access to Elasticsearch client

The package does not and will not try to implement everything from the Elasticsearch client. Access to the client is always possible.

$client = $searcher->getClient();

Usage

use ElasticSearcher\Environment;
use ElasticSearcher\ElasticSearcher;

$env = new Environment(
  ['hosts' => ['localhost:9200']]
);
$searcher = new ElasticSearcher($env);

More usage in the examples and documentation.

Documentation

elasticsearcher's People

Contributors

anahkiasen avatar bjrnblm avatar bramdevries avatar damiankloip avatar dieterve avatar grahamcampbell avatar hannesvdvreken avatar jamesb797 avatar jdrieghe avatar nwidart avatar tonysm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.