PHP code example of ngocnm / elastic-query
1. Go to this page and download the library: Download ngocnm/elastic-query 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/ */
ngocnm / elastic-query example snippets
\Ngocnm\ElasticQuery\ElasticsearchServiceProvider::class
define('ELASTICSEARCH_INDEX_PREFIX',env('ELASTIC_INDEX_PREFIX',''));
define('ELASTICSEARCH_SSN_LOG_DEBUGBAR',env('ELASTICSEARCH_SSN_LOG_DEBUGBAR',false));
$app->singleton("elastic_query",function(){
$hosts = env('ELASTIC_HOST','localhost');
$hosts_config = [
'port'=>env('ELASTIC_PORT',9200),
'scheme'=>env('ELASTIC_SCHEME','http'),
'host'=>$hosts
];
if(!empty(env('ELASTIC_PASSWORD',null))) $hosts_config['pass'] = env('ELASTIC_PASSWORD');
if(!empty(env('ELASTIC_USERNAME',null))) $hosts_config['user'] = env('ELASTIC_USERNAME');
if(!empty(env('ELASTIC_PATH',null))) $hosts_config['path'] = env('ELASTIC_PATH');
if(!empty(env('ELASTIC_SCHEME',null))) $hosts_config['scheme'] = env('ELASTIC_SCHEME');
if(strpos($hosts,',')!==false){
$hosts = explode(',',$hosts);
}
if(is_array($hosts)){
$hosts = array_map(function ($host)use($hosts_config){
$hosts_config['host'] = $host;
return $hosts_config;
},$hosts);
}else{
$hosts = [$hosts_config];
}
return ClientBuilder::create()->setHosts($hosts)->build();
});
# Get query log
$response = $client->select('field_1,field_2')->get();
$response = $client->select('field_1,field_2')->limit(3)->get();
$response = $client->select('field_1,field_2')->offset($offset)->limit(3)->get();
$response = $client->select('field_1,field_2')->where('field_name',$value)->limit(3)->get();
//or
$response = $client->select('field_1,field_2')->where('field_name','>',$value)->limit(3)->get();
$response = $client->select('field_1,field_2')->orderBy('field_name','asc')->get();
$value = ['value_1','value_2'];
$response = $client->select('field_1,field_2')->whereBetween('field_name',$value)->get();
//$distance = '1km' default
//column name map: location
$response = $client->select('field_1,field_2')->whereGeoDistance($lat,$lng,$distance,'asc')->get();
$response = $client->delete($id);
$response = $client->where('field_name',$value)->deleteMulti();
$response = $client->queryString('field_name',$keyword)->get();
$response = $client->select('field_1,field_2')->fullTextSearchTrigrams('field_name',$keyword)->get();
$column = 'field';
//or
$column = ['field_1','field_2'];
$keyword = 'keyword_search';
$config = [
"min_term_freq" => 1,
"max_query_terms" => 12
];//default:null
$response = $client->select('field_1,field_2')->moreLikeThis($column,$keyword,$config)->get();
$value = [23,4,5,...];
$response = $client->whereIn('field_name',$value)->get();
$response = $client->WhereNot('field_name',$value)->get();
//Or
$response = $client->WhereNot('field_name','>',$value)->get();
$value = [23,4,5,...];
$response = $client->whereNotIn('field_name',$value)->get();
$value = ['value_1','value_2'];
$response = $client->select('field_1,field_2')->whereNotBetween('field_name',$value)->get();
$data = [
'field_id_unique'=>1,
'field_1'=>$value_1,
'field_2'=>$value_2
];
$data = [
[
'field_id_unique'=>1,
'field_1'=>$value_1,
'field_2'=>$value_2
],
[
'field_id_unique'=>2,
'field_1'=>$value_1,
'field_2'=>$value_2
]
];
$reponse = $client->insertOrUpdate($data,'name_field_id_unique');
$keyword = "key search";
$matchs = ['filed' =>'field_name_match', 'value'=>'value_match', 'wieght' => 24];
//or
$matchs = [
['filed' =>'field_name_match', 'value'=>'value_match', 'wieght' => 24],
['filed' =>'field_name_match_1', 'value'=>'value_match_1', 'wieght' => 42]
];
$boost = 5;// default: 5;
$max_boost = 42; // default: 42;
$min_score = 23;// default: 23;
$boost_mode = 'multiply'; // default: multiply;
// multiply :scores are multiplied (default)
// sum : scores are summed
// avg : scores are averaged
// first : the first function that has a matching filter is applied
$score_mode = 'max'; // default: max;
// max : maximum score is used
// min : minimum score is used
$response = $client->queryString('field_name',$keyword)->functionScore($matchs, $boost, $max_boost,$min_score, $boost_mode, $score_mode)->get();
$field = 'field_name';
$spans_term = ['value_1','value_2'];
$slop = 1;//default: 1
$in_order = false;// default: false;
$response = $client->spanNearQuery($field, $spans_term, $slop, $in_order);
$data_update = ['field'=>'new_value','field_2'=>'new_value_2'];
$response = $client->where('field_condition','value_condition')->update($data_update);
Ngocnm\ElasticQuery\ElasticsearchQuery::deleteIndex($name_index);
$query_create = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 15,
'number_of_replicas' => 1
]
]
];
Ngocnm\ElasticQuery\ElasticsearchQuery::createIndex($query_create);
$index_name = 'index_demo';
$number_of_shards = 15; // default:15
$number_of_replicas = 1; // default:15
$mappings = [
'_source' => [
'enabled' => true
],
'properties' => [
'location' => [
'type' => 'geo_point'
]
]
]; // default:[]
Ngocnm\ElasticQuery\ElasticsearchQuery::createIndexByOptions($index_name,$number_of_shards,$number_of_replicas,$mappings);
Ngocnm\ElasticQuery\ElasticsearchQuery::indexExists($name_index);
//Cache forever
$response = $client->WhereNot('field_name',$value)->cache()->get();
//With timeout cache
$timeout = 60;//60 second
$response = $client->WhereNot('field_name',$value)->cache($timeout)->get();