承接 rayful/elasticsearch 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rayful/elasticsearch

Composer 安装命令:

composer require rayful/elasticsearch

包简介

elasticsearch client for php

README 文档

README

使用 PHP 对 Elasticsearch 做索引管理,以及索引新文档

安装

$ composer install

添加 mapping

use Rayful\Elasticsearch\Builder;
use Rayful\Elasticsearch\Writer;

$connectionParams = '127.0.0.1:9200';
$builder = new Builder($connectionParams);
$writer = new Writer($builder);

$db = 'test';
$collection = 'user';

$properties = [
    'name' => a['type' => 'string'],
    'age' => ['type' => 'integer'],
    'balance' => ['type' => 'double'],
    'create_at' => ['type' => 'date']
];

// 更多 mapping type 见官方文档: https://www.elastic.co/guide/en/elasticsearch/reference/2.3/mapping-types.html

$writer->setNamespace($db, $collection)->putMapping($properties);

索引单个文档

use Rayful\Elasticsearch\Builder;
use Rayful\Elasticsearch\Writer;

$connectionParams = '127.0.0.1:9200';
$builder = new Builder($connectionParams);
$writer = new Writer($builder);

$db = 'test';
$collection = 'user';

$properties = [
    'name' => ['type' => 'string'],
    'age' => ['type' => 'integer'],
    'balance' => ['type' => 'double'],
    'create_at' => ['type' => 'date']
];

$document = [
    'id'=> '59c1cfb9a008b',
    'name' => 'lvinkim-'.rand(1,1000),
    'age' => rand(10,90),
    'balance' => rand(0,1000),
    'create_at' => time()
];

$writer->setNamespace($db, $collection);

// 手动指定 mapping 字段的类型,如果不手动指定,将由 Elasticsearch 自动生成
$writer->putMapping($properties);

$response = $writer->indexSingleDocument($document);

print_r($response);

索引批量文档

use Rayful\Elasticsearch\Builder;
use Rayful\Elasticsearch\Writer;

$connectionParams = '127.0.0.1:9200';
$builder = new Builder($connectionParams);
$writer = new Writer($builder);

$db = 'test';
$collection = 'user';

$properties = [
    'name' => ['type' => 'string'],
    'age' => ['type' => 'integer'],
    'balance' => ['type' => 'double'],
    'create_at' => ['type' => 'date']
];

$documents = genDocuments();

$writer->setNamespace($db, $collection);

// 手动指定 mapping 字段的类型,如果不手动指定,将由 Elasticsearch 自动生成
$writer->putMapping($properties);

$writer->indexMultiDocuments($documents);

function genDocuments()
{
    for ($i = 0; $i < 10000; $i++) {
        $document = [
            'id' => $i . '-' . uniqid(),
            'name' => 'lvinkim-' . rand(1, 1000),
            'age' => rand(10, 90),
            'balance' => rand(0, 1000),
            'create_at' => time()
        ];
        yield $document;
    }
}

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固