8. Document

Add a document to ElasticSearch

 1<?php
 2
 3$elastic = new ElasticSearch();
 4
 5$index = $elastic->document()
 6                ->index('my-index')
 7                ->body([
 8                    'text' => 'What is your name',
 9                    'location' => [
10                        'type' => 'point',
11                        'coordinates' => [50.30, 20.34],
12                    ]
13                ])
14                ->save();
15
16// You can merge with suggest
17$index = $elastic->document()
18                ->index('my-index')
19                ->body([
20                    'text' => 'My name is Şêro',
21                    'location' => [
22                        'type' => 'point',
23                        'coordinates' => [50.30, 20.34],
24                    ]
25                ])
26                ->mergeWith('suggest', function(){
27                        return $this
28                                ->input([
29                                    [
30                                        'input' => 'Music Time',
31                                        'weight' => 20
32                                    ]
33                                ])
34                                ->getMap();
35                    })
36                ->save();

Find a Document

1<?php
2
3$elastic = new ElasticSearch();
4
5$find = $elastic->find('my-index', 'enter-id')->get();
6
7return $find;

Update a Document

1<?php
2
3$elastic = new ElasticSearch();
4
5$update = $elastic->find('my-index', 'Lpkpd53YBR6XiqYGx3M98')->update([
6    'name' => 'Şêro'
7]);
8
9return $update;

Delete a Document

1<?php
2
3$elastic = new ElasticSearch();
4
5$delete = $elastic->find('my-index', 'Lpkpd53YBR6XiqYGx3M98')->delete();
6
7return $delete;