4.2 Document - Osushi/ElasticsearchOrm GitHub Wiki

Document

Insert a new document

[model]
$model->field = 'test';
$model->save(); # Attach automatic ids
# or
$model->_id = 'id'; # optional
$model->field = 'test';
$model->save();
# or
$model->fill(['_id' => 'id', 'field' => 'test']);
$model->save();

# Insert with refresh
$model->field = 'test';
$model->save('wait_for'); # true, 'wait_for', false
    
[client]
$client->index('index')->type('type')->insert(['field' => 'test'], 'id');
# or
$client->index('index')->type('type')->insert(['field' => 'test']); # Attach automatic ids

# Insert with refresh
$client->index('index')->type('type')->refresh('wait_for')->insert(['field' => 'test']); # true, 'wait_for', false

Update a document

[model]
$model->_id = 'id';
$model->field = 'updated';
$model->update();

# Delete with refresh
$model->_id = 'id';
$model->field = 'updated';
$model->update('wait_for'); # true, 'wait_for', false

[client]
$client->index('index')->type('type')->update(['field' => 'updated'], 'id');

# Delete with refresh
$client->index('index')->type('type')->refresh('wait_for')->update(['field' => 'updated'], 'id'); # true, 'wait_for', false

Delete a document

[model]
$model->_id = 'id';
$model->delete();

# Delete with refresh
$model->_id = 'id';
$model->delete('wait_for'); # true, 'wait_for', false

[client]
$client->index('index')->type('type')->delete('id');

# Delete with refresh
$client->index('index')->type('type')->refresh('wait_for')->delete('id'); # true, 'wait_for', false

Delete documents by queries

[model]
$model->bulk([
    ['field1' => 'test', 'field2' => 1],
    ['field1' => 'test', 'field2' => 2],
    ['field1' => 'test', 'field2' => 3],
    ['field1' => 'test', 'field2' => 4],
    ['field1' => 'test', 'field2' => 5],
]);
$model->where('field2', '>', 1)->delete();
/*
Results: 
$model->get()->toArray();
-> ['field1' => 'test', 'field2' => 1]
*/

# Delete with refresh
$model->where('field2', '>', 1)->delete('wait_for'); # true, 'wait_for', false

[client]
$client->index('index')->type('type')->where('field2', '>', 1)->delete();

# Delete with refresh
$client->index('index')->type('type')->refresh('wait_for')->where('field2', '>', 1)->delete(); # true, 'wait_for', false

Bulk options

[model]
$model->bulk([
    ['field' => 'test1', '_id' => 'id'],
    ['field' => 'test2'], # Attach automatic ids
]);
# or
$model->bulk(function ($bulk) {

    $bulk->id('id')->index('new_index')->type('new_type')->insert(['field' => 'dummy']); # Insert
    $bulk->insert(['field' => 'dummy']); # Attach automatic ids
    $bulk->id('id')->index('new_index')->type('new_type')->update(['field' => 'new_dummy']); # Update
    $bulk->id('id')->index('new_index')->type('new_type')->delete(); # Delete
    
});

# Bulk options with refresh
$model->refresh('wait_for')->bulk([
    ['field' => 'test1', '_id' => 'id'],
    ['field' => 'test2'], # Attach automatic ids
]); # true, 'wait_for', false

[client]
$client->index('index')->type('type')->bulk([
    ['field' => 'test1', '_id' => 'id'],
    ['field' => 'test2'], # Attach automatic ids
]);
# or
$client->index('index')->type('type')->bulk(function ($bulk) {

    $bulk->id('id')->index('new_index')->type('new_type')->insert(['field' => 'dummy']); # Insert
    $bulk->insert(['field' => 'dummy']); # Attach automatic ids
    $bulk->id('id')->index('new_index')->type('new_type')->update(['field' => 'new_dummy']); # Update
    $bulk->id('id')->index('new_index')->type('new_type')->delete(); # Delete

});

# Bulk insert with refresh
builder->index('index1')->type('type')->refresh('wait_for')->bulk([
  ['field' => 'dummy1'],
  ['field' => 'dummy2'],
]); # true, 'wait_for', false

Get documents

[model]
$model->get();

[client]
$client->index('index')->type('type')->get();

Select only fields

[model]
$model->select('field')->get();

[client]
$client->index('index')->type('type')->select('field')->get();

Where clause

[model]
$model->where('field', '=', 'test1')->get(); # Type of filter
$model->orWhere('field', '=', 'test1')->get(); # Type of should
$model->notWhere('field', '=', 'test1')->get(); # Type of must_not
$model->match('field', '=', 'test1')->get(); # Type of must

# Nested query
$model->where('{path}', 'nested', function($query){
  $query->mode('{mode}')->where('{field}', '{operator}', '{value}'); # Supporting operators are '=', '!=', '>', '>=', '<', '<=', 'like'
})->get();
e.g. $model->where('comments', 'nested', function ($nested) {
  $nested->where('comments.age', '=', 28);
  # also you can use 
  # where, orWhere, notWhere, match, 
  # whereBetween, orWhereBetween, notWhereBetween, matchBetween,
  # whereIn, orWhereIn, notWhereIn, matchIn
  # queryOption
})->get();

[client]
$client->index('index')->type('type')->where('field', '=', 'test1')->get();
$client->index('index')->type('type')->orWhere('field', '=', 'test1')->get();
$client->index('index')->type('type')->notWhere('field', '=', 'test1')->get();
$client->index('index')->type('type')->match('field', '=', 'test1')->get();

Here are supported operators:

[
  '=', '!=', '>', '>=', '<', '<=',
  'like', 'nested'
]
# Default operator is '='.
# e.g. where('field', 'value');

Query options:

[
  'minimum_should_match'
]
# e.g. 
# $model->queryOption('minimum_should_match', 1)->orWhere('field', '=', 'test1')->get();

Where in clause

[model]
$model->whereIn('field', ['test1'])->get(); # Type of filter
$model->orWhereIn('field', ['test1'])->get(); # Type of should
$model->notWhereIn('field', ['test1'])->get(); # Type of must_not
$model->matchIn('field', ['test1'])->get(); # Type of must

[client]
$client->index('index')->type('type')->whereIn('field', ['test1'])->get();
$client->index('index')->type('type')->orWhereIn('field', ['test1'])->get();
$client->index('index')->type('type')->notWhereIn('field', ['test1'])->get();
$client->index('index')->type('type')->matchIn('field', ['test1'])->get();

Where between

[model]
$model->whereBetween('field', ['test1', 'test2'])->get(); # Type of filter
$model->orWhereBetween('field', ['test1', 'test2'])->get(); # Type of should
$model->notWhereBetween('field', ['test1', 'test2'])->get(); # Type of must_not
$model->matchBetween('field', ['test1', 'test2'])->get(); # Type of must

Note: 
$model->whereBetween('field', ['test1', 'test2'])->get();
->
'filter' => [
  [
    'range' => [
      'field' => ['gt' => 'test1', 'lt' => 'test2'],
    ],
  ],
]

# The gte or lte options is available.
$model->whereBetween('field', ['test1', 'test2'], [true, false])->get();
->
'filter' => [
  [
    'range' => [
      'field' => ['gte' => 'test1', 'lt' => 'test2'],
    ],
  ],
]

[client]
$client->index('index')->type('type')->whereBetween('field', ['test1', 'test2'])->get();
$client->index('index')->type('type')->orWhereBetween('field', ['test1', 'test2'])->get();
$client->index('index')->type('type')->notWhereBetween('field', ['test1', 'test2'])->get();
$client->index('index')->type('type')->matchBetween('field', ['test1', 'test2'])->get();

Sorting

[model]
$model->orderBy('created_at', 'desc')->get();

# Use mode
$model->orderBy('created_at', 'desc', '{mode}')->get();
e.g. $model->orderBy('created_at', 'desc', 'min')->get(); # Supporting mode are 'min', 'max', 'sum', 'avg', 'median'

# Nested sorting
$model->orderByNest('{field}', '{order}', '{mode}', function ($nested) {
  $nested->path('{path}')->where('{field}', '{operator}', '{value}');
})->get()
e.g. $model->orderByNest('comments.star', 'desc', 'min', function ($nested) {
  $nested->path('comments')->where('comments.age', '>=', 1); # Supporting operators are '=', '!=', '>', '>=', '<', '<=', 'like'
})->get();

[client]
$client->index('index')->type('type')->->orderBy('created_at')->get(); # default order is `asc`

Limit and offset

[model]
$model->take(1)->skip(1)->get();

[client]
$client->index('index')->type('type')->take(1)->skip(1)->get();

Count

[model]
$model->count();

[client]
$client->index('index')->type('type')->count();

Distinct queries

$model->bulk([
    ['field1' => 'test', 'field2' => 1],
    ['field1' => 'test', 'field2' => 2],
    ['field1' => 'test', 'field2' => 3],
    ['field1' => 'test', 'field2' => 4],
    ['field1' => 'test', 'field2' => 5],
]);

[model]
$res = $model->collapse('field1.keyword', function ($innerHits) {
    $innerHits->name('name')->take(10)->skip(0)->orderBy('field2')->add();
})->get();

foreach ($res as $v) {
    dd($v->getInnerHit('name')->toArray());
    /*
    array:5 [▼
      0 => array:2 [▼
        "field1" => "test"
        "field2" => 1
      ]
      1 => array:2 [▼
        "field1" => "test"
        "field2" => 2
       ]
      2 => array:2 [▼
        "field1" => "test"
        "field2" => 3
      ]
      3 => array:2 [▼
        "field1" => "test"
        "field2" => 4
      ]
      4 => array:2 [▼
        "field1" => "test"
        "field2" => 5
      ]
    ]  
    */
    dd($v->getInnerHits());
    /*
    array:1 [▼
      "name" => Collection {#208 ▶}
    ]
    */
    dd($v->getFields());
    /*
    array:1 [▼
      "field1.keyword" => array:1 [▼
        0 => "test"
      ]
    ]
    */
}

[client]
$client->collapse('field1.keyword', function ($innerHits) {
  $innerHits->name('name')->take(10)->skip(0)->orderBy('field2')->select('field1')->add();
})->get();

Scroll queries

[model]
$res = $model->scroll('1m')->take(1)->get();
/*
dd($res);
Collection {#206 ▼
  #items: array:3 [▶]
  +"total": 3
  +"max_score": 1.0
  +"took": 2
  +"timed_out": false
  +"scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQ.... ▶"
  +"shards": {#210 ▶}
}
*/

# Run scrolling
$scrollId = $res->scroll_id;
$model->scroll('1m')->scrollId($scrollId)->get();

# Clear scrollId
$model->scrollId($scrollId)->clearScroll();

[client]
$res = $client->index('index')->type('type')->scroll('1m')->take(1)->get();
// ...