Debian Codegraph @

doc/source/tutorial/match_columns.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: tutorial-match-columns
5
6 match_columns parameter
7 =======================
8
9 Full-text search against multiple columns
10 -----------------------------------------
11
12 Groonga supports full-text search against multiple columns. Let's consider blog site. Usually, blog site has a table which contains title column and content column. How do you search the blog entry which contains specified keywords in title or content?
13
14 In such a case, there are two ways to create indexes. One way is creating column index against each column. The other way is creating one column index against multiple columns. Either way, Groonga supports similar full-text search syntax.
15
16 .. _creating-column-index-against-each-column:
17
18 Creating column index against each column
19 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20
21 Here is the example which create column index against each column.
22
23 First, create ``Blog1`` table, add ``title`` column which stores title string, ``message`` column which stores content of blog entry.
24
25 Then create ``IndexBlog1`` table for column indexes, add ``index_title`` column for ``title`` column, ``index_message`` column for ``message`` column.
26
27 .. groonga-command
28 .. include:: ../example/tutorial/match_columns-1.log
29 .. table_create --name Blog1 --flags TABLE_HASH_KEY --key_type ShortText
30 .. column_create --table Blog1 --name title --flags COLUMN_SCALAR --type ShortText
31 .. column_create --table Blog1 --name message --flags COLUMN_SCALAR --type ShortText
32 .. table_create --name IndexBlog1 --flags TABLE_PAT_KEY --key_type ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
33 .. column_create --table IndexBlog1 --name index_title --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source title
34 .. column_create --table IndexBlog1 --name index_message --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source message
35 .. load --table Blog1
36 .. [
37 .. {"_key":"grn1","title":"Groonga test","message":"Groonga message"},
38 .. {"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 Groonga moritars"},
39 .. {"_key":"grn3","title":"Groonga message","message":"none"}
40 .. ]
41
42 ``match_columns`` option of ``select`` command accepts multiple columns as search target.
43 Specify query string to ``query`` option. Then you can do full-text search title and content of blog entries.
44
45 Let's try to search blog entries.
46
47 .. groonga-command
48 .. include:: ../example/tutorial/match_columns-2.log
49 .. select --table Blog1 --match_columns title||message --query groonga
50 .. select --table Blog1 --match_columns title||message --query message
51 .. select --table Blog1 --match_columns title --query message
52
53 Creating one column index against multiple columns
54 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55
56 Groonga also supports one column index against multiple columns.
57
58 The difference for previous example is only one column index exists. Thus, There is one common column index against title and message column.
59
60 Even though same column index is used, Groonga supports to search against title column only, message column only and title or message column.
61
62 .. groonga-command
63 .. include:: ../example/tutorial/match_columns-3.log
64 .. table_create --name Blog2 --flags TABLE_HASH_KEY --key_type ShortText
65 .. column_create --table Blog2 --name title --flags COLUMN_SCALAR --type ShortText
66 .. column_create --table Blog2 --name message --flags COLUMN_SCALAR --type ShortText
67 .. table_create --name IndexBlog2 --flags TABLE_PAT_KEY --key_type ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
68 .. column_create --table IndexBlog2 --name index_blog --flags COLUMN_INDEX|WITH_POSITION|WITH_SECTION --type Blog2 --source title,message
69 .. load --table Blog2
70 .. [
71 .. {"_key":"grn1","title":"Groonga test","message":"Groonga message"},
72 .. {"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 Groonga moritars"},
73 .. {"_key":"grn3","title":"Groonga message","message":"none"}
74 .. ]
75
76 Let's search same query in previous section. You can get same search results.
77
78 .. groonga-command
79 .. include:: ../example/tutorial/match_columns-4.log
80 .. select --table Blog2 --match_columns title||message --query groonga
81 .. select --table Blog2 --match_columns title||message --query message
82 .. select --table Blog2 --match_columns title --query message
83
84 .. note::
85
86 There may be a question that "which is the better solution for indexing."
87 It depends on the case.
88
89 * Indexes for each column - The update performance tends to be better than multiple column index because there is enough buffer for updating. On the other hand, the efficiency of disk usage is not so good.
90 * Indexes for multiple column - It saves disk usage because it shares common buffer. On the other hand, the update performance is not so good.
91
92
93 .. _full-text-search-with-specific-index-name:
94
95 Full text search with specific index name
96 -----------------------------------------
97
98 Groonga also supports full text search with specific index name.
99
100 In this section, you learn how to use specific index column efficiently.
101
102 Here is the concrete example about specific index name.
103
104 .. groonga-command
105 .. include:: ../example/tutorial/match_columns-specific-index-schema.log
106 .. table_create Entries TABLE_HASH_KEY ShortText
107 .. column_create Entries title COLUMN_SCALAR ShortText
108 .. column_create Entries body COLUMN_SCALAR ShortText
109 .. load --table Entries
110 .. [
111 .. {"_key": "http://example.com/entry1", "title":"Hello Groonga.", "body":"This is my first entry."},
112 .. {"_key": "http://example.com/entry2", "title":"Hello world.", "body":"I love Groonga!"},
113 .. {"_key": "http://example.com/entry3", "title":"Hello Mroonga, bye Groonga.", "body":"I use Mroonga."},
114 .. {"_key": "http://example.com/entry4", "title":"Say, Hello Groonga!", "body":"I'm back."}
115 .. ]
116 .. table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
117 .. column_create Terms entries_title COLUMN_INDEX|WITH_POSITION Entries title
118 .. column_create Terms entries_body COLUMN_INDEX|WITH_POSITION Entries body
119 .. column_create Terms entries_whole COLUMN_INDEX|WITH_POSITION|WITH_SECTION Entries title,body
120
121 The table which stores entries has columns for title and body.
122 And the terms table has index columns for title and body to entries table.
123
124 There are three index columns in terms table.
125
126 * entries_title: index column for title
127 * entries_body: index column for body
128 * entries_whole: index column for title and body
129
130 If you specify index column which is related to specific data column, related data column is searched with that index implicitly.
131
132 For example, if you want to search title or body only, specify ``Terms.entries_title`` or ``Terms.entries_body`` index column.
133
134 .. groonga-command
135 .. include:: ../example/tutorial/match_columns-specific-title-index.log
136 .. select --table Entries --output_columns title --match_columns Terms.entries_title --query "Groonga"
137
138 This example uses ``Terms.entries_title`` as index, then search "Groonga" against title data column.
139
140 .. groonga-command
141 .. include:: ../example/tutorial/match_columns-specific-body-index.log
142 .. select --table Entries --output_columns body --match_columns Terms.entries_body --query "Groonga"
143
144 This example uses ``Terms.entries_body`` as index, then search "Groonga" against body data column.
145
146 If you specify multiple index column which is related to specific data columns, you can also specify data column name as suffix. It means that "Use specific index and search specific data column explicitly".
147
148 For example, if you want to search title or body only with ``entries_whole`` index, specify ``Terms.entries_whole.title`` or ``Terms.entries_whole.body``. It uses ``Terms.entries_whole`` index and search ``title`` column or ``body`` column explicitly.
149
150 .. groonga-command
151 .. include:: ../example/tutorial/match_columns-specific-whole-with-title.log
152 .. select --table Entries --output_columns title --match_columns Terms.entries_whole.title --query "Groonga"
153
154 This example uses ``Terms.entries_whole`` as index, then search "Groonga" against title data column.
155
156 .. groonga-command
157 .. include:: ../example/tutorial/match_columns-specific-whole-with-body.log
158 .. select --table Entries --output_columns body --match_columns Terms.entries_whole.body --query "Groonga"
159
160 This example uses ``Terms.entries_whole`` as index, then search "Groonga" against body data column.
161
162 .. _nested-index-search:
163
164 Nested index search among related table by column index
165 --------------------------------------------------------
166
167 If there are relationships among multiple table with column index,
168 you can search multiple table by specifying reference column name.
169
170 Here is the concrete example.
171
172 There are tables which store blog articles, comments for articles.
173 The table which stores articles has columns for article and comment.
174 And the comment column refers Comments table.
175 The table which stores comments has columns for comment and column index to article table.
176
177 if you want to search the articles which contain specified keyword in comment,
178 you need to execute fulltext search for table of comment, then search the records which contains fulltext search results.
179
180 But, you can search the records by specifying the reference column index at once.
181
182 Here is the sample schema.
183
184 .. groonga-command
185 .. include:: ../example/tutorial/match_columns-nested-index-schema.log
186 .. table_create Comments TABLE_HASH_KEY UInt32
187 .. column_create Comments content COLUMN_SCALAR ShortText
188 .. table_create Articles TABLE_NO_KEY
189 .. column_create Articles content COLUMN_SCALAR Text
190 .. column_create Articles comment COLUMN_SCALAR Comments
191 .. table_create Lexicon TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
192 .. column_create Lexicon articles_content COLUMN_INDEX|WITH_POSITION Articles content
193 .. column_create Lexicon comments_content COLUMN_INDEX|WITH_POSITION Comments content
194 .. column_create Comments article COLUMN_INDEX Articles comment
195
196 Here is the sample data.
197
198 .. groonga-command
199 .. include:: ../example/tutorial/match_columns-nested-index-data.log
200 .. load --table Comments
201 .. [
202 .. {"_key": 1, "content": "I'm using Groonga too!"},
203 .. {"_key": 2, "content": "I'm using Groonga and Mroonga!"},
204 .. {"_key": 3, "content": "I'm using Mroonga too!"}
205 .. ]
206 .. load --table Articles
207 .. [
208 .. {"content": "Groonga is fast!", "comment": 1},
209 .. {"content": "Groonga is useful!"},
210 .. {"content": "Mroonga is fast!", "comment": 3}
211 .. ]
212
213 You can write the query that search the records which contains specified keyword as a comment, then fetch the articles which refers to it.
214
215 Query for searching the records described above::
216
217 select Articles --match_columns comment.content --query groonga --output_columns "_id, _score, *"
218
219 You need to concatenate comment column of Articles table and content column of Comments table with period( ``.`` ) as ``--match_columns`` arguments.
220
221 At first, this query execute fulltext search from content of Comments table, then fetch the records of Articles table which refers to already searched records of Comments table.
222 (Because of this, if you comment out the query which creates index column ``article`` of Comments table, you can't get intended search results.)
223
224 .. groonga-command
225 .. include:: ../example/tutorial/match_columns-nested-index-select.log
226 .. select Articles --match_columns comment.content --query groonga --output_columns "_id, _score, *"
227
228 Now, you can search articles which contains specific keywords as a comment.
229
230 The feature of nested index search is not limited to the relationship between two table only.
231
232 Here is the sample schema similar to previous one. The difference is added table which express 'Reply' and relationship is extended to three tables.
233
234 .. groonga-command
235 .. include:: ../example/tutorial/match_columns-nested-index-schema-with-three-relationship.log
236 .. table_create Replies2 TABLE_HASH_KEY UInt32
237 .. column_create Replies2 content COLUMN_SCALAR ShortText
238 .. table_create Comments2 TABLE_HASH_KEY UInt32
239 .. column_create Comments2 content COLUMN_SCALAR ShortText
240 .. column_create Comments2 comment COLUMN_SCALAR Replies2
241 .. table_create Articles2 TABLE_NO_KEY
242 .. column_create Articles2 content COLUMN_SCALAR Text
243 .. column_create Articles2 comment COLUMN_SCALAR Comments2
244 .. table_create Lexicon2 TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
245 .. column_create Lexicon2 articles_content COLUMN_INDEX|WITH_POSITION Articles2 content
246 .. column_create Lexicon2 comments_content COLUMN_INDEX|WITH_POSITION Comments2 content
247 .. column_create Lexicon2 replies_content COLUMN_INDEX|WITH_POSITION Replies2 content
248 .. column_create Comments2 article COLUMN_INDEX Articles2 comment
249 .. column_create Replies2 reply_to COLUMN_INDEX Comments2 comment
250
251 Here is the sample data.
252
253 .. groonga-command
254 .. include:: ../example/tutorial/match_columns-nested-index-data-with-three-relationship.log
255 .. load --table Replies2
256 .. [
257 .. {"_key": 1, "content": "I'm using Rroonga too!"},
258 .. {"_key": 2, "content": "I'm using Groonga and Mroonga and Rroonga!"},
259 .. {"_key": 3, "content": "I'm using Nroonga too!"}
260 .. ]
261 .. load --table Comments2
262 .. [
263 .. {"_key": 1, "content": "I'm using Groonga too!", "comment": 1},
264 .. {"_key": 2, "content": "I'm using Groonga and Mroonga!", "comment": 2},
265 .. {"_key": 3, "content": "I'm using Mroonga too!"}
266 .. ]
267 .. load --table Articles2
268 .. [
269 .. {"content": "Groonga is fast!", "comment": 1},
270 .. {"content": "Groonga is useful!", "comment": 2},
271 .. {"content": "Mroonga is fast!", "comment": 3}
272 .. ]
273
274 Query for searching the records described above::
275
276 select Articles2 --match_columns comment.content --query mroonga --output_columns "_id, _score, *"
277 select Articles2 --match_columns comment.comment.content --query mroonga --output_columns "_id, _score, *"
278
279 The first query searches ``mroonga`` from ``Comments2`` table, the second one searches ``mroonga`` from ``Replies2`` and ``Comments2`` table by using reference column index.
280
281 .. groonga-command
282 .. include:: ../example/tutorial/match_columns-nested-index-select-with-three-relationship.log
283 .. select Articles2 --match_columns comment.content --query mroonga --output_columns "_id, _score, *"
284 .. select Articles2 --match_columns comment.comment.content --query mroonga --output_columns "_id, _score, *"
285
286 As a result, the first query matches two article because of ``Comments2`` table has two records which contains ``mroonga`` as keyword.
287
288 On the other hand, the second one matches one article only because of ``Replies2`` table has only one record which contains ``mroonga`` as keyword, and there is one record which contains same keyword and refers to the record in ``Comments2`` table.
289
290 Indexes with Weight
291 -------------------
292
293 If index columns are created for data columns, you can search by indexes with weight.
294 For example, let's try to search blog entries by indexes with weight which contains ``Groonga`` as important keyword in ``Blog1`` table.
295 Generally speaking, an important keyword tend to be included in blog title, so if ``title`` column contains ``Groonga``, its score ( ``_score`` ) must be raised in contrast to ``message`` column. The indexes with weight is used for such a purpose.
296
297 Here is the example which search blog entries with ``Groonga`` as important keyword in ``title`` or ``message`` columns.
298
299 The sample schema and data is same as :ref:`creating-column-index-against-each-column`.
300
301 .. groonga-command
302 .. include:: ../example/tutorial/match_columns-indexes-with-weight.log
303 .. select --table Blog1 --match_columns 'IndexBlog1.index_title * 10 || IndexBlog1.index_message' --query 'Groonga' --output_columns "_id, _score, *"
304
305 In above query, ``'IndexBlog1.index_title * 10 || IndexBlog1.index_message'`` is specified for ``--match_columns``.
306 It means that if ``title`` column (search ``title`` column using ``IndexBlog1.index_title`` index) matches to ``Groonga``, its weight is multiplied to 10 and if ``message`` column (search ``message`` column using ``IndexBlog1.index_message`` index) matches to ``Groonga``,
307 its weight is 1 (default). If ``Groonga`` matches to ``title`` and ``message``, its weight is 11 (10 + 1) in this case.
308
309 As a result, ``Groonga test`` blog entry is listed in first.