PHP code example of aman-rawat / elastic-adapter
1. Go to this page and download the library: Download aman-rawat/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/ */
aman-rawat / elastic-adapter example snippets
$client = \Elasticsearch\ClientBuilder::fromConfig([
'hosts' => [
'localhost:9200'
]
]);
$indexManager = new \ElasticAdapter\Indices\IndexManager($client);
$index = new \ElasticAdapter\Indices\IndexBlueprint('my_index');
$indexManager->create($index);
$mapping = (new \ElasticAdapter\Indices\Mapping())
->text('title', [
'boost' => 2,
])
->keyword('tag', [
'null_value' => 'NULL'
])
->geoPoint('location')
->dynamicTemplate('no_doc_values', [
'match_mapping_type' => '*',
'mapping' => [
'type' => '{dynamic_type}',
'doc_values' => false,
],
]);
$settings = (new \ElasticAdapter\Indices\Settings())
->index([
'number_of_replicas' => 2,
'refresh_interval' => -1
]);
$index = new \ElasticAdapter\Indices\IndexBlueprint('my_index', $mapping, $settings);
$indexManager->create($index);
$mapping = [
'properties' => [
'title' => [
'type' => 'text'
]
]
];
$settings = [
'number_of_replicas' => 2
];
$indexManager->createRaw('my_index', $mapping, $settings);
$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);
$mapping = [
'properties' => [
'title' => [
'type' => 'text'
]
]
];
$indexManager->putMappingRaw('my_index', $mapping);
$settings = (new \ElasticAdapter\Indices\Settings())
->analysis([
'analyzer' => [
'content' => [
'type' => 'custom',
'tokenizer' => 'whitespace'
]
]
]);
$indexManager->putSettings('my_index', $settings);
$settings = [
'number_of_replicas' => 2
];
$indexManager->putSettingsRaw('my_index', $settings);
$indexManager->exists('my_index');
$indexManager->open('my_index');
$indexManager->close('my_index');
$alias = new \ElasticAdapter\Indices\Alias('my_alias', [
'term' => [
'user_id' => 12,
],
]);
$indexManager->putAlias('my_index', $alias);
$indexManager->getAliases('my_index');
$indexManager->deleteAlias('my_index', 'my_alias');
$client = \Elasticsearch\ClientBuilder::fromConfig([
'hosts' => [
'localhost:9200'
]
]);
$documentManager = new \ElasticAdapter\Documents\DocumentManager($client);
$documents = collect([
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);
$routing = (new ElasticAdapter\Documents\Routing())
->add('1', 'value1')
->add('2', 'value2');
$documentManager->index('my_index', $documents, false, $routing);
$documentIds = ['1', '2'];
$documentManager->delete('my_index', $documentIds);
$documentManager->delete('my_index', $documentIds, true);
$routing = (new ElasticAdapter\Documents\Routing())
->add('1', 'value1')
->add('2', 'value2');
$documentManager->delete('my_index', $documentIds, false, $routing);
$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);
// create a search request
$request = new \ElasticAdapter\Search\SearchRequest([
'match' => [
'message' => 'test'
]
]);
// configure highlighting
$request->highlight([
'fields' => [
'message' => [
'type' => 'plain',
'fragment_size' => 15,
'number_of_fragments' => 3,
'fragmenter' => 'simple'
]
]
]);
// add suggestions
$request->suggest([
'message_suggest' => [
'text' => 'test',
'term' => [
'field' => 'message'
]
]
]);
// enable source filtering
$request->source(['message', 'post_date']);
// collapse fields
$request->collapse([
'field' => 'user'
]);
// aggregate data
$request->aggregations([
'max_likes' => [
'max' => [
'field' => 'likes'
]
]
]);
// sort documents
$request->sort([
['post_date' => ['order' => 'asc']],
'_score'
]);
// rescore documents
$request->rescore([
'window_size' => 50,
'query' => [
'rescore_query' => [
'match_phrase' => [
'message' => [
'query' => 'the quick brown',
'slop' => 2,
],
],
],
'query_weight' => 0.7,
'rescore_query_weight' => 1.2,
]
]);
// add a post filter
$request->postFilter([
'term' => [
'cover' => 'hard'
]
]);
// track total hits
$request->trackTotalHits(true);
// track scores
$request->trackScores(true);
// script fields
$request->scriptFields([
'my_doubled_field' => [
'script' => [
'lang' => 'painless',
'source' => 'doc[params.field] * params.multiplier',
'params' => [
'field' => 'my_field',
'multiplier' => 2,
],
],
],
]);
// boost indices
$request->indicesBoost([
['my-alias' => 1.4],
['my-index' => 1.3],
]);
// define the search type
$request->searchType('query_then_fetch');
// set the preference
$request->preference('_local');
// use pagination
$request->from(0)->size(20);
// execute the search request and get the response
$response = $documentManager->search('my_index', $request);
// get the total number of matching documents
$total = $response->total();
// get the corresponding hits
$hits = $response->hits();
// every hit provides access to the related index name, the score, the document, the highlight and the inner hits
// in addition, you can get a raw representation of the hit
foreach ($hits as $hit) {
$indexName = $hit->indexName();
$score = $hit->score();
$document = $hit->document();
$highlight = $hit->highlight();
$innerHits = $hit->innerHits();
$raw = $hit->raw();
}
// get the suggestions
$suggestions = $response->suggestions();
// get the aggregations
$aggregations = $response->aggregations();