Coder Social home page Coder Social logo

evilfreelancer / laravel-manticoresearch Goto Github PK

View Code? Open in Web Editor NEW
20.0 4.0 7.0 29 KB

An easy way to use the official ManticoreSearch client in your Laravel or Lumen applications.

License: MIT License

PHP 100.00%
laravel laravel-package manticoresearch lumen lumen-package

laravel-manticoresearch's Introduction

Latest Stable Version Total Downloads Build Status Code Coverage Code Climate Scrutinizer Code Quality License

Laravel ManticoreSearch plugin

An easiest way to use the official ManticoreSearch client in your Laravel or Lumen applications.

composer require evilfreelancer/laravel-manticoresearch

Post install

Laravel

The package's service provider will automatically register its service provider.

Publish the configuration file:

php artisan vendor:publish --provider="ManticoreSearch\Laravel\ServiceProvider"

Alternative configuration method via .env file

After you publish the configuration file as suggested above, you may configure ManticoreSearch by adding the following to your application's .env file (with appropriate values):

MANTICORESEARCH_HOST=localhost
MANTICORESEARCH_PORT=9200
MANTICORESEARCH_TRANSPORT=Http
MANTICORESEARCH_USER=
MANTICORESEARCH_PASS=

All available environments variables

Name Default value Description
MANTICORESEARCH_CONNECTION default Name of default connection
MANTICORESEARCH_HOST localhost Address of host with Manticore server
MANTICORESEARCH_PORT 9308 Port number with REST server
MANTICORESEARCH_TRANSPORT Http Type of transport, can be: Http, Https, PhpHttp or your custom driver
MANTICORESEARCH_USER Username
MANTICORESEARCH_PASS Password
MANTICORESEARCH_TIMEOUT 5 Timeout between requests
MANTICORESEARCH_CONNECTION_TIMEOUT 1 Timeout before connection
MANTICORESEARCH_PROXY Url of HTTP proxy server
MANTICORESEARCH_PERSISTENT true Define whenever connection is persistent or not
MANTICORESEARCH_RETRIES 2 Amount of retries if connection is lost

Lumen

If you work with Lumen, please register the service provider and configuration in bootstrap/app.php:

// Enable shortname of facade
$app->withFacades(true, [
    'ManticoreSearch\Laravel\Facade' => 'Facade',
]);

// Register Config Files
$app->configure('manticoresearch');

// Register Service Providers
$app->register(ManticoreSearch\Laravel\ServiceProvider::class);

Manually copy the configuration file to your application.

How to use

The ManticoreSearch facade is just an entry point into the ManticoreSearch client, so previously you might have used:

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

$config = ['host'=>'127.0.0.1', 'port'=>9308];
$client = new \Manticoresearch\Client($config);
$index  = new \Manticoresearch\Index($client);
$index->setName('movies'); 

Instead of these few lines above you can use single line solution:

$index = \ManticoreSearch::index('movies');

That will run the command on the default connection. You can run a command on any connection (see the defaultConnection setting and connections array in the configuration file).

$index   = \ManticoreSearch::connection('connectionName')->index($nameOfIndex);
$pq      = \ManticoreSearch::connection('connectionName')->pq();
$cluster = \ManticoreSearch::connection('connectionName')->cluster();
$indices = \ManticoreSearch::connection('connectionName')->indices();
$nodes   = \ManticoreSearch::connection('connectionName')->nodes();

// etc...

methods of the Client class:

\ManticoreSearch::connection('connectionName')->sql($params);
\ManticoreSearch::connection('connectionName')->replace($params);
\ManticoreSearch::connection('connectionName')->delete($params);

// etc...

Lumen users who aren't using facades will need to use dependency injection, or the application container in order to get the ManticoreSearch Index object:

// using injection:
public function handle(\ManticoreSearch\Laravel\Manager $manticoresearch)
{
    $manticoresearch->describe();
}

// using application container:
$manticoreSearch = $this->app('manticoresearch');

Of course, dependency injection and the application container work for Laravel applications as well.

Logging

Since the PHP client of the ManticoreSearch supports logging through PSR-compatible loggers, you can use them in the same way as presented in the official documentation.

For example, you want to use the Monolog logger.

composer require monolog/monolog

By default, you need to write something like this:

$logger = new \Monolog\Logger('name');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('/my/log.file', Logger::INFO));
$config = ['host' => '127.0.0.1', 'port' => 9306];
$client = new \Manticoresearch\Client($config, $logger);
$index  = new \Manticoresearch\Index($client);
$index->setName('movies');

But if you want to use the Monolog together with this library then you may simplify your code like this:

$logger = new \Monolog\Logger('name');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('/my/log.file', Logger::INFO));

$index = \ManticoreSearch::connection('connectionName', $logger)->index('movies');

Testing

Just install dev requirements composer install --dev, then execute following command from root of this library:

./vendor/bin/phpunit

Links

laravel-manticoresearch's People

Contributors

evilfreelancer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel-manticoresearch's Issues

I get instant response back dumped to screen from api

In the below example when I try to loop the result I get back the result object only as a response before I can use it for the loop.
Any idea why? I don't dd() or dump() the response anywhere:

// I use Lumen, the test movies are already there and indexed
$manticoreSearch = app('manticoresearch');
$search = $manticoreSearch->index('movies');
$bool = new \Manticoresearch\Query\BoolQuery();
$bool->must(new \Manticoresearch\Query\Match(['query' => 'team of explorers', 'operator' => 'and'], 'title,plot'));

$results = $search->search($bool)->get(); // at this line the object is dumped to screen

foreach($results as $doc) {
  echo 'Document:'.$doc->getId()."\n";
  foreach($doc->getData() as $field=>$value)
  {   
      echo $field.": ".$value."\n";
  }
}

And the result shown in my browser:

^ Manticoresearch\Response {#98 ▼
  #time: 0.0031960010528564
  #string: "{"took":0,"timed_out":false,"hits":{"total":1,"hits":[{"_id":"2","_score":3565,"_source":{"year":2014,"rating":8.500000,"title":"Interstellar","plot":"A team of ▶"
  #transportInfo: array:3 [▶]
  #status: 200
  #response: array:3 [▶]
}

screenshot:
Screenshot 2020-09-16 at 10 32 51

If I try to dd() the result nothing happens, above response object shown in browser again:

...
$results = $search->search($bool)->get();

dd($results);

foreach($results as $doc) {
   echo 'Document:'.$doc->getId()."\n";
   foreach($doc->getData() as $field=>$value)
   {   
        echo $field.": ".$value."\n";
   }
}

How to process the response I want furthermore, and don't get the response dumped as object ?
Thanks

EDIT: it's in a simple GET request method for testing the search

EDIT 2: if I use the package way (and not directly the manticoresearch-php classes), like
$results = $movies->search('team of explorers')->get();
the thing is the same

Question about Facade

Hello,
I cannot understand, how can the following code work (from the README.md file):

\ManticoreSearch::connection('connectionName')->sql($params);

... since there is no ManticoreSearch class in the package? I just get "Class "ManticoreSearch" not found" error.

And facade class is named \ManticoreSearch\Laravel\Facade.

So if I write

\ManticoreSearch\Laravel\Facade::connection('connectionName')->sql($params);

.. it works, but it's not very neat syntax.

mysql connection error?

Am I right that I can't connect to Mysql with manticore-php package? As in the docs, the transport layer can be Http, Https only. If not, any hint how to make it work?

searchd runs on following ports, and I created an index without error:

tcp4       0      0  *.9306                 *.*                    LISTEN      131072 131072   9254      0 0x0180 0x00000206
tcp4       0      0  *.9312                 *.*                    LISTEN      131072 131072   9254      0 0x0180 0x00000206

searchd config:

searchd
{
    listen                  = 9312
    listen                  = 9306:mysql
    pid_file                = /usr/local/var/run/manticore/searchd.pid
    max_matches             = 1000
    binlog_path             = /var/data
}

what should I set for MANTICORESEARCH_PORT then in the config file? I tried both 9306 & 9312.

I get the following connection error btw:

^ Manticoresearch\Exceptions\NoMoreNodesException {#97 ▼
  #request: null
  #message: "No more retries left"
  #code: 0
  #file: "/Users/x/web/y/conference-api/vendor/manticoresoftware/manticoresearch-php/src/Manticoresearch/Connection/ConnectionPool.php"
  #line: 62
  trace: {▼
    /Users/x/web/y/conference-api/vendor/manticoresoftware/manticoresearch-php/src/Manticoresearch/Connection/ConnectionPool.php:62 {▶}
    /Users/x/web/y/conference-api/vendor/manticoresoftware/manticoresearch-php/src/Manticoresearch/Client.php:340 {▼
      Manticoresearch\Client->request(Request $request, array $params = []): Response …
      › try {
      ›     $connection = $this->connectionPool->getConnection();
      ›     $this->lastResponse = $connection->getTransportHandler($this->logger)->execute($request, $params);
    }
    /Users/x/web/y/conference-api/vendor/manticoresoftware/manticoresearch-php/src/Manticoresearch/Client.php:359 {▼
      Manticoresearch\Client->request(Request $request, array $params = []): Response …
      › 
      ›     return $this->request($request, $params);
      › }
      arguments: {▶}
    }
    /Users/x/web/y/conference-api/vendor/manticoresoftware/manticoresearch-php/src/Manticoresearch/Client.php:169 {▼
      Manticoresearch\Client->search(array $params = [], $obj = false) …
      › $endpoint = new Endpoints\Search($params);
      › $response = $this->request($endpoint);
      › if ($obj === true) {
      arguments: {▶}
    }
    /Users/x/web/y/conference-api/vendor/manticoresoftware/manticoresearch-php/src/Manticoresearch/Search.php:290 {▼
      Manticoresearch\Search->get() …
      › $this->body = $this->compile();
      › $resp = $this->client->search(['body' => $this->body], true);
      › return new ResultSet($resp);
      arguments: {▶}
    }
    /Users/x/web/y/conference-api/app/Http/Controllers/SearchApiController.php:122 {▼
      App\Http\Controllers\SearchApiController->test3() …
      › 
      › $results = $conferences->search('searchterm')->get();
      › 
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.