博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 操作 elasticsearch
阅读量:4299 次
发布时间:2019-05-27

本文共 2506 字,大约阅读时间需要 8 分钟。

文章目录

列出所有索引

列出所有索引(列出所有的数据库)GET /_cat/indices?v

添加索引

PUT /goods{
"settings": {
// 副本数 "number_of_replicas": 1, // 分片数 "number_of_shards": 5 }}

删除索引

DELETE /goods

修改文档

POST /goods/_doc/1/_update{
"doc": {
"price":100 }}

查询(搜索)

GET /goods/_search// 查询是xiaomi9的GET /goods/_search{
"query": {
"match": {
"title": "xiaomi9" } }}// 排序GET /goods/_search{
"query": {
"match_all": {
} }, "sort": [ {
"_id": {
"order": "desc" } } ]}## 进行中文分词搜索PUT /goods{
"mappings": {
"_doc":{
"properties":{
"name":{
"type":"text", "analyzer":"ik_max_word", "search_analyzer":"ik_max_word" }, "desn":{
"type":"text", "analyzer":"ik_max_word", "search_analyzer":"ik_max_word" } } } }}

PHP操作ES

官网:

创建索引

$hosts = [    '127.0.0.1:9200'];$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();// 创建索引   number_of_shards   分区数后期不可更改    number_of_replicas   副本数后期可更改$params = [    'index' => 'goods',    'body' => [        'settings' => [            'number_of_shards' => 5,            'number_of_replicas' => 1        ],        'mappings' => [            '_doc' => [                '_source' => [                    'enabled' => true                ],                'properties' => [                    'title' => [                        'type' => 'keyword'                    ],                    'desn' => [                        'type' => 'text',                        'analyzer' => 'ik_max_word',                        'search_analyzer' => 'ik_max_word'                    ]                ]            ]        ]    ]];$response = $client->indices()->create($params);

更新文档

$hosts = [    '127.0.0.1:9200',];$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();// 写文档$params = [    'index' => 'goods',    'type' => '_doc',    'id' => $model->id,    'body' => [        'title' => $model->title,        'desn' => $model->desn,    ],];$response = $client->index($params);

搜索

$hosts = [    '127.0.0.1:9200',];$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();$params = [    'index' => 'goods',    'type' => '_doc',    'body' => [        'query' => [            'match' => [                'title'=>[                    'query' => '手机'                ]            ]        ]    ]];$results = $client->search($params);dump($results);

转载地址:http://priws.baihongyu.com/

你可能感兴趣的文章
量化策略回测ATRRSI
查看>>
量化策略回测tdma
查看>>
量化策略回测TRIXKDJ
查看>>
量化策略回测唐安奇通道
查看>>
CTA策略如何过滤部分震荡行情?
查看>>
量化策略回测DualThrust
查看>>
量化策略回测BoolC
查看>>
量化策略回测DCCV2
查看>>
mongodb查询优化
查看>>
五步git操作搞定Github中fork的项目与原作者同步
查看>>
git 删除远程分支
查看>>
删远端分支报错remote refs do not exist或git: refusing to delete the current branch解决方法
查看>>
python multiprocessing遇到Can’t pickle instancemethod问题
查看>>
APP真机测试及发布
查看>>
iOS学习之 plist文件的读写
查看>>
通知机制 (Notifications)
查看>>
10 Things You Need To Know About Cocoa Auto Layout
查看>>
C指针声明解读之左右法则
查看>>
一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
查看>>
iOS 如何放大按钮点击热区
查看>>