Debian Codegraph @

doc/source/tutorial/micro_blog.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: tutorial-micro-blog
5
6 Let's create micro-blog
7 =======================
8
9 Let's create micro-blog with full text search by Groonga.
10 Micro-blog is one of the broadcast medium in the forms of blog. It is mainly used to post small messages like a Twitter.
11
12 Create a table
13 --------------
14
15 Let's create table.
16
17 ::
18
19 table_create --name Users --flags TABLE_HASH_KEY --key_type ShortText
20 table_create --name Comments --flags TABLE_HASH_KEY --key_type ShortText
21 table_create --name HashTags --flags TABLE_HASH_KEY --key_type ShortText
22 table_create --name Bigram --flags TABLE_PAT_KEY --key_type ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
23 table_create --name GeoIndex --flags TABLE_PAT_KEY --key_type WGS84GeoPoint
24
25 column_create --table Users --name name --flags COLUMN_SCALAR --type ShortText
26 column_create --table Users --name follower --flags COLUMN_VECTOR --type Users
27 column_create --table Users --name favorites --flags COLUMN_VECTOR --type Comments
28 column_create --table Users --name location --flags COLUMN_SCALAR --type WGS84GeoPoint
29 column_create --table Users --name location_str --flags COLUMN_SCALAR --type ShortText
30 column_create --table Users --name description --flags COLUMN_SCALAR --type ShortText
31 column_create --table Users --name followee --flags COLUMN_INDEX --type Users --source follower
32
33 column_create --table Comments --name comment --flags COLUMN_SCALAR --type ShortText
34 column_create --table Comments --name last_modified --flags COLUMN_SCALAR --type Time
35 column_create --table Comments --name replied_to --flags COLUMN_SCALAR --type Comments
36 column_create --table Comments --name replied_users --flags COLUMN_VECTOR --type Users
37 column_create --table Comments --name hash_tags --flags COLUMN_VECTOR --type HashTags
38 column_create --table Comments --name location --flags COLUMN_SCALAR --type WGS84GeoPoint
39 column_create --table Comments --name posted_by --flags COLUMN_SCALAR --type Users
40 column_create --table Comments --name favorited_by --flags COLUMN_INDEX --type Users --source favorites
41
42 column_create --table HashTags --name hash_index --flags COLUMN_INDEX --type Comments --source hash_tags
43
44 column_create --table Bigram --name users_index --flags COLUMN_INDEX|WITH_POSITION|WITH_SECTION --type Users --source name,location_str,description
45 column_create --table Bigram --name comment_index --flags COLUMN_INDEX|WITH_POSITION --type Comments --source comment
46
47 column_create --table GeoIndex --name users_location --type Users --flags COLUMN_INDEX --source location
48 column_create --table GeoIndex --name comments_location --type Comments --flags COLUMN_INDEX --source location
49
50 Users table
51 ^^^^^^^^^^^
52
53 This is the table which stores user information.
54 It stores name of user, profile, list of follower and so on.
55
56 ``_key``
57 User ID
58
59 ``name``
60 User name
61
62 ``follower``
63 List of following users
64
65 ``favorites``
66 List of favorite comments
67
68 ``location``
69 Current location of user (geolocation)
70
71 ``location_str``
72 Current location of user (string)
73
74 ``description``
75 User profile
76
77 ``followee``
78 Indexes for ``follower`` column in ``Users`` table.
79 With this indexes, you can search users who follows the person.
80
81 Comments table
82 ^^^^^^^^^^^^^^
83
84 This is the table which stores comments and its metadata.
85 It stores content of comment, posted date, comment which reply to, and so on.
86
87 ``_key``
88 Comment ID
89
90 ``comment``
91 Content of comment
92
93 ``last_modified``
94 Posted date
95
96 ``replied_to``
97 Comment which you reply to someone
98
99 ``replied_users``
100 List of users who you reply to
101
102 ``hash_tags``
103 List of hash tags about comment
104
105 ``location``
106 Posted place (for geolocation)
107
108 ``posted_by``
109 Person who write comment
110
111 ``favorited_by``
112 Indexes for ``favorites`` column in ``Users`` table.
113 With this indexes, you can search the person who mark comment as favorite one.
114
115 HashTags table
116 ^^^^^^^^^^^^^^
117
118 This is the table which stores hash tags for comments.
119
120 ``_key``
121 Hash tag
122
123 ``hash_index``
124 Indexes for ``Comments.hash_tags``.
125 With this indexes, you can search list of comments with specified hash tags.
126
127 Bigram table
128 ^^^^^^^^^^^^
129
130 This is the table which stores indexes for full text search by user information or comments.
131
132 ``_key``
133 Word
134
135 ``users_index``
136 Indexes of user information.
137 This column contains indexes of user name (``Users.name``), current location (``Users.location_str``), profile (``Users.description``).
138
139 ``comment_index``
140 Indexes about content of comments (``Comments.comment``).
141
142 GeoIndex table
143 ^^^^^^^^^^^^^^
144
145 This is the table which stores indexes of location column to search geo location effectively.
146
147 ``users_location``
148 Indexes of location column for Users table
149
150 ``comments_location``
151 Indexes of location column for Comments table
152
153 Loading data
154 ------------
155
156 Then, load example data.
157
158 ::
159
160 load --table Users
161 [
162 {
163 "_key": "alice",
164 "name": "Alice",
165 "follower": ["bob"],
166 "favorites": [],
167 "location": "152489000x-255829000",
168 "location_str": "Boston, Massachusetts",
169 "description": "Groonga developer"
170 },
171 {
172 "_key": "bob",
173 "name": "Bob",
174 "follower": ["alice","charlie"],
175 "favorites": ["alice:1","charlie:1"],
176 "location": "146249000x-266228000",
177 "location_str": "Brooklyn, New York City",
178 "description": ""
179 },
180 {
181 "_key": "charlie",
182 "name": "Charlie",
183 "follower": ["alice","bob"],
184 "favorites": ["alice:1","bob:1"],
185 "location": "146607190x-267021260",
186 "location_str": "Newark, New Jersey",
187 "description": "Hmm,Hmm"
188 }
189 ]
190
191 load --table Comments
192 [
193 {
194 "_key": "alice:1",
195 "comment": "I've created micro-blog!",
196 "last_modified": "2010/03/17 12:05:00",
197 "posted_by": "alice",
198 },
199 {
200 "_key": "bob:1",
201 "comment": "First post. test,test...",
202 "last_modified": "2010/03/17 12:00:00",
203 "posted_by": "bob",
204 },
205 {
206 "_key": "alice:2",
207 "comment": "@bob Welcome!!!",
208 "last_modified": "2010/03/17 12:05:00",
209 "replied_to": "bob:1",
210 "replied_users": ["bob"],
211 "posted_by": "alice",
212 },
213 {
214 "_key": "bob:2",
215 "comment": "@alice Thanks!",
216 "last_modified": "2010/03/17 13:00:00",
217 "replied_to": "alice:2",
218 "replied_users": ["alice"],
219 "posted_by": "bob",
220 },
221 {
222 "_key": "bob:3",
223 "comment": "I've just used 'Try-Groonga' now! #groonga",
224 "last_modified": "2010/03/17 14:00:00",
225 "hash_tags": ["groonga"],
226 "location": "146566000x-266422000",
227 "posted_by": "bob",
228 },
229 {
230 "_key": "bob:4",
231 "comment": "I'm come at city of New York for development camp! #groonga #travel",
232 "last_modified": "2010/03/17 14:05:00",
233 "hash_tags": ["groonga", "travel"],
234 "location": "146566000x-266422000",
235 "posted_by": "bob",
236 },
237 {
238 "_key": "charlie:1",
239 "comment": "@alice @bob I've tried to register!",
240 "last_modified": "2010/03/17 15:00:00",
241 "replied_users": ["alice", "bob"],
242 "location": "146607190x-267021260",
243 "posted_by": "charlie",
244 }
245 {
246 "_key": "charlie:2",
247 "comment": "I'm at the Museum of Modern Art in NY now!",
248 "last_modified": "2010/03/17 15:05:00",
249 "location": "146741340x-266319590",
250 "posted_by": "charlie",
251 }
252 ]
253
254 ``follower`` column and ``favorites`` column in ``Users`` table and ``replied_users`` column in ``Comments`` table are vector column, so specify the value as an array.
255
256 ``location`` column in ``Users`` table, ``location`` column in ``Comments`` table use GeoPoint type. This type accepts "[latitude]x[longitude]".
257
258 ``last_modified`` column in ``Comments`` table use Time type.
259
260 There are two way to specify the value.
261 First, specify epoch (seconds since Jan, 1, 1970 AM 00:00:00) directly. In this case, you can specify micro seconds as fractional part.
262 The value is converted from factional part to the time which is micro seconds based one when data is loaded.
263 The second, specify the timestamp as string in following format: "(YEAR)/(MONTH)/(DAY) (HOUR):(MINUTE):(SECOND)". In this way, the string is casted to proper micro seconds
264 when data is loaded.
265
266 .. groonga-command
267 .. table_create --name Users --flags TABLE_HASH_KEY --key_type ShortText
268 .. table_create --name Comments --flags TABLE_HASH_KEY --key_type ShortText
269 .. table_create --name HashTags --flags TABLE_HASH_KEY --key_type ShortText
270 .. table_create --name Bigram --flags TABLE_PAT_KEY --key_type ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
271 .. table_create --name GeoIndex --flags TABLE_PAT_KEY --key_type WGS84GeoPoint
272 .. column_create --table Users --name name --flags COLUMN_SCALAR --type ShortText
273 .. column_create --table Users --name follower --flags COLUMN_VECTOR --type Users
274 .. column_create --table Users --name favorites --flags COLUMN_VECTOR --type Comments
275 .. column_create --table Users --name location --flags COLUMN_SCALAR --type WGS84GeoPoint
276 .. column_create --table Users --name location_str --flags COLUMN_SCALAR --type ShortText
277 .. column_create --table Users --name description --flags COLUMN_SCALAR --type ShortText
278 .. column_create --table Users --name followee --flags COLUMN_INDEX --type Users --source follower
279 .. column_create --table Comments --name comment --flags COLUMN_SCALAR --type ShortText
280 .. column_create --table Comments --name last_modified --flags COLUMN_SCALAR --type Time
281 .. column_create --table Comments --name replied_to --flags COLUMN_SCALAR --type Comments
282 .. column_create --table Comments --name replied_users --flags COLUMN_VECTOR --type Users
283 .. column_create --table Comments --name hash_tags --flags COLUMN_VECTOR --type HashTags
284 .. column_create --table Comments --name location --flags COLUMN_SCALAR --type WGS84GeoPoint
285 .. column_create --table Comments --name posted_by --flags COLUMN_SCALAR --type Users
286 .. column_create --table Comments --name favorited_by --flags COLUMN_INDEX --type Users --source favorites
287 .. column_create --table HashTags --name hash_index --flags COLUMN_INDEX --type Comments --source hash_tags
288 .. column_create --table Bigram --name users_index --flags COLUMN_INDEX|WITH_POSITION|WITH_SECTION --type Users --source name,location_str,description
289 .. column_create --table Bigram --name comment_index --flags COLUMN_INDEX|WITH_POSITION --type Comments --source comment
290 .. column_create --table GeoIndex --name users_location --type Users --flags COLUMN_INDEX --source location
291 .. column_create --table GeoIndex --name comments_location --type Comments --flags COLUMN_INDEX --source location
292 .. load --table Users
293 .. [
294 .. {
295 .. "_key": "alice",
296 .. "name": "Alice",
297 .. "follower": ["bob"],
298 .. "favorites": [],
299 .. "location": "152489000x-255829000",
300 .. "location_str": "Boston, Massachusetts",
301 .. "description": "Groonga developer"
302 .. },
303 .. {
304 .. "_key": "bob",
305 .. "name": "Bob",
306 .. "follower": ["alice","charlie"],
307 .. "favorites": ["alice:1","charlie:1"],
308 .. "location": "146249000x-266228000",
309 .. "location_str": "Brooklyn, New York City",
310 .. "description": ""
311 .. },
312 .. {
313 .. "_key": "charlie",
314 .. "name": "Charlie",
315 .. "follower": ["alice","bob"],
316 .. "favorites": ["alice:1","bob:1"],
317 .. "location": "146607190x-267021260",
318 .. "location_str": "Newark, New Jersey",
319 .. "description": "Hmm,Hmm"
320 .. }
321 .. ]
322 .. load --table Comments
323 .. [
324 .. {
325 .. "_key": "alice:1",
326 .. "comment": "I've created micro-blog!",
327 .. "last_modified": "2010/03/17 12:05:00",
328 .. "posted_by": "alice",
329 .. },
330 .. {
331 .. "_key": "bob:1",
332 .. "comment": "First post. test,test...",
333 .. "last_modified": "2010/03/17 12:00:00",
334 .. "posted_by": "bob",
335 .. },
336 .. {
337 .. "_key": "alice:2",
338 .. "comment": "@bob Welcome!!!",
339 .. "last_modified": "2010/03/17 12:05:00",
340 .. "replied_to": "bob:1",
341 .. "replied_users": ["bob"],
342 .. "posted_by": "alice",
343 .. },
344 .. {
345 .. "_key": "bob:2",
346 .. "comment": "@alice Thanks!",
347 .. "last_modified": "2010/03/17 13:00:00",
348 .. "replied_to": "alice:2",
349 .. "replied_users": ["alice"],
350 .. "posted_by": "bob",
351 .. },
352 .. {
353 .. "_key": "bob:3",
354 .. "comment": "I've just used 'Try-Groonga' now! #groonga",
355 .. "last_modified": "2010/03/17 14:00:00",
356 .. "hash_tags": ["groonga"],
357 .. "location": "146566000x-266422000",
358 .. "posted_by": "bob",
359 .. },
360 .. {
361 .. "_key": "bob:4",
362 .. "comment": "I'm come at city of New York for development camp! #groonga #travel",
363 .. "last_modified": "2010/03/17 14:05:00",
364 .. "hash_tags": ["groonga", "travel"],
365 .. "location": "146566000x-266422000",
366 .. "posted_by": "bob",
367 .. },
368 .. {
369 .. "_key": "charlie:1",
370 .. "comment": "@alice @bob I've tried to register!",
371 .. "last_modified": "2010/03/17 15:00:00",
372 .. "replied_users": ["alice", "bob"],
373 .. "location": "146607190x-267021260",
374 .. "posted_by": "charlie",
375 .. }
376 .. {
377 .. "_key": "charlie:2",
378 .. "comment": "I'm at the Museum of Modern Art in NY now!",
379 .. "last_modified": "2010/03/17 15:05:00",
380 .. "location": "146741340x-266319590",
381 .. "posted_by": "charlie",
382 .. }
383 .. ]
384
385
386 Search
387 ------
388
389 Let's search micro-blog.
390
391 Search users by keyword
392 ^^^^^^^^^^^^^^^^^^^^^^^
393
394 In this section, we search micro-blog against multiple column by keyword.
395 See :doc:`match_columns` to search multiple column at once.
396
397 Let's search user from micro-blog's user name, location, description entries.
398
399 .. groonga-command
400 .. include:: ../example/tutorial/micro_blog_keyword.log
401 .. select --table Users --match_columns name,location_str,description --query "New York" --output_columns _key,name
402
403 By using "New York" as searching keyword for user, "Bob" who lives in "New York" is listed in search result.
404
405 Search users by geolocation data (GeoPoint)
406 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
407
408 In this section, we search users by column data which use type of GeoPoint.
409 See :doc:`search` about GeoPoint column.
410
411 Following example searches users who live in within 20km from specified location.
412
413 .. groonga-command
414 .. include:: ../example/tutorial/micro_blog_user.log
415 .. select --table Users --filter 'geo_in_circle(location,"146710080x-266315480",20000)' --output_columns _key,name
416
417 It shows that "Bob" and "Charlie" lives in within 20 km from station of "Grand Central Terminal".
418
419 Search users who follows specific user
420 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
421
422 In this section, we do reverse resolution of reference relationships which is described at :doc:`index`.
423
424 Following examples shows reverse resolution about ``follower`` column of ``Users`` table.
425
426 .. groonga-command
427 .. include:: ../example/tutorial/micro_blog_follower.log
428 .. select --table Users --query follower:@bob --output_columns _key,name
429
430 It shows that "Alice" and "Charlie" follows "Bob".
431
432 Search comments by using the value of GeoPoint type
433 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
434
435 In this section, we search comments which are written within specific location.
436
437 Then, we also use drill down which is described at :doc:`drilldown`.
438 Following example shows how to drill down against search results.
439 As a result, we get the value of count which is grouped by user, and hash tags respectively.
440
441 .. groonga-command
442 .. include:: ../example/tutorial/micro_blog_drilldown.log
443 .. select --table Comments --filter 'geo_in_circle(location,"146867000x-266280000",20000)' --output_columns posted_by.name,comment --drilldown hash_tags,posted_by
444
445 Above query searches comments which are posted within 20 km from Central Park in city of New York.
446
447 As specified range is 20 km, all comments with location are collected.
448 You know that search results contain 2 #groonga hash tags and one #travel hash tag, and bob and charlie posted 2 comments.
449
450 Search comments by keyword
451 ^^^^^^^^^^^^^^^^^^^^^^^^^^
452
453 In this section, we search comments which contains specific keyword.
454 And more, Let's calculate the value of `_score` which is described at :doc:`search`.
455
456 .. groonga-command
457 .. include:: ../example/tutorial/micro_blog_score.log
458 .. select --table Comments --query comment:@Now --output_columns comment,_score
459
460 By using 'Now' as a keyword, above query returns 2 comments. It also contains count of 'Now' as the value of `_score`.
461
462 Search comments by keyword and geolocation
463 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
464
465 In this section, we search comments by specific keyword and geolocation.
466 By using `--query` and `--filter` option, following query returns records which are matched to both conditions.
467
468 .. groonga-command
469 .. include:: ../example/tutorial/micro_blog_keyword_and_location.log
470 .. select --table Comments --query comment:@New --filter 'geo_in_circle(location,"146867000x-266280000",20000)' --output_columns posted_by.name,comment --drilldown hash_tags,posted_by
471
472 It returns 1 comment which meets both condition.
473 It also returns result of drilldown. There is 1 comment which is commented by Bob.
474
475 Search comments by hash tags
476 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
477
478 In this section, we search comments which contains specific hash tags.
479 Let's use reverse resolution of reference relationships.
480
481 .. groonga-command
482 .. include:: ../example/tutorial/micro_blog_hash_tag.log
483 .. select --table Comments --query hash_tags:@groonga --output_columns posted_by.name,comment --drilldown posted_by
484
485 Above query returns 2 comments which contains #groonga hash tag.
486 It also returns result of drilldown grouped by person who posted it. It shows that there are 2 comments. Bob commented it.
487
488 Search comments by user id
489 ^^^^^^^^^^^^^^^^^^^^^^^^^^
490
491 In this section, we search comments which are posted by specific user.
492
493 .. groonga-command
494 .. include:: ../example/tutorial/micro_blog_posted_by.log
495 .. select --table Comments --query posted_by:bob --output_columns comment --drilldown hash_tags
496
497 Above query returns 4 comments which are posted by Bob.
498 It also returns result of drilldown by hash tags. There are 2 comments which contains #groonga, and 1 comment which contains #travel as hash tag.
499
500 Search user's favorite comments
501 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
502
503 In this section, we search user's favorite comments.
504
505 .. groonga-command
506 .. include:: ../example/tutorial/micro_blog_favorite.log
507 .. select --table Users --query _key:bob --output_columns favorites.posted_by,favorites.comment
508
509 Above query returns Bob's favorite comments.
510
511 Search comments by posted time
512 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
513
514 In this section, we search comments by posted time.
515 See type of `Time` in :doc:`data`.
516
517 Let's search comments that posted time are older than specified time.
518
519 .. groonga-command
520 .. include:: ../example/tutorial/micro_blog_last_modified.log
521 .. select Comments --filter 'last_modified<=1268802000' --output_columns posted_by.name,comment,last_modified --drilldown hash_tags,posted_by
522
523 Above query returns 5 comments which are older than 2010/03/17 14:00:00.
524 It also returns result of drilldown by posted person. There are 2 comments by Alice, 3 comments by Bob.
525
526 .. TODO: 以下の機能はgroonga本体での支援が必要。
527 .. タイムライン表示: あるユーザがfollowしているユーザの発言を、時系列順の逆順で並べて10件のみ表示。
528 .. 複数の条件での検索: favoriteの数がn以上の発言で全文検索、さらにユーザでドリルダウン