K
K
Konstantin2015-05-20 08:51:02
elasticsearch
Konstantin, 2015-05-20 08:51:02

Elastcisearch - how to use in PHP?

Please share a good example of using ElasticSearch in PHP, you are interested in filling the index with data and organizing a search on it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Narek, 2015-05-20
@Junart1

I advise you to read the Elasticsearch-PHP documentation

P
Puma Thailand, 2015-05-20
@opium

filling the index with data usually has little to do with php
search in the elastic server is usually done through the good old http request

M
MintTea, 2015-05-21
@MintTea

If it is uncritical for you to use the official client, I can recommend an alternative library: Elastica .
Here is an example of "filling the index with data and organizing searches":

use Elastica\Client;
use Elastica\Document;
use Elastica\Index;
use Elastica\Query;
use Elastica\QueryBuilder;

// Get client
$client = new Client();

// Get index
$index = $client->getIndex('index_name');
$index->create(array(), true);

// Get type
$tweets = $index->getType('tweets');
$tweets->addDocuments(array(
    new Document(1, ['author' => 'anton', 'text' => 'My first tweet']),
    new Document(2, ['author' => 'ivan', 'text' => 'My second tweet']),
    new Document(3, ['author' => 'vadim', 'text' => 'My third tweet']),
    new Document(4, ['author' => 'anton', 'text' => 'My fourth tweet']),
));

// Perform refresh to be able to search
$index->refresh();

// Get query builder
$qb = new QueryBuilder();

// Get query
$filter = $qb->filter()->term(['author' => 'vadim']);
$filteredQuery = $qb->query()->filtered($filter);
$query = new Query($filteredQuery);

// Search!
$resultSet = $tweets->search($query);

// Result set is iterable
foreach($resultSet as $result) {
    var_dump($result->getData());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question