aqarmap/elastic-repository

elastic search active repository package

0.1.3 2017-10-31 12:29 UTC

README

💥 Goals

  • provide active repository pattern over your elasticsearch indices
  • provide query builder for your elasticsearch repository

✨ Usage

  • Extend ElasticRepository class as a repository for your type or index.
class schoolsRepository extends ElasticRepository 
{
    // method contains some bussiness logic 
    public function returnQueryWherename()
    {
        $this->where('name', 'EGSchool', 0.5)->getResultQuery();
    }
}

📋 Documentation

ElasticRepository

setIndex(), setType()

The setIndex() and setType() methods for setting up your index name and type name into Repository:

setTransformer($transformer)

The setTransformer($transformer) to add transformer for your result transformer must implement TransformerContract

get()

method get() to get result from your final query after building it using query builder:

getResultWithScore()

The getResultWithScore($scoreFunction) method to get results after adding a score function: takes Query\FunctionScore $functionScore as a parameter to be applied to your results

getResultQuery()

the getResultQuery() return Query object

getResultQueryWithScore($scoreFunction)

takes Query\FunctionScore $functionScore as a parameter to apply scoring to your query

QueryBuilder

Every ElasticRepository class have it's own query builder which have a lot of operations

where(), whereNot()

The where() and whereNot() methods adding must and must not to the main filter:

//attribute paramter then the expected value and optional value for the field boost
$queryBuilder->where($attribute, $value = null, $boost = 1.0);

whereIn(), whereNotIn()

The whereIn() and whereNotIn() methods adding Range to the main filter:

//attribute paramter then a optional value for the fields from and to
$queryBuilder->whereIn($attribute, $from = '', $to = '');

setSort()

The setSort() method adds main sort criteria for the query: sorting with _score by default when adding score function

// pass field name to sort by 
$queryBuilder->setSort('fieldName');

setOrder()

The setOrder() method to specify sort direction:

$queryBuilder->setSort('fieldName')->setOrder('desc');

exist('fieldName')

$queryBuilder->exist('fieldName');

match($attribute, $keyword)

$queryBuilder->match('fieldName', $keywordToMatch);

todo

  • queries caching support
  • cover more code with tests