Elasticsearch php

Notifications Star 4.5k Fork 851
Code Issues 22 Pull requests 17 Actions Projects 1 Security Insights
Code Issues Pull requests Actions Projects Security Insights
Open with obatambeienwasirherbal.com Desktop Download ZIP
Launching obatambeienwasirherbal.com Desktop

If nothing happens, download obatambeienwasirherbal.com Desktop and try again.

Bạn đang xem: Elasticsearch php

Go baông xã
Launching obatambeienwasirherbal.com Desktop

If nothing happens, tải về obatambeienwasirherbal.com Desktop and try again.

Go back
Launching Visual Studio

If nothing happens, download the obatambeienwasirherbal.com extension for Visual Studio and try again.

Go back


php composer.phar install

Require Composer"s autoloader

Composer also prepares an autoload file that"s capable of autoloading all the classes in any of the libraries that it downloads. To use it, just add the following line khổng lồ your code"s bootstrap process:


build ();

You can find out more on how to lớn install Composer, configure autoloading, và other best-practices for defining dependencies at getcomposer.org.

Xem thêm: sprintf php array

PHPhường. Version Requirement

Version 7.0 of this library requires at least PHPhường version 7.1. In addition, it requires the native JSON extension to be version 1.3.7 or higher.

Elasticsearch-PHP Branch PHP Version 7.0 >= 7.1.0 6.0 >= 7.0.0 5.0 >= 5.6.6 2.0 >= 5.4.0 0.4, 1.0 >= 5.3.9

Quickstart

Index a document

In elasticsearch-php, almost everything is configured by associative sầu arrays. The REST endpoint, document and optional parameters - everything is an associative sầu array.

Xem thêm: Sửa Lỗi Beginning Dump Of Physical Memory, Lỗi Màn Hình Xanh :(( Help Me!

To index a document, we need to lớn specify three pieces of information: index, id and a document body toàn thân. This is done by constructing an associative sầu array of key:value pairs. The request toàn thân is itself an associative array with key:value pairs corresponding to the data in your document:


$ params = < "index" => "my_index" , "id" => "my_id" , "body" => <"testField" => "abc" > >; $ response = $ client ->index ($ params ); print_r ($ response );

The response that you get baông chồng indicates the document was created in the index that you specified. The response is an associative array containing a decoded version of the JSON that Elastictìm kiếm returns:


Get a document

Let"s get the document that we just indexed. This will simply return the document:


$ params = < "index" => "my_index" , "id" => "my_id" >; $ response = $ client ->get ($ params ); print_r ($ response );

The response contains some metadata (index, version, etc.) as well as a _source field, which is the original document that you sent to lớn Elastictìm kiếm.


If you want lớn retrieve sầu the _source field directly, there is the getSource method:


Array ( => abc )

Search for a document

Searching is a hallmark of Elasticsearch, so let"s perform a tìm kiếm. We are going to lớn use the Match query as a demonstration:


$ params = < "index" => "my_index" , "body" => < "query" => < "match" => < "testField" => "abc" > > > >; $ response = $ client ->search ($ params ); print_r ($ response );

The response is a little different from the previous responses. We see some metadata (took, timed_out, etc.) and an array named hits. This represents your search results. Inside of hits is another array named hits, which contains individual tìm kiếm results:


Delete a document

Alright, let"s go ahead and delete the document that we added previously:


$ params = < "index" => "my_index" , "id" => "my_id" >; $ response = $ client ->delete ($ params ); print_r ($ response );

You"ll notice this is identical syntax lớn the get syntax. The only difference is the operation: delete instead of get. The response will confirm the document was deleted:


Array ( <_index> => my_index <_type> => _doc <_id> => my_id <_version> => 2 => deleted <_shards> => Array ( => 1 => 1 => 0 ) <_seq_no> => 1 <_primary_term> => 1 )

Delete an index

Due khổng lồ the dynamic nature of Elastictìm kiếm, the first document we added automatically built an index with some mặc định settings. Let"s delete that index because we want to lớn specify our own settings later:


Array ( => 1 )

Create an index

Now that we are starting fresh (no data or index), let"s add a new index with some custom settings:


Unit Testing using Mock a Elastic Client
use GuzzleHttp Ring Client MockHandler ; use Elasticsearch ClientBuilder ; // The connection class requires "body" khổng lồ be a file stream handle // Depending on what kind of request you bởi, you may need to lớn mix more values here $ handler = new MockHandler (< "status" => 200 , "transfer_stats" => < "total_time" => 100 >, "body" => fopen ("somefile.json" ), "effective_url" => "localhost" >); $ builder = ClientBuilder ::create (); $ builder ->setHosts (<"somehost" >); $ builder ->setHandler ($ handler ); $ client = $ builder ->build (); // Do a request and you"ll get baông chồng the "body" response above

Chuyên mục: