dkullmann / cakephp-elastic-search-datasource
CakePHP 数据源和工具,用于将模型连接到 Elastic Search 类型
Requires
This package is not auto-updated.
Last update: 2020-01-20 07:05:42 UTC
README
CakePHP Elastic 插件 & ElasticSearch "ElasticSource" 数据源
方便地将记录索引和访问到 ElasticSearch。
背景
无缝地从 MySQL 或其他 SQL/DBO 支持的数据源过渡到惊人的 ElasticSearch NoSQL 索引器。符合 CakePHP 的 ORM,同时也提供对分面和查询的访问。
欢迎补丁和问题。请包括单元测试。
要求
- PHP >= 5.3
- CakePHP 2.x
- ElasticSearch (http://elasticsearch.org)
- 对 ElasticSearch 有基本了解。
安装
[手动]
- 下载此内容:http://github.com/josegonzalez/cakephp-elastic-search-datasource/zipball/master
- 解压下载内容。
- 将生成的文件夹复制到 app/Plugin
- 将您刚刚复制的文件夹重命名为 Elastic
[GIT 子模块]
在您的应用目录中键入
git submodule add git://github.com/josegonzalez/cakephp-elastic-search-datasource.git Plugin/Elastic
git submodule update --init
[GIT Clone]
在您的应用目录中键入
git clone git://github.com/josegonzalez/cakephp-elastic-search-datasource.git Plugin/Elastic
启用插件
在您的 app/Config/bootstrap.php
文件中启用插件
CakePlugin::load('Elastic');
如果您已经使用 CakePlugin::loadAll();
,则无需此操作。
用法
设置 ElasticSearch
如果您还没有,请设置一个 ElasticSearch 实例以使用。
打开 Config/database.php
并添加一个名为 index
的数据源
public $index = array(
'datasource' => 'Elastic.ElasticSource',
'index' => 'people',
'port' => 9200
);
然后创建您的模型
class Contact extends AppModel {
public $useDbConfig = 'index';
}
这将把您的 Contact
模型存储在 people
索引中,类型为 contacts
。默认情况下,ElasticSearch 的 "type" 与您为模型使用的表名相同。如果您想更改 ElasticSearch 类型,请将变量 useType
添加到您的模型中
class Contact extends AppModel {
public $useDbConfig = 'index';
public $useType = 'mytype';
}
映射您的模型
Elastic 插件附带一个 shell,可以帮助您管理索引、创建映射和索引记录
Davids-MacBook-Pro:app dkullmann$ Console/cake Elastic.elastic
ElasticSearch Plugin Console Commands. Map and index data
Usage:
cake elastic.elastic [subcommand] [-h] [-v] [-q]
Subcommands:
create_index Create or alias an index
mapping Map a model to ElasticSearch
index Index a model into ElasticSearch
list_sources Display output from listSources
To see help on a subcommand use `cake elastic.elastic [subcommand] --help`
Options:
--help, -h Display this help.
--verbose, -v Enable verbose output.
--quiet, -q Enable quiet output.
首先,创建您的索引
Console/cake Elastic.elastic create_index test
情况 1:您的模型已在 'default' 数据源中
您可以使用此命令复制模式
Console/cake Elastic.elastic mapping Contact
情况 2:您的模型在响应 describe
的其他数据源中
Console/cake Elastic.elastic mapping Contact -d <datasource>
案例 3:您的模型尚未映射
您可以在模型中添加一个名为 elasticMapping 的方法来生成映射。
支持特殊的 ElasticSearch 类型,如 geopoint 和 multi_field。
class Contact extends AppModel {
public $useDbConfig = 'index';
public $_mapping = array(
'id' => array('type' => 'integer'),
'name' => array('type' => 'string'),
'number' => array('type' => 'string'),
'special_type' => array(
'type' => 'multi_field',
'fields' => array(
'not_analyzed' => array('type' => 'string', 'index' => 'not_analyzed'),
'analyzed' => array('type' => 'string', 'index' => 'analyzed')
)
),
'created' => array('type' => 'datetime'),
'modified' => array('type' => 'datetime')
);
public function elasticMapping() {
return $this->_mapping;
}
}
索引一些记录
如果您还没有 MySQL 中的数据并且正在跟随本教程,您应该创建您的 MySQL 表和数据
CREATE TABLE `contacts` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`number` varchar(255) DEFAULT NULL,
`special_type` varchar(255) DEFAULT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `contacts` (`id`, `name`, `number`, `special_type`, `created`, `modified`)
VALUES
(1, 'David', '555-888-1212', 'Multifield', '2012-07-19 20:31:29', '2012-07-19 20:31:29');
否则,直接开始索引。
要开始索引记录,您可以使用此命令
Console/cake Elastic.elastic index Contact
ElasticShell 将在模型未添加 IndexableBehavior 的情况下将其添加到模型中。要永久添加,请将其添加到模型中
public $actsAs = array('Elastic.Indexable');
默认情况下,IndexableBehavior 将将您的 "modified" 字段声明为跟踪每个记录何时更新或创建的字段,以便与 ElasticSearch 同步。
使用此命令进行测试
curl -XGET 'http://localhost:9200/people/contacts/1
输出应包括
{
"_index" : "test",
"_type" : "contacts",
"_id" : "1",
"_version" : 2,
"exists" : true, "_source" : {"Contact":{"created":"2012-07-19 20:31:29","id":"1","modified":"2012-07-19 20:31:29","name":"David","number":"555-888-1212","special_type":"Multifield"}}
}
CRUD 操作
因为 ElasticSource 符合 CakePHP ORM 的 CRUD 操作,所以操作很容易
// Create
$record = array(
'id' => 1,
'name' => 'David',
);
$this->Model->create($data);
$this->Model->save();
// Read
$this->Model->findById(1);
// Update
$this->Model->save($data);
// Delete
$this->Model->delete(1);
查询
使用此插件通过 Elastic Search 进行查询非常简单,您会发现它与您已经熟悉的查询关系型数据库的方式非常相似
简单条件
使用 Model.field 语法,后跟一个运算符
<?php
$this->Model->find('all', array(
'conditions' => array(
'Model.name' => 'text', // produces {term: {"Model.name": "text"}}
'Model.quantity >' => 10 // produces {range: "Model.quantity": {gt: 10}}, can also use <, <=, >=
)
));
基于距离的地理定位查询
以下查询将搜索位于提供的经纬度最多 10 英里范围内的所有结果
<?php
$this->Model->find('all', array(
'conditions' => array(
'Model.location' => array(
'distance' => '10mi',
'lat' => 10,
'lon' => 70
'unit' => 'mi' //Optional (also accepts km)
'distance_type' => 'arc' // Optional, defaults to plain
)
)
));
布尔查询
布尔查询是过滤结果的强大工具,基于应严格满足的条件以及只是一些提示或“最好有”的条件。Bool 查询创建方式如下
<?php
$this->Model->find('all', array(
'conditions' => array(
'bool' => array(
array('Model.tags should' => 'cakephp'),
// Multiple conditions per field
array('Model.tags must' => 'cakephp'),
array('Model.tags must' => 'elastic'),
array('Model.tags must' => 'source'),
'Model.readers must_not <' => 100,
)
)
));
查询字符串搜索
如果您需要使用单个查询字符串同时对多个字段进行搜索,则请使用 query_string
键
<?php
$this->Model->find('all', array(
'conditions' => array(
'query_string' => array(
//Search on both name and description using a boost for name
'fields' => array('Model.name^2', 'Model.description'),
'query' => 'Text to be looked up'
)
)
));
has_parent / has_child 查询
如果您正在编写 has_parent 或 has_child 查询,您需要将标准 JSON 查询中的 'type' 替换为 'model'
<?php
$this->Model->find('all', array(
'conditions' => array(
'has_child' => array(
//Maps to Elasticsearch 'type'
'model' => 'ChildModel',
'child_model_id' => $id
)
)
));
其他
使用 IndexableBehavior 提供的高级功能,如边界和多边形地理定位搜索和排序
待办事项
- 让更多人使用它,并告诉我他们想要什么/修复它
- 单元测试!
- 重新排列一些逻辑,使其以更简单/更少侵入性的方式符合 ORM
- 记录 IndexableBehavior
许可证
版权所有 (c) 2012 David Kullmann
特此授予任何获得本软件及其相关文档文件(以下简称“软件”)副本的任何人,在不受限制的情况下处理软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许将软件提供给任何软件提供者,以便他们这样做,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
本软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者都不应对任何索赔、损害或其他责任负责,无论这些责任是基于合同、侵权或其他原因,以及与软件或软件的使用或其他相关事项有关。