3xw / cakephp-elastic-search
此包最新版本(4.0.0)没有可用的许可证信息。
CakePHP页面插件
4.0.0
2021-09-03 12:53 UTC
Requires
- php: >=7.2
- cakephp/cakephp: ^4.0
- cakephp/elastic-search: 3.2.1
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-26 18:59:50 UTC
README
此插件允许您处理ES
安装
您可以使用 composer 将此插件安装到您的CakePHP应用程序中。
安装composer包的推荐方法是
composer require 3xw/elastic-search
在config/boostrap.php中加载它
Plugin::load('Trois/ElasticSearch');
设置
在app.php中添加至少一个连接
'Datasources' => [
'elastic' => [
'className' => 'Cake\ElasticSearch\Datasource\Connection',
'driver' => 'Cake\ElasticSearch\Datasource\Connection',
'host' => 'localhost',
'port' => 6379,
],
...
]
映射
Shell会要求您输入映射模板。按照官方文档 原生Elasticsearch映射格式 创建您自己的索引映射,或者使用提示中建议的默认映射:vendor/3xw/cakephp-elastic-search/templates/mapping.json
{
"settings": {
"analysis": {
"filter": {
"french_elision": {
"type":"elision",
"articles_case": true,
"articles": [
"l", "m", "t", "qu", "n", "s",
"j", "d", "c", "jusqu", "quoiqu",
"lorsqu", "puisqu","t"
]
},
"french_stop": {
"type": "stop",
"stopwords": "_french_"
},
"french_keywords": {
"type": "keyword_marker",
"keywords": [
"Prométerre",
"Agrivit","Ecoprest","Estimapro","Fidasol","Fiprom",
"Fondation rurale de prévoyance","FRP",
"Mandaterre",
"Office de crédit agricole","OCA",
"Proconseil","Proterroir","SAD","SRPJ","Sofia SA"
]
},
"french_stemmer": {
"type": "stemmer",
"language": "light_french"
}
},
"analyzer": {
"french": {
"tokenizer": "standard",
"filter": [
"french_elision",
"lowercase",
"french_stop",
"french_keywords",
"french_stemmer"
]
}
}
}
},
"mappings": {
"items": {
"properties": {
"foreign_key": { "type": "integer"},
"model": { "type": "keyword"},
"title": {"type": "completion", "analyzer":"french", "max_input_length": 255},
"content": {"type": "text"}
}
}
}
}
行为
根据您的意愿设置行为
$this->addBehavior(\Trois\ElasticSearch\ORM\Behavior\SyncWithESBehavior::class,[
'index' => 'App\Model\Index\ItemsIndex',
'primaryKey' => 'foreign_key', // string or callable
'translate' => false, // property name if yes ex: locale
'staticMatching' => [
'model' => 'Posts'
], // or [keyN => valueN/callableN]
'mapping' => [ // properties => 1. Array: entity field(s) || properties => 2. String: static value or callable
'title' => new \Trois\ElasticSearch\ORM\CompletionConstructor(['title'],[
'contexts' => [
'model' => 'Posts'
],
]),
'content' => ['header','content']
],
'deleteDocument' => true,
'separator' => ' - '
]);
Shell
创建索引
bin/cake elastic create [indexName] [mappingFile.json]
------
Execute create index w/ mapping file Task.
Usage:
cake trois/elastic_search.elastic create [-c] [-h] [-q] [-r 1] [-s 5] [-v]
Options:
--connection, -c The Elestic Search connection to use
--help, -h Display this help.
--quiet, -q Enable quiet output.
--replicas, -r The index number of replicas. Default is 1
(default: 1)
--shards, -s The index number of shards. Default is 5
(default: 5)
--verbose, -v Enable verbose output.
获取索引信息
bin/cake elastic info [indexName]
------
Execute info on index Task.
Usage:
cake trois/elastic_search.elastic info [-c] [-h] [-q] [-v]
Options:
--connection, -c The Elestic Search connection to use
--help, -h Display this help.
--quiet, -q Enable quiet output.
--verbose, -v Enable verbose output.
删除索引
bin/cake elastic delete [indexName]
------
Execute delete index Task.
Usage:
cake trois/elastic_search.elastic delete [-c] [-h] [-q] [-v]
Options:
--connection, -c The Elestic Search connection to use
--help, -h Display this help.
--quiet, -q Enable quiet output.
--verbose, -v Enable verbose output.
导入整个表
bin/cake elastic import [indexName] [tableName]
------
Execute import on index with table.
Usage:
cake trois/elastic_search.elastic import [-c] [-h] [-q] [-v]
Options:
--connection, -c The Elestic Search connection to use
--help, -h Display this help.
--quiet, -q Enable quiet output.
--verbose, -v Enable verbose output.