ngocnm/elastic-query

This package is abandoned and no longer maintained. The author suggests using the ngocnm/laravel-elasticsearch package instead.

Elasticsearch query

5.0 2022-08-19 10:10 UTC

This package is auto-updated.

Last update: 2023-04-17 06:54:05 UTC


README

Elastic Query Builder For Lumen, Laravel

Install

composer require ngocnm/elastic-query

Config

  • Register Service Provider
\Ngocnm\ElasticQuery\ElasticsearchServiceProvider::class
  • Define env
ELASTIC_HOST = localhost,localhost_2 #default: localhost
ELASTIC_PORT = 9200 #default: 9200
ELASTIC_INDEX_PREFIX = project_1 #default: null
ELASTIC_USERNAME= root #default: null
ELASTIC_PASSWORD= admin #default: null
ELASTIC_SCHEME = https #default: http
ELASTIC_PATH= /data/elastic #default: null
ELASTICSEARCH_SSN_LOG_DEBUGBAR = true # add log query to debugbar  on core sosanhnha
ELASTIC_CACHE_ENABLED=true #default: true
  • Config without laravel lumen
    • Create singleton with key (elastic_query):
      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

$query_log =\Ngocnm\ElasticQuery\ElasticsearchQueryLog::getLog();

Query

  • Create Object
 $client = new Ngocnm\ElasticQuery\ElasticsearchQuery('index_name');
  • Select query
$response = $client->select('field_1,field_2')->get();
  • Limit query
$response = $client->select('field_1,field_2')->limit(3)->get();
  • Offset query
$response = $client->select('field_1,field_2')->offset($offset)->limit(3)->get();
  • Where query
$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();
  • OrderBy query(ASC,DESC)
$response = $client->select('field_1,field_2')->orderBy('field_name','asc')->get();
  • Where between query
$value = ['value_1','value_2'];
$response = $client->select('field_1,field_2')->whereBetween('field_name',$value)->get();
  • Where GeoDistance query
//$distance = '1km' default 
//column name map: location 
$response = $client->select('field_1,field_2')->whereGeoDistance($lat,$lng,$distance,'asc')->get();
  • Delete row by id
$response = $client->delete($id);
  • Delete Multi rows
$response = $client->where('field_name',$value)->deleteMulti();
  • QueryString - Fulltext search
$response = $client->queryString('field_name',$keyword)->get();
  • Full Text Search Trigrams query
$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();
  • WhereIn query
$value = [23,4,5,...];
$response = $client->whereIn('field_name',$value)->get();
  • WhereNot query
$response = $client->WhereNot('field_name',$value)->get();
//Or
$response = $client->WhereNot('field_name','>',$value)->get();
  • WhereNotIn query
$value = [23,4,5,...];
$response = $client->whereNotIn('field_name',$value)->get();
  • WhereNotBetween query
$value = ['value_1','value_2'];
$response = $client->select('field_1,field_2')->whereNotBetween('field_name',$value)->get();
  • Insert a document or multi documents
    • Insert or update a document
      $data = [
          'field_id_unique'=>1,
          'field_1'=>$value_1,
          'field_2'=>$value_2
      ];
    • Insert or update multi documents
      $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');
  • Customize functionScore
$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();

Document functionScore

  • Span near query
  $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);

Document span near

  • Update multi documents with condition (Update by query)
  $data_update = ['field'=>'new_value','field_2'=>'new_value_2'];
  $response = $client->where('field_condition','value_condition')->update($data_update);
  • Delete index
Ngocnm\ElasticQuery\ElasticsearchQuery::deleteIndex($name_index);
  • CreateIndex index by query
$query_create = [
        'index' => $index_name,
        'body' => [
            'settings' => [
                'number_of_shards' => 15,
                'number_of_replicas' => 1
            ]
        ]
    ];
    
Ngocnm\ElasticQuery\ElasticsearchQuery::createIndex($query_create);
  • CreateIndex index by options
$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);

Document mapping

  • Check index exist
Ngocnm\ElasticQuery\ElasticsearchQuery::indexExists($name_index);
  • Cache query - Only has on laravel framework(Manage cache's laravel):
//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();