Debian Codegraph @

doc/source/reference/scorer.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: scorer
5
6 Scorer
7 ======
8
9 Summary
10 -------
11
12 Groonga has scorer module that customizes score function. Score
13 function computes score of matched record. The default scorer function
14 uses the number of appeared terms. It is also known as TF (term
15 frequency).
16
17 TF is a fast score function but it's not suitable for the following
18 cases:
19
20 * Search query contains one or more frequently-appearing words such
21 as "the" and "a".
22 * Document contains many same keywords such as "They are keyword,
23 keyword, keyword ... and keyword". Search engine spammer may use
24 the technique.
25
26 Score function can solve these cases. For example, `TF-IDF
27 <https://en.wikipedia.org/wiki/Tf%E2%80%93idf>`_ (term
28 frequency-inverse document frequency) can solve the first case.
29 `Okapi BM25 <https://en.wikipedia.org/wiki/Okapi_BM25>`_ can solve the
30 second case. But their are slower than TF.
31
32 Groonga provides TF-IDF based scorer as
33 :doc:`/reference/scorers/scorer_tf_idf` but doesn't provide Okapi BM25
34 based scorer yet.
35
36 .. include:: scoring_note.rst
37
38 Usage
39 -----
40
41 This section describes how to use scorer.
42
43 Here are a schema definition and sample data to show usage.
44
45 Sample schema:
46
47 .. groonga-command
48 .. include:: ../example/reference/scorer/usage_setup_schema.log
49 .. table_create Memos TABLE_HASH_KEY ShortText
50 .. column_create Memos title COLUMN_SCALAR ShortText
51 .. column_create Memos content COLUMN_SCALAR Text
52 ..
53 .. table_create Terms TABLE_PAT_KEY ShortText \
54 .. --default_tokenizer TokenBigram \
55 .. --normalizer NormalizerAuto
56 .. column_create Terms title_index COLUMN_INDEX|WITH_POSITION Memos title
57 .. column_create Terms content_index COLUMN_INDEX|WITH_POSITION Memos content
58
59 Sample data:
60
61 .. groonga-command
62 .. include:: ../example/reference/scorer/usage_setup_data.log
63 .. load --table Memos
64 .. [
65 .. {
66 .. "_key": "memo1",
67 .. "title": "Groonga is easy",
68 .. "content": "Groonga is very easy full text search engine!"
69 .. },
70 .. {
71 .. "_key": "memo2",
72 .. "title": "Mroonga is easy",
73 .. "content": "Mroonga is more easier full text search engine!"
74 .. },
75 .. {
76 .. "_key": "memo3",
77 .. "title": "Rroonga is easy",
78 .. "content": "Ruby is very helpful."
79 .. },
80 .. {
81 .. "_key": "memo4",
82 .. "title": "Groonga is fast",
83 .. "content": "Groonga! Groonga! Groonga! Groonga is very fast!"
84 .. },
85 .. {
86 .. "_key": "memo5",
87 .. "title": "PGroonga is fast",
88 .. "content": "PGroonga is very fast!"
89 .. },
90 .. {
91 .. "_key": "memo6",
92 .. "title": "PGroonga is useful",
93 .. "content": "SQL is easy because many client libraries exist."
94 .. },
95 .. {
96 .. "_key": "memo7",
97 .. "title": "Mroonga is also useful",
98 .. "content": "MySQL has replication feature. Mroonga can use it."
99 .. }
100 .. ]
101
102 You can specify custom score function in :ref:`select-match-columns`.
103 There are some syntaxes.
104
105 For score function that doesn't require any parameter such as
106 :doc:`/reference/scorers/scorer_tf_idf`::
107
108 SCORE_FUNCTION(COLUMN)
109
110 You can specify weight::
111
112 SCORE_FUNCTION(COLUMN) * WEIGHT
113
114 For score function that requires one or more parameters such as
115 :doc:`/reference/scorers/scorer_tf_at_most`::
116
117 SCORE_FUNCTION(COLUMN, ARGUMENT1, ARGUMENT2, ...)
118
119 You can specify weight::
120
121 SCORE_FUNCTION(COLUMN, ARGUMENT1, ARGUMENT2, ...) * WEIGHT
122
123 You can use different score function for each
124 :ref:`select-match-columns`::
125
126 SCORE_FUNCTION1(COLUMN1) ||
127 SCORE_FUNCTION2(COLUMN2) * WEIGHT ||
128 SCORE_FUNCTION3(COLUMN3, ARGUMENT1) ||
129 ...
130
131 Here is a simplest example:
132
133 .. groonga-command
134 .. include:: ../example/reference/scorer/usage_one_no_argument_no_weight.log
135 .. select Memos \
136 .. --match_columns "scorer_tf_idf(content)" \
137 .. --query "Groonga" \
138 .. --output_columns "content, _score" \
139 .. --sort_keys "-_score"
140
141 ``Groonga! Groonga! Groonga! Groonga is very fast!`` contains 4
142 ``Groonga``. If you use TF based scorer that is the default scorer,
143 ``_score`` is ``4``. But the actual ``_score`` is ``2``. Because the
144 ``select`` command uses TF-IDF based scorer ``scorer_tf_idf()``.
145
146 Here is an example that uses weight:
147
148 .. groonga-command
149 .. include:: ../example/reference/scorer/usage_one_no_argument_weight.log
150 .. select Memos \
151 .. --match_columns "scorer_tf_idf(content) * 10" \
152 .. --query "Groonga" \
153 .. --output_columns "content, _score" \
154 .. --sort_keys "-_score"
155
156 ``Groonga! Groonga! Groonga! Groonga is very fast!`` has ``22`` as
157 ``_score``. It had ``2`` as ``_score`` in the previous example that
158 doesn't specify weight.
159
160 Here is an example that uses scorer that requires one
161 argument. :doc:`/reference/scorers/scorer_tf_at_most` scorer requires
162 one argument. You can limit TF score by the scorer.
163
164 .. groonga-command
165 .. include:: ../example/reference/scorer/usage_one_one_argument_no_weight.log
166 .. select Memos \
167 .. --match_columns "scorer_tf_at_most(content, 2.0)" \
168 .. --query "Groonga" \
169 .. --output_columns "content, _score" \
170 .. --sort_keys "-_score"
171
172 ``Groonga! Groonga! Groonga! Groonga is very fast!`` contains 4
173 ``Groonga``. If you use normal TF based scorer that is the default
174 scorer, ``_score`` is ``4``. But the actual ``_score`` is ``2``.
175 Because the scorer used in the ``select`` command limits the maximum
176 score value to ``2``.
177
178 Here is an example that uses multiple scorers:
179
180 .. groonga-command
181 .. include:: ../example/reference/scorer/usage_multiple_scorers.log
182 .. select Memos \
183 .. --match_columns "scorer_tf_idf(title) || scorer_tf_at_most(content, 2.0)" \
184 .. --query "Groonga" \
185 .. --output_columns "title, content, _score" \
186 .. --sort_keys "-_score"
187
188 The ``--match_columns`` uses ``scorer_tf_idf(title)`` and
189 ``scorer_tf_at_most(content, 2.0)``. ``_score`` value is sum of them.
190
191 You can use the default scorer and custom scorer in the same
192 ``--match_columns``. You can use the default scorer by just specifying
193 a match column:
194
195 .. groonga-command
196 .. include:: ../example/reference/scorer/usage_default_and_custom_scorers.log
197 .. select Memos \
198 .. --match_columns "title || scorer_tf_at_most(content, 2.0)" \
199 .. --query "Groonga" \
200 .. --output_columns "title, content, _score" \
201 .. --sort_keys "-_score"
202
203 The ``--match_columns`` uses the default scorer (TF) for ``title`` and
204 :doc:`/reference/scorers/scorer_tf_at_most` for
205 ``content``. ``_score`` value is sum of them.
206
207 Built-in scorers
208 ----------------
209
210 Here are built-in scores:
211
212 .. toctree::
213 :maxdepth: 1
214 :glob:
215
216 scorers/*