Skip to content

Commit 58259c9

Browse files
committed
add update script && increment && decrement && add search boost
1 parent 6e257ce commit 58259c9

File tree

2 files changed

+214
-38
lines changed

2 files changed

+214
-38
lines changed

readme.md

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ Index and type names setted in query will override values the configuration file
4242

4343
#### Available methods:
4444

45+
##### Getting document by id
46+
47+
$documents = ES::_id(3)->get();
4548

4649
##### Sorting
4750

@@ -79,11 +82,29 @@ Index and type names setted in query will override values the configuration file
7982
##### Where not clause
8083

8184
ES::whereNot("id", 150)->get(); or ES::where("id", "=", 150)->get();
85+
86+
##### Where greater than
87+
8288
ES::whereNot("id", ">", 150)->get();
89+
90+
##### Where greater than or equal
91+
8392
ES::whereNot("id", ">=", 150)->get();
93+
94+
##### Where less than
95+
8496
ES::whereNot("id", "<", 150)->get();
97+
98+
##### Where less than or equal
99+
85100
ES::whereNot("id", "<=", 150)->get();
101+
102+
##### Where like
103+
86104
ES::whereNot("title", "like", "foo")->get();
105+
106+
##### Where field exists
107+
87108
ES::whereNot("hobbies", "exists", true)->get(); or ES::whereExists("hobbies", true)->get();
88109

89110
##### Where not in clause
@@ -99,8 +120,12 @@ Index and type names setted in query will override values the configuration file
99120
100121
##### Search the entire document
101122

123+
102124
ES::search("bar")->get();
103125

126+
# search with Boost = 2
127+
128+
ES::search("bar", 2)->get();
104129

105130
>
106131
@@ -120,7 +145,7 @@ Index and type names setted in query will override values the configuration file
120145
121146
$documents = ES::search("bar")->paginate(5);
122147

123-
// getting pagination links
148+
# getting pagination links
124149

125150
$documents->links();
126151

@@ -133,26 +158,26 @@ Index and type names setted in query will override values the configuration file
133158
"index" => "my_index",
134159
"type" => "my_type",
135160
"body" => [
136-
"query": [
137-
"bool": [
138-
"must": [
139-
[ "match": [ "address": "mill" ] ],
140-
[ "match": [ "address": "lane" ] ]
141-
]
142-
]
143-
]
144-
]
145-
]);
161+
"query": [
162+
"bool": [
163+
"must": [
164+
[ "match": [ "address": "mill" ] ],
165+
[ "match": [ "address": "lane" ] ]
166+
]
167+
]
168+
]
169+
]
170+
]);
146171

147172

148173
>
149174
150175
##### Insert a new document
151176

152-
ES::insert([
177+
ES::_id(3)->insert([
153178
"title" => "Test document",
154-
"content" => "sample content"
155-
], 3);
179+
"content" => "Sample content"
180+
]);
156181

157182

158183
A new document will be inserted with _id = 3.
@@ -167,11 +192,11 @@ Index and type names setted in query will override values the configuration file
167192
ES::bulk(
168193
10 => [
169194
"title" => "Test document 1",
170-
"content" => "sample content 1"
195+
"content" => "Sample content 1"
171196
],
172197
11 => [
173198
"title" => "Test document 2",
174-
"content" => "sample content 2"
199+
"content" => "Sample content 2"
175200
],
176201
);
177202
@@ -181,21 +206,74 @@ Index and type names setted in query will override values the configuration file
181206
182207
##### Update an existing document
183208
184-
ES::update([
209+
ES::_id(3)->update([
185210
"title" => "Test document",
186211
"content" => "sample content"
187-
], 3);
188-
212+
]);
189213
190214
Document has _id = 3 will be updated.
191215

192216
[id is required]
193217

194218
>
195219
220+
##### Incrementing field
221+
222+
ES::_id(3)->increment("views");
223+
224+
Document has _id = 3 will be incremented by 1.
225+
226+
ES::_id(3)->increment("views", 3);
227+
228+
Document has _id = 3 will be incremented by 3.
229+
230+
[id is required]
231+
232+
>
233+
234+
##### Decrementing field
235+
236+
ES::_id(3)->decrement("views");
237+
238+
Document has _id = 3 will be decremented by 1.
239+
240+
ES::_id(3)->decrement("views", 3);
241+
242+
Document has _id = 3 will be decremented by 3.
243+
244+
[id is required]
245+
246+
>
247+
248+
##### Update using script
249+
250+
251+
# icrement field by script
252+
253+
ES::_id(3)->script(
254+
"ctx._source.$field -= params.count",
255+
["count" => 1]
256+
);
257+
258+
# add php tag to tags array list
259+
260+
ES::_id(3)->script(
261+
"ctx._source.tags.add(params.tag)",
262+
["tag" => "php"]
263+
);
264+
265+
# delete the doc if the tags field contain mongodb, otherwise it does nothing (noop)
266+
267+
ES::_id(3)->script(
268+
"if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
269+
["tag" => "mongodb"]
270+
);
271+
272+
>
273+
196274
##### Delete a document
197275
198-
ES::delete(3);
276+
ES::_id(3)->delete();
199277
200278
Document has _id = 3 will be deleted.
201279

0 commit comments

Comments
 (0)