PHP code example of anyitsolutions / elastic-adapter
1. Go to this page and download the library: Download anyitsolutions/elastic-adapter library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
anyitsolutions / elastic-adapter example snippets
$client = \Elasticsearch\ClientBuilder::fromConfig([
'hosts' => [
'localhost:9200'
]
]);
$indexManager = new \ElasticAdapter\Indices\IndexManager($client);
$index = new \ElasticAdapter\Indices\Index('my_index');
$indexManager->create($index);
$mapping = (new \ElasticAdapter\Indices\Mapping())
->text('title', [
'boost' => 2,
])
->keyword('tag', [
'null_value' => 'NULL'
])
->geoPoint('location');
$settings = (new \ElasticAdapter\Indices\Settings())
->index([
'number_of_replicas' => 2,
'refresh_interval' => -1
]);
$index = new \ElasticAdapter\Indices\Index('my_index', $mapping, $settings);
$indexManager->create($index);
$indexManager->drop('my_index');
$mapping = (new \ElasticAdapter\Indices\Mapping())
->text('title', [
'boost' => 2,
])
->keyword('tag', [
'null_value' => 'NULL'
])
->geoPoint('location');
$indexManager->putMapping('my_index', $mapping);
$settings = (new \ElasticAdapter\Indices\Settings())
->analysis([
'analyzer' => [
'content' => [
'type' => 'custom',
'tokenizer' => 'whitespace'
]
]
]);
$indexManager->putSettings('my_index', $settings);
$indexManager->exists('my_index');
$indexManager->open('my_index');
$indexManager->close('my_index');
$client = \Elasticsearch\ClientBuilder::fromConfig([
'hosts' => [
'localhost:9200'
]
]);
$documentManager = new \ElasticAdapter\Documents\DocumentManager($client);
$documents = [
new ElasticAdapter\Documents\Document('1', ['title' => 'foo']),
new ElasticAdapter\Documents\Document('2', ['title' => 'bar']),
];
$documentManager->index('my_index', $documents);
$documentManager->index('my_index', $documents, true);
$documents = [
new ElasticAdapter\Documents\Document('1', ['title' => 'foo']),
new ElasticAdapter\Documents\Document('2', ['title' => 'bar']),
];
$documentManager->delete('my_index', $documents);
$documentManager->delete('my_index', $documents, true);
$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);
$request = new \ElasticAdapter\Search\SearchRequest([
'match' => [
'message' => 'test'
]
]);
$request->setHighlight([
'fields' => [
'message' => [
'type' => 'plain',
'fragment_size' => 15,
'number_of_fragments' => 3,
'fragmenter' => 'simple'
]
]
]);
$request->setSuggest([
'my_suggest' => [
'text' => 'test',
'term' => [
'field' => 'message'
]
]
]);
$request->setSource(['message', 'post_date']);
$request->setCollapse([
'field' => 'user'
]);
$request->setSort([
['post_date' => ['order' => 'asc']],
'_score'
]);
$request->setFrom(0)->setSize(20);
$response = $documentManager->search('my_index', $request);
// total number of matched documents
$total = $response->getHitsTotal();
// corresponding hits
$hits = $response->getHits();
// document, highlight or raw representation of the hit
foreach ($hits as $hit) {
$document = $hit->getDocument();
$highlight = $hit->getHighlight();
$raw = $hit->getRaw();
}
// suggestions
$suggestions = $response->getSuggestions();