| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-include : drilldown.rst |
| 4 |
|
| 5 |
.. groonga-command |
| 6 |
.. database: tutorial |
| 7 |
|
| 8 |
Tag search and reverse resolution of reference relationships |
| 9 |
============================================================ |
| 10 |
|
| 11 |
As you know, Groonga supports to store array in column which refers other table. |
| 12 |
In fact, you can do tag search by using array data which refers other table. |
| 13 |
|
| 14 |
Tag search is very fast because Groonga use inverted index as data structure. |
| 15 |
|
| 16 |
Tag search |
| 17 |
---------- |
| 18 |
|
| 19 |
Let's consider to create a search engine for an web site to share movies. Each movie may be associated with multiple keywords which represents the content of movie. |
| 20 |
|
| 21 |
Let's create tables for movie information, then search the movies. |
| 22 |
|
| 23 |
First, create the ``Video`` table which stores movie information. the ``Video`` table has two columns. the ``title`` column stores title of the movie. the ``tags`` column stores multiple tag information in reference Tag table. |
| 24 |
|
| 25 |
Next, create the ``Tag`` table which stores tag information. the ``Tag`` table has one column. The tag string is stored as primary key, then ``index_tags`` stores indexes for tags column of Video table. |
| 26 |
|
| 27 |
.. groonga-command |
| 28 |
.. include:: ../example/tutorial/index-1.log |
| 29 |
.. table_create --name Video --flags TABLE_HASH_KEY --key_type UInt32 |
| 30 |
.. table_create --name Tag --flags TABLE_HASH_KEY --key_type ShortText |
| 31 |
.. column_create --table Video --name title --flags COLUMN_SCALAR --type ShortText |
| 32 |
.. column_create --table Video --name tags --flags COLUMN_VECTOR --type Tag |
| 33 |
.. column_create --table Tag --name index_tags --flags COLUMN_INDEX --type Video --source tags |
| 34 |
.. load --table Video |
| 35 |
.. [ |
| 36 |
.. {"_key":1,"title":"Soccer 2010","tags":["Sports","Soccer"]}, |
| 37 |
.. {"_key":2,"title":"Zenigata Kinjirou","tags":["Variety","Money"]}, |
| 38 |
.. {"_key":3,"title":"groonga Demo","tags":["IT","Server","groonga"]}, |
| 39 |
.. {"_key":4,"title":"Moero!! Ultra Baseball","tags":["Sports","Baseball"]}, |
| 40 |
.. {"_key":5,"title":"Hex Gone!","tags":["Variety","Quiz"]}, |
| 41 |
.. {"_key":6,"title":"Pikonyan 1","tags":["Animation","Pikonyan"]}, |
| 42 |
.. {"_key":7,"title":"Draw 8 Month","tags":["Animation","Raccoon"]}, |
| 43 |
.. {"_key":8,"title":"K.O.","tags":["Animation","Music"]} |
| 44 |
.. ] |
| 45 |
|
| 46 |
After creating indexed column, you can do full-text search very fast. The indexed column is also automatically updated when stored data is refreshed. |
| 47 |
|
| 48 |
List up the movies that specific keywords are given. |
| 49 |
|
| 50 |
.. groonga-command |
| 51 |
.. include:: ../example/tutorial/index-2.log |
| 52 |
.. select --table Video --query tags:@Variety --output_columns _key,title |
| 53 |
.. select --table Video --query tags:@Sports --output_columns _key,title |
| 54 |
.. select --table Video --query tags:@Animation --output_columns _key,title |
| 55 |
|
| 56 |
You can search by tags such as "Variety", "Sports" and "Animation". |
| 57 |
|
| 58 |
Reverse resolution of reference relationships |
| 59 |
--------------------------------------------- |
| 60 |
|
| 61 |
Groonga supports indexes for reverse resolution among tables. Tag search is one of concrete examples. |
| 62 |
|
| 63 |
For example, you can search friendships by reverse resolution in social networking site. |
| 64 |
|
| 65 |
Following example shows how to create ``User`` table which stores user information, ``username`` column which stores user name, ``friends`` column which stores list of user's friends in array, ``index_friends`` column as indexed column. |
| 66 |
|
| 67 |
.. groonga-command |
| 68 |
.. include:: ../example/tutorial/index-3.log |
| 69 |
.. table_create --name User --flags TABLE_HASH_KEY --key_type ShortText |
| 70 |
.. column_create --table User --name username --flags COLUMN_SCALAR --type ShortText |
| 71 |
.. column_create --table User --name friends --flags COLUMN_VECTOR --type User |
| 72 |
.. column_create --table User --name index_friends --flags COLUMN_INDEX --type User --source friends |
| 73 |
.. load --table User |
| 74 |
.. [ |
| 75 |
.. {"_key":"ken","username":"健作","friends":["taro","jiro","tomo","moritapo"]} |
| 76 |
.. {"_key":"moritapo","username":"森田","friends":["ken","tomo"]} |
| 77 |
.. {"_key":"taro","username":"ぐるんが太郎","friends":["jiro","tomo"]} |
| 78 |
.. {"_key":"jiro","username":"ぐるんが次郎","friends":["taro","tomo"]} |
| 79 |
.. {"_key":"tomo","username":"トモちゃん","friends":["ken","hana"]} |
| 80 |
.. {"_key":"hana","username":"花子","friends":["ken","taro","jiro","moritapo","tomo"]} |
| 81 |
.. ] |
| 82 |
|
| 83 |
Let's show list of users who contains specified user in friend list. |
| 84 |
|
| 85 |
.. groonga-command |
| 86 |
.. include:: ../example/tutorial/index-4.log |
| 87 |
.. select --table User --query friends:@tomo --output_columns _key,username |
| 88 |
.. select --table User --query friends:@jiro --output_columns _key,username |
| 89 |
|
| 90 |
Then drilldown the count which shows user is listed as friend. |
| 91 |
|
| 92 |
.. groonga-command |
| 93 |
.. include:: ../example/tutorial/index-5.log |
| 94 |
.. select --table User --limit 0 --drilldown friends |
| 95 |
|
| 96 |
As you can see, it shows the results which follows reverse resolution of reference relationship. |
| 97 |
|
| 98 |
Geo location search with index |
| 99 |
------------------------------ |
| 100 |
|
| 101 |
Groonga supports to add indexes to the column which stores geo location information. |
| 102 |
Groonga is very fast because it use such indexes against the column which contains geo location information to search enormous number of records. |
| 103 |
|
| 104 |
.. groonga-command |
| 105 |
.. include:: ../example/tutorial/index-6.log |
| 106 |
.. table_create --name GeoSite --flags TABLE_HASH_KEY --key_type ShortText |
| 107 |
.. column_create --table GeoSite --name location --type WGS84GeoPoint |
| 108 |
.. table_create --name GeoIndex --flags TABLE_PAT_KEY --key_type WGS84GeoPoint |
| 109 |
.. column_create --table GeoIndex --name index_point --type GeoSite --flags COLUMN_INDEX --source location |
| 110 |
.. load --table GeoSite |
| 111 |
.. [ |
| 112 |
.. {"_key":"http://example.org/","location":"128452975x503157902"}, |
| 113 |
.. {"_key":"http://example.net/","location":"128487316x502920929"} |
| 114 |
.. ] |
| 115 |
.. select --table GeoSite --filter 'geo_in_circle(location, "128515259x503187188", 5000)' --output_columns _key,location |
| 116 |
|
| 117 |
These indexes are also used when sorting the records with geo location search. |
| 118 |
|
| 119 |
.. groonga-command |
| 120 |
.. include:: ../example/tutorial/index-7.log |
| 121 |
.. select --table GeoSite --filter 'geo_in_circle(location, "128515259x503187188", 50000)' --output_columns _key,location,_score --sort_keys '-geo_distance(location, "128515259x503187188")' --scorer '_score = geo_distance(location, "128515259x503187188")' |