PHP Класс ElasticSource

Автор: David Kullmann
Наследование: extends DataSource
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$Http object Holds HttpSocket Object
$columns array Elasticsearch column definition
$fullDebug string Prevents error from being thrown in CakeTestFixture
$useNestedTransactions string Prevents error from being thrown in CakeTestFixture

Защищенные свойства (Protected)

Свойство Тип Описание
$_document array The document for this transaction (for saving more than one model)
$_filterOps string Valid operators from DboSource to support
$_id string If we are in a transaction this is the id for the model we are indexing
$_listSources boolean Don't list sources by default, there's still some problems here
$_parent string Holding data for _parent.
$_queryLog string Query log
$_schema array Holds the mappings
$_transactionStarted boolean Track if we are in a transaction or not - allows saving multiple models to a document
$_type string If we are in a transaction this is the 'type' for the model indexing

Открытые методы

Метод Описание
__call ( string $method, array $arguments ) : mixed Call HttpSocket methods
__construct ( array $config = [] ) Constructor, call the parent and setup Http
addToDocument ( Model $Model, array $document = [] ) : boolean Store a document to save later, merge it with existing model information for that document
afterParseConditions ( Model $Model, array $filters = [] ) : array Perform this check after parseConditions has completed, since parseConditions is recursive we have to perform this check in a separate method (or use a static variable or whatever, but, I think this is cleaner)
allDocuments ( ) : array Get all documents for this transaction
begin ( ) : true Begin a transaction - allows saving of multiple models pseudo-automically through _bulk API, but it's main purpose is to allow saving associated model data and multiple models (saveMany/saveAssociated)
bulkIndex ( string $type = null, array $documents = [] ) : boolean Bulk index a group of models
calculate ( Model $Model, $func, $params = null )
checkMapping ( Model $Model ) : boolean Check to see if a mapping exists
commit ( ) : boolean Save all models we have been storing
create ( Model $Model, array $fields = [], array $values = [] ) : mixed If this operation is atomic then store the model data to be created on "commit" later, this allows for saving multiple models in one document through saveAll, saveMany, and saveAssociated.
createIndex ( $index, $alias = false, $settings = [] )
createSchema ( $Schema ) : void Create schema, used in testing
currentDocument ( ) : array Get the current document
currentModel ( Model &$Model ) : Model Get/set the current model - called when requests are starting
delete ( Model $Model, array $conditions = null ) : boolean Delete a record
describe ( $model ) : array Describe a model based on it's mapping
dropIndex ( $index )
dropMapping ( Model $Model ) : boolean Delete a mapping
dropSchema ( string $Schema ) : void Drop a schema
execute ( $method = null, $type = null, $api = null, $data = [] )
filterResults ( array $results = [] ) : array Filter results from a call, parsing out the records
fullTableName ( Model | string $model, boolean $quote = true, boolean $schema = true ) : string Gets full table name including prefix
generateQuery ( Model $Model, array $queryData = [] ) : array Generate an ElasticSearch query from Cake's ORM $queryData
geo ( $key, $operator, $value )
geoBoundary ( $field, array $boundaries ) : array Creates filter for a geo boundary box
geoDistance ( $field, array $options ) : array Creates filter for a geo distance search
geoDistanceRange ( $field, $minDistance ) : array Creates filter for a geo distance range search
getColumnType ( Model $model, string $column ) : string Returns the column type of a column in the model.
getLog ( ) : void Get the query log - support for DebugKit Toolbar
getMapping ( ) : array Get the entire index mapping
getType ( Model $Model ) : void Get the useType if its set, otherwise use the table name
inTransaction ( ) : void Check to see if we are in a transacion
index ( $type, $id = null, $document = [] )
insertMulti ( $type, array $fields, array $values, $refresh = true ) : void Used by CakeTestFixture to insert many records.
isConnected ( ) : boolean Returns true to signal that the source is connected to the datastore
listSources ( $data = NULL ) : array List the types available for each index
logQuery ( string $method, string $uri, string $body, string $results = [] ) : void Log a new query if debug is on
mapModel ( Model $Model, array $description = [], $alias = true ) : boolean Map a model based on it's description from MySQL (or your own)
missing ( $key, $value )
name ( string $alias ) : string Make Elastic play nice with Model::escapeField();
parseConditions ( Model $Model, array $conditions = [] ) : array Parse the 'conditions' key of a query from CakePHP's ORM
parseMapping ( array $mapping = [], $sourcesOnly = false ) : array Parse an entire index mapping to create the schema for this datasource
parseOrder ( Model $Model, $query = [] )
parseQueryType ( $query )
query ( $method, $params, Model $Model )
range ( $key, $operator, $value )
read ( Model $Model, array $queryData = [], $recursive = NULL ) : mixed Query ElasticSearch to retrieve records
reindex ( ElasticScroll $scroll, array $options = [] ) : void Copies all documents from an index type to another
reset ( ) : void Reset our transaction state, document, id, and type
resetSequence ( string $table, string $column ) : boolean Reset a sequence based on the MAX() value of $column. Useful for resetting sequences after using insertMulti().
rollback ( ) : void Cancel this save operation/transaction
scan ( Model $model, integer $pageSize = 50, string $cursorTtl = '2m' ) : ElasticScroll Returns an iterator with all results for an index type, the results order is maintained by Elastic search for $cursorTtl time in minutes. This function is commonly used for re-indexing a type when it's internal pproperty definition change.
term ( $key, $operator, $value )
truncate ( string $type, $refresh = true ) : void Used by CakeTestFixture to truncate a type
typeToAlias ( $type ) : string Convert a pluralized table name (test_models) to an alias (TestModel)
update ( Model $Model, $fields = [], $values = [], $conditions = NULL )

Защищенные методы

Метод Описание
_addLog ( string $data = [] ) : void Added an item to the query log
_convertAttributes ( string $attr, string $val ) : string Convert MySQL or CakePHP ORM field attributes into ElasticSearch compatible attributes
_findKey ( Model $Model, array $document = [] ) : mixed Find the key for this document
_normalizeBoolFilter ( array $filter ) : array Restructures bool filters so they have the propper formatting for Elastic Search
_parseDescription ( array $description = [], $Model = null ) : array Recursive method to map a SQL-like model description into a ElasticSearch one
_parseKey ( Model $Model, string $key, mixed $value ) : array Used to parse a key for ElasticSearch filters
_parseResponse ( CakeResponse $response ) : mixed Parse the response from ElasticSearch, throwing errors if necessary
_setupTransaction ( Model $Model, array $document = [] ) : void If we are in a new transaction, set _type and _id, if we are in an existing transaction then start a new document and set _id
_throwError ( string $info ) : void Throw the right error
_uri ( array $config ) : array Get the URI for a request

Описание методов

__call() публичный Метод

Call HttpSocket methods
См. также: ElasticSource::_parseResponse()
См. также: ElasticSource::_filterResults()
Автор: David Kullmann
public __call ( string $method, array $arguments ) : mixed
$method string
$arguments array
Результат mixed Results from HttpSocket calls parsed by _parseResponse and _filterResults

__construct() публичный Метод

Constructor, call the parent and setup Http
Автор: David Kullmann
public __construct ( array $config = [] )
$config array

_addLog() защищенный Метод

Added an item to the query log
Автор: David Kullmann
protected _addLog ( string $data = [] ) : void
$data string
Результат void

_convertAttributes() защищенный Метод

Convert MySQL or CakePHP ORM field attributes into ElasticSearch compatible attributes
Автор: David Kullmann
protected _convertAttributes ( string $attr, string $val ) : string
$attr string
$val string
Результат string ES compatible attribute

_findKey() защищенный Метод

Find the key for this document
Автор: David Kullmann
protected _findKey ( Model $Model, array $document = [] ) : mixed
$Model Model
$document array
Результат mixed Boolean false if no key is present, otherwise the key (string/int)

_normalizeBoolFilter() защищенный Метод

Restructures bool filters so they have the propper formatting for Elastic Search
protected _normalizeBoolFilter ( array $filter ) : array
$filter array single bool filter created by _parseKey
Результат array

_parseDescription() защищенный Метод

Recursive method to map a SQL-like model description into a ElasticSearch one
Автор: David Kullmann
protected _parseDescription ( array $description = [], $Model = null ) : array
$description array
Результат array Array representing ES Mapping

_parseKey() защищенный Метод

Used to parse a key for ElasticSearch filters
Автор: David Kullmann
protected _parseKey ( Model $Model, string $key, mixed $value ) : array
$Model Model
$key string
$value mixed
Результат array ElasticSearch compatible filter

_parseResponse() защищенный Метод

Parse the response from ElasticSearch, throwing errors if necessary
Автор: David Kullmann
protected _parseResponse ( CakeResponse $response ) : mixed
$response CakeResponse
Результат mixed boolean true or false, or body of request as array

_setupTransaction() защищенный Метод

If we are in a new transaction, set _type and _id, if we are in an existing transaction then start a new document and set _id
Автор: David Kullmann
protected _setupTransaction ( Model $Model, array $document = [] ) : void
$Model Model
$document array
Результат void

_throwError() защищенный Метод

Throw the right error
Автор: David Kullmann
protected _throwError ( string $info ) : void
$info string
Результат void

_uri() защищенный Метод

Get the URI for a request
Автор: David Kullmann
protected _uri ( array $config ) : array
$config array HttpSocket $config style array
Результат array Array compatible with HttpSocket $uri for get/post/put/delete

addToDocument() публичный Метод

Allows saveAssociated to work and allows storing multiple models in one document
Автор: David Kullmann
public addToDocument ( Model $Model, array $document = [] ) : boolean
$Model Model
$document array
Результат boolean true

afterParseConditions() публичный Метод

Perform this check after parseConditions has completed, since parseConditions is recursive we have to perform this check in a separate method (or use a static variable or whatever, but, I think this is cleaner)
Автор: David Kullmann
public afterParseConditions ( Model $Model, array $filters = [] ) : array
$Model Model
$filters array
Результат array

allDocuments() публичный Метод

Get all documents for this transaction
Автор: David Kullmann
public allDocuments ( ) : array
Результат array Array of documents keyed by their primary key

begin() публичный Метод

Begin a transaction - allows saving of multiple models pseudo-automically through _bulk API, but it's main purpose is to allow saving associated model data and multiple models (saveMany/saveAssociated)
Автор: David Kullmann
public begin ( ) : true
Результат true

bulkIndex() публичный Метод

Bulk index a group of models
Автор: David Kullmann
public bulkIndex ( string $type = null, array $documents = [] ) : boolean
$type string
$documents array
Результат boolean true on success

calculate() публичный Метод

public calculate ( Model $Model, $func, $params = null )
$Model Model

checkMapping() публичный Метод

Check to see if a mapping exists
Автор: David Kullmann
public checkMapping ( Model $Model ) : boolean
$Model Model
Результат boolean true if it exists

commit() публичный Метод

Save all models we have been storing
Автор: David Kullmann
public commit ( ) : boolean
Результат boolean true on success

create() публичный Метод

The first model to be saved in the transaction determines the type, and if more than one set of models (saveMany) are being saved then they will be stored and saved in bulk Otherwise simply index the model data given based on it's type and Id
Автор: David Kullmann
public create ( Model $Model, array $fields = [], array $values = [] ) : mixed
$Model Model
$fields array
$values array
Результат mixed boolean or array of model data

createIndex() публичный Метод

public createIndex ( $index, $alias = false, $settings = [] )

createSchema() публичный Метод

Create schema, used in testing
Автор: David Kullmann
public createSchema ( $Schema ) : void
Результат void

currentDocument() публичный Метод

Get the current document
Автор: David Kullmann
public currentDocument ( ) : array
Результат array Document data

currentModel() публичный Метод

Get/set the current model - called when requests are starting
Автор: David Kullmann
public currentModel ( Model &$Model ) : Model
$Model Model
Результат Model the current model

delete() публичный Метод

Delete a record
Автор: David Kullmann
public delete ( Model $Model, array $conditions = null ) : boolean
$Model Model
$conditions array
Результат boolean True on success

describe() публичный Метод

Describe a model based on it's mapping
Автор: David Kullmann
public describe ( $model ) : array
Результат array Schema

dropIndex() публичный Метод

public dropIndex ( $index )

dropMapping() публичный Метод

Delete a mapping
Автор: David Kullmann
public dropMapping ( Model $Model ) : boolean
$Model Model
Результат boolean true on success

dropSchema() публичный Метод

Drop a schema
Автор: David Kullmann
public dropSchema ( string $Schema ) : void
$Schema string
Результат void

execute() публичный Метод

public execute ( $method = null, $type = null, $api = null, $data = [] )

filterResults() публичный Метод

Filter results from a call, parsing out the records
Автор: David Kullmann
public filterResults ( array $results = [] ) : array
$results array
Результат array Array of results

fullTableName() публичный Метод

Gets full table name including prefix
public fullTableName ( Model | string $model, boolean $quote = true, boolean $schema = true ) : string
$model Model | string Either a Model object or a string table name.
$quote boolean Whether you want the table name quoted.
$schema boolean Whether you want the schema name included.
Результат string Full quoted table name

generateQuery() публичный Метод

Generate an ElasticSearch query from Cake's ORM $queryData
Автор: David Kullmann
public generateQuery ( Model $Model, array $queryData = [] ) : array
$Model Model
$queryData array
Результат array Array that can be converted to JSON for ElasticSearch

geo() публичный Метод

public geo ( $key, $operator, $value )

geoBoundary() публичный Метод

Creates filter for a geo boundary box
public geoBoundary ( $field, array $boundaries ) : array
$boundaries array
Результат array

geoDistance() публичный Метод

Creates filter for a geo distance search
public geoDistance ( $field, array $options ) : array
$options array
Результат array

geoDistanceRange() публичный Метод

Creates filter for a geo distance range search
public geoDistanceRange ( $field, $minDistance ) : array
Результат array

getColumnType() публичный Метод

according to its mapping.
public getColumnType ( Model $model, string $column ) : string
$model Model instance to be inspected
$column string The name of the model column
Результат string Column type

getLog() публичный Метод

Get the query log - support for DebugKit Toolbar
Автор: David Kullmann
public getLog ( ) : void
Результат void

getMapping() публичный Метод

Get the entire index mapping
Автор: David Kullmann
public getMapping ( ) : array
Результат array ES Mapping

getType() публичный Метод

Get the useType if its set, otherwise use the table name
Автор: David Kullmann
public getType ( Model $Model ) : void
$Model Model
Результат void

inTransaction() публичный Метод

Check to see if we are in a transacion
Автор: David Kullmann
public inTransaction ( ) : void
Результат void

index() публичный Метод

public index ( $type, $id = null, $document = [] )

insertMulti() публичный Метод

You should use index instead
Автор: David Kullmann
public insertMulti ( $type, array $fields, array $values, $refresh = true ) : void
$fields array
$values array
Результат void

isConnected() публичный Метод

Returns true to signal that the source is connected to the datastore
public isConnected ( ) : boolean
Результат boolean True

listSources() публичный Метод

List the types available for each index
Автор: David Kullmann
public listSources ( $data = NULL ) : array
Результат array Array of types - similar to tables in a DB

logQuery() публичный Метод

Log a new query if debug is on
Автор: David Kullmann
public logQuery ( string $method, string $uri, string $body, string $results = [] ) : void
$method string
$uri string
$body string
$results string
Результат void

mapModel() публичный Метод

Map a model based on it's description from MySQL (or your own)
Автор: David Kullmann
public mapModel ( Model $Model, array $description = [], $alias = true ) : boolean
$Model Model
$description array
Результат boolean true on success

missing() публичный Метод

public missing ( $key, $value )

name() публичный Метод

Make Elastic play nice with Model::escapeField();
public name ( string $alias ) : string
$alias string The model alias
Результат string

parseConditions() публичный Метод

Parse the 'conditions' key of a query from CakePHP's ORM
Автор: David Kullmann
public parseConditions ( Model $Model, array $conditions = [] ) : array
$Model Model
$conditions array
Результат array Array of filters for ElasticSearch

parseMapping() публичный Метод

Parse an entire index mapping to create the schema for this datasource
Автор: David Kullmann
public parseMapping ( array $mapping = [], $sourcesOnly = false ) : array
$mapping array
Результат array CakePHP schema

parseOrder() публичный Метод

public parseOrder ( Model $Model, $query = [] )
$Model Model

parseQueryType() публичный Метод

public parseQueryType ( $query )

query() публичный Метод

public query ( $method, $params, Model $Model )
$Model Model

range() публичный Метод

public range ( $key, $operator, $value )

read() публичный Метод

Query ElasticSearch to retrieve records
Автор: David Kullmann
public read ( Model $Model, array $queryData = [], $recursive = NULL ) : mixed
$Model Model
$queryData array
Результат mixed boolean false on failure or array of records on success

reindex() публичный Метод

Copies all documents from an index type to another
public reindex ( ElasticScroll $scroll, array $options = [] ) : void
$scroll ElasticScroll Iterator with results to be copied, can be from another server.
$options array Array with following options: - toIndex: Target index name. If none provided then default configured index for this datasource will be used leave blanck wehn oyu want to copy data from one server to the other using same index name. - toType: Type name to use for storing new documents in target index (required) - transform: A callback function that will get each document before it is stored in target index. useful for adding, removing or changing any data before it is saved.
Результат void

reset() публичный Метод

Reset our transaction state, document, id, and type
Автор: David Kullmann
public reset ( ) : void
Результат void

resetSequence() публичный Метод

This method should be implemented by datasources that require sequences to be used.
public resetSequence ( string $table, string $column ) : boolean
$table string The name of the table to update.
$column string The column to use when reseting the sequence value.
Результат boolean success.

rollback() публичный Метод

Cancel this save operation/transaction
Автор: David Kullmann
public rollback ( ) : void
Результат void

scan() публичный Метод

Returns an iterator with all results for an index type, the results order is maintained by Elastic search for $cursorTtl time in minutes. This function is commonly used for re-indexing a type when it's internal pproperty definition change.
public scan ( Model $model, integer $pageSize = 50, string $cursorTtl = '2m' ) : ElasticScroll
$model Model The model intance to introspect to get the scrollable results
$pageSize integer as results are iterated, how large should the page be when asking Elastic Searhc for results bigger numbers make fewer requests to ElasticSearch but consume more memory and take longer to process in php
$cursorTtl string Time to keep cursor results cached in Elastic Seaach (Example: '10m' for ten minutes)
Результат ElasticScroll result iterator with all entries for an index type.

term() публичный Метод

public term ( $key, $operator, $value )

truncate() публичный Метод

Used by CakeTestFixture to truncate a type
Автор: David Kullmann
public truncate ( string $type, $refresh = true ) : void
$type string
Результат void

typeToAlias() публичный Метод

Convert a pluralized table name (test_models) to an alias (TestModel)
Автор: David Kullmann
public typeToAlias ( $type ) : string
Результат string

update() публичный Метод

public update ( Model $Model, $fields = [], $values = [], $conditions = NULL )
$Model Model

Описание свойств

$Http публичное свойство

Holds HttpSocket Object
public object $Http
Результат object

$_document защищенное свойство

The document for this transaction (for saving more than one model)
protected array $_document
Результат array

$_filterOps защищенное свойство

Valid operators from DboSource to support
protected string $_filterOps
Результат string

$_id защищенное свойство

If we are in a transaction this is the id for the model we are indexing
protected string $_id
Результат string

$_listSources защищенное свойство

Don't list sources by default, there's still some problems here
protected bool $_listSources
Результат boolean

$_parent защищенное свойство

Holding data for _parent.
protected string $_parent
Результат string

$_queryLog защищенное свойство

Query log
protected string $_queryLog
Результат string

$_schema защищенное свойство

Holds the mappings
protected array $_schema
Результат array

$_transactionStarted защищенное свойство

Track if we are in a transaction or not - allows saving multiple models to a document
protected bool $_transactionStarted
Результат boolean

$_type защищенное свойство

If we are in a transaction this is the 'type' for the model indexing
protected string $_type
Результат string

$columns публичное свойство

Elasticsearch column definition
public array $columns
Результат array

$fullDebug публичное свойство

Prevents error from being thrown in CakeTestFixture
public string $fullDebug
Результат string

$useNestedTransactions публичное свойство

Prevents error from being thrown in CakeTestFixture
public string $useNestedTransactions
Результат string