Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
goods
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
goods
Commits
edf34efd
Commit
edf34efd
authored
May 27, 2021
by
chenyuelong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add:elastic
parent
b61fdca2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
212 additions
and
0 deletions
+212
-0
ElasticService.php
application/services/demo/ElasticService.php
+172
-0
elastic.php
public/elastic.php
+40
-0
No files found.
application/services/demo/ElasticService.php
0 → 100644
View file @
edf34efd
<?php
namespace
App\Services\demo
;
use
Api\PhpUtils\Elastic\ElasticUtil
;
class
ElasticService
{
public
function
createIndex
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$data
=
[
'mappings'
=>
[
'properties'
=>
[
'test_field'
=>
[
'type'
=>
'text'
,
"analyzer"
=>
"ik_max_word"
,
"search_analyzer"
=>
"ik_smart"
],
]
]
];
$res
=
$elasticClient
->
createIndex
(
$index
,
$data
);
var_export
(
$res
);
}
public
function
deleteIndex
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$res
=
$elasticClient
->
deleteIndex
(
$index
);
var_export
(
$res
);
}
public
function
getSettings
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$res
=
$elasticClient
->
getSettings
([
$index
]);
var_export
(
$res
);
}
public
function
putSettings
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$data
=
[
'settings'
=>
[
'number_of_replicas'
=>
0
,
'refresh_interval'
=>
-
1
]
];
$res
=
$elasticClient
->
putSettings
(
$index
,
$data
);
var_export
(
$res
);
}
public
function
getMapping
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$res
=
$elasticClient
->
getMapping
(
$index
);
var_export
(
$res
);
}
public
function
putMapping
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$data
=
[
'properties'
=>
[
'second_field'
=>
[
'type'
=>
'text'
,
"analyzer"
=>
"ik_max_word"
,
"search_analyzer"
=>
"ik_smart"
],
]
];
$res
=
$elasticClient
->
putMapping
(
$index
,
$data
);
var_export
(
$res
);
}
public
function
indexDoc
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$data
=
[
'test_field'
=>
'美国留给伊拉克的是个烂摊子吗'
];
$res
=
$elasticClient
->
indexDoc
(
$index
,
$data
,
'test_id'
);
var_export
(
$res
);
}
public
function
getDoc
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$res
=
$elasticClient
->
getDoc
(
$index
,
'test_id'
);
var_export
(
$res
);
}
public
function
updateDoc
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$data
=
[
'doc'
=>
[
'new_field'
=>
'abc'
]
];
$res
=
$elasticClient
->
updateDoc
(
$index
,
'test_id'
,
$data
);
var_export
(
$res
);
}
public
function
deleteDoc
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$res
=
$elasticClient
->
deleteDoc
(
$index
,
'test_id'
);
var_export
(
$res
);
}
public
function
bulk
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$data
=
[
[
'index'
=>
[
'_index'
=>
$index
]],
[
'test_field'
=>
'公安部:各地校车将享最高路权'
],
[
'index'
=>
[
'_index'
=>
$index
]],
[
'test_field'
=>
'中韩渔警冲突调查:韩警平均每天扣1艘中国渔船'
],
[
'index'
=>
[
'_index'
=>
$index
]],
[
'test_field'
=>
'中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首'
],
];
$res
=
$elasticClient
->
bulkIndexDoc
(
$data
);
var_export
(
$res
);
}
public
function
searchDoc
()
:
void
{
$elasticClient
=
ElasticUtil
::
getInstance
();
$index
=
'demo'
;
$data
=
[
'query'
=>
[
'match'
=>
[
'test_field'
=>
'中国'
]
],
'highlight'
=>
[
'pre_tags'
=>
[
'<tag1>'
,
'<tag2>'
],
'post_tags'
=>
[
'</tag1>'
,
'</tag2>'
],
"fields"
=>
[
'test_field'
=>
new
\stdClass
()
]
]
];
$res
=
$elasticClient
->
searchDoc
(
$index
,
$data
);
var_export
(
$res
);
}
}
\ No newline at end of file
public/elastic.php
0 → 100644
View file @
edf34efd
<?php
ini_set
(
"display_errors"
,
"On"
);
//打开错误提示
ini_set
(
"error_reporting"
,
E_ALL
);
//显示所有错误
/*
* cli入口脚本
* cli 配置文件:conf/cli.ini
* cli bootstrap:application/BootstrapCli.php ( 在cli.ini中配置
* 默认模块:modules/cli
* 脚本位置:modules/cli/controllers/xxx.php
* 调用方式:php cli.php controller action "a=1&b=2"
* 测试脚本:php cli.php test index "a=1&b=2"
*/
if
(
!
substr
(
php_sapi_name
(),
0
,
3
)
==
'cli'
)
{
die
;
}
define
(
'APPLICATION_PATH'
,
realpath
(
__DIR__
.
'/../'
));
require
APPLICATION_PATH
.
'/vendor/autoload.php'
;
$application
=
new
Yaf\Application
(
APPLICATION_PATH
.
"/conf/cli.ini"
);
$es
=
new
\App\Services\demo\ElasticService
();
//$es->createIndex();
//$es->getSettings();
//$es->putSettings();
//$es->getSettings();
//$es->getMapping();
//$es->putMapping();
//$es->getMapping();
//$es->indexDoc();
//$es->bulk();
//$es->searchDoc();
//$es->updateDoc();
//$es->getDoc();
//$es->deleteDoc();
//$es->deleteIndex();
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment