elasticsearch 使用命令

3-20 2,432 views

文档地址:
https://www.elastic.co/guide/en/elasticsearch/client/php-api/5.0/index.html
https://packagist.org/packages/elasticsearch/elasticsearch
es权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/foreword_id.html

首先安装composer

在composer添加

{
    "require": {
        "elasticsearch/elasticsearch": "~6.0"
    }
}

composer update 安装成功

基础概念

  • 索引 : 含有相同属性的文档集合 (mysql 表)
  • 类型 : 索引可定义一个类型或多个类型,文档必须属于一个类型(类似分类)
  • 文档 : 文档是可以被索引的基本单位(mysql一条记录)
  • 分片 : 每个索引有多个分片,每个分片都是一个lucene 索引(分片在创建索引时创建,不可修改)
  • 备份 : 拷贝一份分片,就完成了分片的备份

字段类型

[图解Elasticsearch中的_source、_all、store和index属性](https://blog.csdn.net/napoay/article/details/62233031 "图解Elasticsearch中的_source、_all、store和index属性")
[es字段类型](https://blog.csdn.net/chengyuqiang/article/details/79048800 "es字段类型")

建立索引

$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
#创建一个索引名为blogs,内含news type的索引 ,number_of_shards 分片,number_of_replicas备份
        $params = [
            'index' => 'blogs',
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 1
                ],
                'mappings' => [
                    'news' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties' => [
                            'title' => [
                                'type' => 'keyword',
                            ],
                            'content' => [
                                'type' => 'text'
                            ],
                            'author' => [
                                'type' => 'keyword',
                            ],
                            'created_at' => [
                                'type' => 'date',
                                'format' => 'yyyy-MM-dd HH:mm:ss'
                            ],
                            'updated_at' => [
                                'type' => 'date',
                                'format' => 'yyyy-MM-dd HH:mm:ss'
                            ]
                        ]
                    ],
                ]
            ]
        ];
#创建索引
$es_client->indices()->create($params);

使用

 'blogs',
                'type' => 'news',
                'id' => '1',
                'body' => [
                    'title' => 'first blog',
                    'content' => 'it is a test blog',
                    'author' => 'xiaoli',
                    'created_at' => '2019-03-20 12:12:22',
                    'updated_at' => '2019-03-20 12:12:22',
                ]
            ];
$client->index($params);

#更新数据
$params = [
    'index' => 'blogs',
    'type' => 'news',
    'id' => $post['id'],
    'body' => [
        'doc' => [
        'title' =>  $post['title'],
        'content' =>  $post['content'],
        ]
    ]
];
$client->update($params);

#删除数据
$params = [
    'index' => 'blogs',
    'type' => 'news',
    'id' => $id
];
// Delete doc at /blogs/news/my_id
$client->delete($params);

#搜索,match
$json = '{
    "query" : {
        "match" : {
            "title" : "'.$keywords.'"
        }
    }
}';

$params = [
    'index' => 'blogs',
    'type' => 'news',
    'body' => $json
];
$client->search($params);

#排序
$json = '{
    "sort": { "created_at": { "order": "desc" }}
 }';
#从零开始取10条
$params = [
    'index' => 'blogs',
    'type' => 'news',
    'size' => 10,
    'from' => 0,
    'body' => $json
];
$client->search($params);

#完全匹配
$params = '{
    "query":{
        "term": {"title":"'.$keywords.'"}
    }
}';

#组合查询
{
   "bool" : {
      "must" :     [],
      "should" :   [],
      "must_not" : [],
   }
}
must
所有的语句都 必须(must) 匹配,与 AND 等价。
must_not
所有的语句都 不能(must not) 匹配,与 NOT 等价。
should
至少有一个语句要匹配,与 OR 等价。
#
    $json = '{
            "query" : {
                "bool" : {
                    "should": [
                        {
                            "match" : { "title" : "测试" }
                        },
                        {
                            "match" : { "content" : "你好" }
                        }
                    ]
                }
            }
        }';

一个简单的service类


namespace App\Services;
use Elasticsearch\ClientBuilder;
class ElasticSearchService{

    protected $es_client;

    public function __construct()
    {
        $this->es_client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();

    }
    /*
     * 建立索引
     */
    public function createIndex($params = []){

        return $this->es_client->indices()->create($params);
    }

    /*
     * 删除索引
     */
    public function deleteIndex($params = []){

        return $this->es_client->indices()->delete($params);
    }

    /*
     * 更新索引
     */
    public function putIndex($params = []){

        return $this->es_client->indices()->putSettings($params);
    }

    /*
     *  获得索引信息
     */
    public function getIndex($params = []){

        return $this->es_client->indices()->getSettings($params);
    }

    /*
     * 插入数据
     */
    public function insertData($params = []){

        return $this->es_client->index($params);
    }

    /*
     * 批量插入数据
     */
    public function insertDatas($params = []){

        return $this->es_client->bulk($params);
    }

    /*
     * 更新数据
     */
    public function updateData($params = []){

        return $this->es_client->update($params);
    }

    /*
     * 删除数据
     */
    public function deleteData($params = []){

        return $this->es_client->delete($params);
    }

    /*
     * 搜索数据
     */
    public function searchData($params = []){

        return $this->es_client->search($params);
    }

}
?>

解决 laravel-admin between datetime 假如数据库是时间戳int类型无法筛选。

laravel-admin默认的between->datetime(),查询默认是datetime类型,但是假如数据库是时间戳类型就会报错,又不想改底层文件的话可以试试加自定义筛选功能...

阅读全文

php解析英文语句,自动分解。

参考:https://www.php.net/manual/en/function.str-split.php 最近碰到一个问题,客户的英文地址太长,超出接口api字段长度,所以需要解析下语句分解发送。 ...

阅读全文

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

欢迎留言