百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

elasticsearch

> 编程语言
开源

用于 ElasticSearch 的简单 PHP 客户端

342 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于 ElasticSearch 的简单 PHP 客户端

ElasticSearch PHP client

ElasticSearch is a distributed lucene powered search indexing, this is a PHP client for it

Usage

Initial setup

  1. Install composer. curl -s http://getcomposer.org/installer | php

  2. Create composer.json containing:

    javascript
    {
        "require" : {
            "nervetattoo/elasticsearch" : ">=2.0"
        }
    }
  3. Run ./composer.phar install

  4. Keep up-to-date: ./composer.phar update

Indexing and searching

php
require_once __DIR__ . '/vendor/autoload.php';

use \ElasticSearch\Client;
// The recommended way to go about things is to use an environment variable called ELASTICSEARCH_URL
$es = Client::connection();

// Alternatively you can use dsn string
$es = Client::connection('http://127.0.0.1:9200/myindex/mytype');

$es->index(array('title' => 'My cool document'), $id);
$es->get($id);
$es->search('title:cool');

Creating mapping

php
$es->map(array(
    'title' => array(
        'type' => 'string',
	'index' => 'analyzed'
    )
));

Search multiple indexes or types

php
$results = $es
    ->setIndex(array("one", "two"))
    ->setType(array("mytype", "other-type"))
    ->search('title:cool');

Using the Query DSL

php
$es->search(array(
    'query' => array(
        'term' => array('title' => 'cool')
    )
);

Provide configuration as array

Using an array for configuration also works

php
$es = Client::connection(array(
    'servers' => '127.0.0.1:9200',
    'protocol' => 'http',
    'index' => 'myindex',
    'type' => 'mytype'
));

Support for Routing

php
$document = array(
    'title' => 'My routed document',
    'user_id' => '42'
);
$es->index($document, $id, array('routing' => $document['user_id']));
$es->search('title:routed', array('routing' => '42'));

Support for Bulking

php
$document = array(
    'title' => 'My bulked entry',
    'user_id' => '43'
);
$es->beginBulk();
$es->index($document, $id, array('routing' => $document['user_id']));
$es->delete(2);
$es->delete(3);
$es->commitBulk();

$es->createBulk()
    ->delete(4)
    ->index($document, $id, 'myIndex', 'myType', array('parent' => $parentId));
    ->delete(5)
    ->delete(6)
    ->commit();

Usage as a service in Symfony2

In order to use the Dependency Injection to inject the client as a service, you'll have to define it before. So in your bundle's services.yml file you can put something like this :

yaml
    your_bundle.elastic_transport:
        class: ElasticSearch\Transport\HTTP
        arguments:
            - localhost
            - 9200
            - 60

    your_bundle.elastic_client:
        class: ElasticSearch\Client
        arguments:
            - @your_bundle.elastic_transport

To make Symfony2 recognize the ElasticSearch namespace, you'll have to register it. So in your app/autoload.php make sure your have :

php
// ...

$loader->registerNamespaces(array(
    // ...
    'ElasticSearch' => __DIR__.'/path/to/your/vendor/nervetattoo/elasticsearch/src',
));

Then, you can get your client via the service container and use it like usual. For example, in your controller you can do this :

php
class FooController extends Controller
{
    // ...

    public function barAction()
    {
        // ...
        $es = $this->get('your_bundle.elastic_client');
        $results = $es
            ->setIndex(array("one", "two"))
            ->setType(array("mytype", "other-type"))
            ->search('title:cool');
    }
}

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言