elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
42 stars 969 forks source link

Installation not via composer #35

Closed jayzalowitz closed 10 years ago

jayzalowitz commented 10 years ago

If i just want to include the php lib and don't have composer for various reasons, how would i do so.

jayzalowitz commented 10 years ago

I am assuming https://github.com/elasticsearch/elasticsearch-php/blob/master/src/Elasticsearch/Client.php is the correct file, just want to be sure.

polyfractal commented 10 years ago

Hi there, sorry for the delay...I was on vacation without much email access.

The easiest solution is to run composer locally on your machine and upload the generated files to your server. Internally, composer is A) downloading all the dependencies and B) generating an "autoload.php" file which has code to include all the dependencies. Once everything is generated, the files and directory structure are static and can easily be moved around.

If you can't run composer at all, you will need to find a way to include all the relevant files (library + dependencies), not just the Client.php script. I can look into including a manual autoloader in the client itself so that composer isn't strictly required. It will still require you to download all the dependencies manually.

jayzalowitz commented 10 years ago

Just a fyi, it seems as though you cannot load composer on micro ec2-instances on aws, so this is a really big issue for some people. looking into your fix.. Thanks.

polyfractal commented 10 years ago

Why does it not work on a micro instance? Memory constraints?

The general composer recommendation for small memory footprints is to enable swap, install to a local deployment and upload the composer.lock file to your micro instance.

Once the composer.lock is uploaded, you can do a composer install which should considerably less memory than a composer update.

jayzalowitz commented 10 years ago

[ec2-user@ip-10-51-135-195 test]$ php composer.phar install --no-dev Loading composer repositories with package information Installing dependencies from lock file Nothing to install or update Generating autoload files [ec2-user@ip-10-51-135-195 test]$ php elasticdemo.php PHP Fatal error: Class 'ElasticSearch\Client' not found in /var/www/html/elasticdemo.php on line 9

polyfractal commented 10 years ago

I'm sorry, I'm having a very hard time reproducing your problem. I just spun up a 64bit Amazon Linux on a micro instance. Everything worked as it should.

I updated packages, installed PHP, then installed tree:

[ec2-user@ip-10-141-184-210 ~]$ sudo yum update
[ec2-user@ip-10-141-184-210 ~]$ sudo yum install php
[ec2-user@ip-10-141-184-210 ~]$ sudo yum install tree

Then installed composer + the client:

[ec2-user@ip-10-141-184-210 ~]$ pwd
/home/ec2-user
[ec2-user@ip-10-141-184-210 ~]$ mkdir test
[ec2-user@ip-10-141-184-210 ~]$ cd test
[ec2-user@ip-10-141-184-210 test]$ nano composer.json

  GNU nano 2.0.9             File: composer.json                                

{
        "require": {
            "elasticsearch/elasticsearch": "~0.4"
        }
    }
                               [ Wrote 5 lines ]

[ec2-user@ip-10-141-184-210 test]$ curl -s http://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /home/ec2-user/test/composer.phar
Use it: php composer.phar
[ec2-user@ip-10-141-184-210 test]$ php composer.phar install --no-dev
Loading composer repositories with package information
Installing dependencies
  - Installing psr/log (1.0.0)
    Downloading: 100%         

  - Installing symfony/event-dispatcher (v2.4.1)
    Downloading: 100%         

  - Installing guzzle/guzzle (v3.8.0)
    Downloading: 100%         

  - Installing pimple/pimple (v1.1.0)
    Downloading: 100%         

  - Installing monolog/monolog (1.7.0)
    Downloading: 100%         

  - Installing elasticsearch/elasticsearch (v0.4.3)
    Downloading: 100%         

symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/event-dispatcher suggests installing symfony/http-kernel ()
monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
Writing lock file
Generating autoload files
[ec2-user@ip-10-141-184-210 test]$ ls
composer.json  composer.lock  composer.phar  vendor
[ec2-user@ip-10-141-184-210 test]$ nano test.php

  GNU nano 2.0.9              File: test.php                                    

<?php
    require 'vendor/autoload.php';

    $client = new Elasticsearch\Client(); 

                               [ Wrote 4 lines ]
[ec2-user@ip-10-141-184-210 test]$ tree ../ -L 4
../
└── test
    ├── composer.json
    ├── composer.lock
    ├── composer.phar
    ├── test.php
    └── vendor
        ├── autoload.php
        ├── composer
        │   ├── autoload_classmap.php
        │   ├── autoload_namespaces.php
        │   ├── autoload_psr4.php
        │   ├── autoload_real.php
        │   ├── ClassLoader.php
        │   └── installed.json
        ├── elasticsearch
        │   └── elasticsearch
        ├── guzzle
        │   └── guzzle
        ├── monolog
        │   └── monolog
        ├── pimple
        │   └── pimple
        ├── psr
        │   └── log
        └── symfony
            └── event-dispatcher

15 directories, 11 files

[ec2-user@ip-10-141-184-210 test]$ php test.php
[ec2-user@ip-10-141-184-210 test]

I don't have elasticsearch running on the micro instance, so the client doesn't do anything. But it doesn't throw errors which means that the autoload has worked correctly, etc.

jayzalowitz commented 10 years ago

Call $es = Client::connection(); and see if it still dosent fail (this is where it fails on my scripts)

polyfractal commented 10 years ago

There is no static method called "connection" in the client. In fact, there are no static methods in the entire library...

Can you gist up your complete code so I can see what you are attempting to do?

jayzalowitz commented 10 years ago

Here is a Very limited version https://gist.github.com/jayzalowitz/7ab7b3b5e0233330ee65

polyfractal commented 10 years ago

It appears you are using the syntax for Nervetattoo/Elasticsearch, which is a different (unofficial) client library. Please consult the author of that library for syntax help.

Alternatively, if you want to use official Elasticsearch/Elasticsearch library...documentation is here: http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/index.html

The syntax is considerably different. Take some time to read the Quickstart and various examples in the readme. I'd be happy to help once you've converted the syntax over to this library. :)

jayzalowitz commented 10 years ago

Nailed that issue, converted it over as far as I can understand and it still isn't functioning under localhost

neither require 'vendor/autoload.php';

$esstartparams = array(); $esstartparams['hosts'] = array('http://localhost:9200'); $es = new Elasticsearch\Client($esstartparams); nor $es = new Elasticsearch\Client(); yeild anything other than a message similar to (With respect to the index data) Notice: Undefined index: data in /var/www/html/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Namespaces/IndicesNamespace.php on line 307 when running any function, be it $es->indices()->getMapping(); $es->search($searchParams); $es->get($getParams); any advice?

jayzalowitz commented 10 years ago

Disregard this, this is what happens when your es is down ... different issue (micros+es just does not really stay alive on an active application)

berendo commented 10 years ago

I'm not sure if this is the exact right context to ask this question, but could someone answer the quick question of what the specific dependency on the minimum PHP version of 5.3.9 (as opposed to 5.3.x) is? It was introduced here 182fcb8, but no specific reason was logged. Thanks much!

polyfractal commented 10 years ago

@jayzalowitz Ah, yeah, that particular bug was fixed in this commit (https://github.com/elasticsearch/elasticsearch-php/commit/63cfea80824416fc4c77490e345d951461e4c09d). The client should throw an exception when ES is not available, but that bug was preventing it from happening. I'll be pushing a new tag soon and this bugfix will be available for regular installations.

@berendo In short, it's a bug in PHP versions under 5.3.9. I wrote a longer explanation here: https://github.com/elasticsearch/elasticsearch-php/issues/10#issuecomment-26259850 . I've had several people ask about the 5.3.9 dependency lately...I'll make a note and explanation about it in the readme so that it is easier to find.

(Also, sorry, I should have written a better commit message on that version change. Oops!)

polyfractal commented 10 years ago

@jayzalowitz I pushed the v0.4.4 tag this morning (https://github.com/elasticsearch/elasticsearch-php/tree/v0.4.4), which contains the fix for the no-host bug.

Closing this issue, let me know if you have any more problems!