File: scorer.rst

package info (click to toggle)
groonga 9.0.0-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,496 kB
  • sloc: ansic: 608,707; ruby: 35,042; xml: 23,643; cpp: 10,319; sh: 7,453; yacc: 5,968; python: 3,033; makefile: 2,609; perl: 133
file content (218 lines) | stat: -rw-r--r-- 6,420 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
.. -*- rst -*-

.. highlightlang:: none

.. groonga-command
.. database: scorer

Scorer
======

Summary
-------

Groonga has scorer module that customizes score function. Score
function computes score of matched record. The default scorer function
uses the number of appeared terms. It is also known as TF (term
frequency).

TF is a fast score function but it's not suitable for the following
cases:

  * Search query contains one or more frequently-appearing words such
    as "the" and "a".
  * Document contains many same keywords such as "They are keyword,
    keyword, keyword ... and keyword". Search engine spammer may use
    the technique.

Score function can solve these cases. For example, `TF-IDF
<https://en.wikipedia.org/wiki/Tf%E2%80%93idf>`_ (term
frequency-inverse document frequency) can solve the first case.
`Okapi BM25 <https://en.wikipedia.org/wiki/Okapi_BM25>`_ can solve the
second case. But their are slower than TF.

Groonga provides TF-IDF based scorer as
:doc:`/reference/scorers/scorer_tf_idf` but doesn't provide Okapi BM25
based scorer yet.

.. include:: scoring_note.rst

Usage
-----

This section describes how to use scorer.

Here are a schema definition and sample data to show usage.

Sample schema:

.. groonga-command
.. include:: ../example/reference/scorer/usage_setup_schema.log
.. table_create Memos TABLE_HASH_KEY ShortText
.. column_create Memos title COLUMN_SCALAR ShortText
.. column_create Memos content COLUMN_SCALAR Text
..
.. table_create Terms TABLE_PAT_KEY ShortText \
..   --default_tokenizer TokenBigram \
..   --normalizer NormalizerAuto
.. column_create Terms title_index COLUMN_INDEX|WITH_POSITION Memos title
.. column_create Terms content_index COLUMN_INDEX|WITH_POSITION Memos content

Sample data:

.. groonga-command
.. include:: ../example/reference/scorer/usage_setup_data.log
.. load --table Memos
.. [
.. {
..   "_key": "memo1",
..   "title": "Groonga is easy",
..   "content": "Groonga is very easy full text search engine!"
.. },
.. {
..   "_key": "memo2",
..   "title": "Mroonga is easy",
..   "content": "Mroonga is more easier full text search engine!"
.. },
.. {
..   "_key": "memo3",
..   "title": "Rroonga is easy",
..   "content": "Ruby is very helpful."
.. },
.. {
..   "_key": "memo4",
..   "title": "Groonga is fast",
..   "content": "Groonga! Groonga! Groonga! Groonga is very fast!"
.. },
.. {
..   "_key": "memo5",
..   "title": "PGroonga is fast",
..   "content": "PGroonga is very fast!"
.. },
.. {
..   "_key": "memo6",
..   "title": "PGroonga is useful",
..   "content": "SQL is easy because many client libraries exist."
.. },
.. {
..   "_key": "memo7",
..   "title": "Mroonga is also useful",
..   "content": "MySQL has replication feature. Mroonga can use it."
.. }
.. ]

You can specify custom score function in :ref:`select-match-columns`.
There are some syntaxes.

For score function that doesn't require any parameter such as
:doc:`/reference/scorers/scorer_tf_idf`::

  SCORE_FUNCTION(COLUMN)

You can specify weight::

  SCORE_FUNCTION(COLUMN) * WEIGHT

For score function that requires one or more parameters such as
:doc:`/reference/scorers/scorer_tf_at_most`::

  SCORE_FUNCTION(COLUMN, ARGUMENT1, ARGUMENT2, ...)

You can specify weight::

  SCORE_FUNCTION(COLUMN, ARGUMENT1, ARGUMENT2, ...) * WEIGHT

You can use different score function for each
:ref:`select-match-columns`::

  SCORE_FUNCTION1(COLUMN1) ||
    SCORE_FUNCTION2(COLUMN2) * WEIGHT ||
    SCORE_FUNCTION3(COLUMN3, ARGUMENT1) ||
    ...

Here is a simplest example:

.. groonga-command
.. include:: ../example/reference/scorer/usage_one_no_argument_no_weight.log
.. select Memos \
..   --match_columns "scorer_tf_idf(content)" \
..   --query "Groonga" \
..   --output_columns "content, _score" \
..   --sort_keys "-_score"

``Groonga! Groonga! Groonga! Groonga is very fast!`` contains 4
``Groonga``. If you use TF based scorer that is the default scorer,
``_score`` is ``4``. But the actual ``_score`` is ``2``. Because the
``select`` command uses TF-IDF based scorer ``scorer_tf_idf()``.

Here is an example that uses weight:

.. groonga-command
.. include:: ../example/reference/scorer/usage_one_no_argument_weight.log
.. select Memos \
..   --match_columns "scorer_tf_idf(content) * 10" \
..   --query "Groonga" \
..   --output_columns "content, _score" \
..   --sort_keys "-_score"

``Groonga! Groonga! Groonga! Groonga is very fast!`` has ``22`` as
``_score``. It had ``2`` as ``_score`` in the previous example that
doesn't specify weight.

Here is an example that uses scorer that requires one
argument. :doc:`/reference/scorers/scorer_tf_at_most` scorer requires
one argument. You can limit TF score by the scorer.

.. groonga-command
.. include:: ../example/reference/scorer/usage_one_one_argument_no_weight.log
.. select Memos \
..   --match_columns "scorer_tf_at_most(content, 2.0)" \
..   --query "Groonga" \
..   --output_columns "content, _score" \
..   --sort_keys "-_score"

``Groonga! Groonga! Groonga! Groonga is very fast!`` contains 4
``Groonga``. If you use normal TF based scorer that is the default
scorer, ``_score`` is ``4``. But the actual ``_score`` is ``2``.
Because the scorer used in the ``select`` command limits the maximum
score value to ``2``.

Here is an example that uses multiple scorers:

.. groonga-command
.. include:: ../example/reference/scorer/usage_multiple_scorers.log
.. select Memos \
..   --match_columns "scorer_tf_idf(title) || scorer_tf_at_most(content, 2.0)" \
..   --query "Groonga" \
..   --output_columns "title, content, _score" \
..   --sort_keys "-_score"

The ``--match_columns`` uses ``scorer_tf_idf(title)`` and
``scorer_tf_at_most(content, 2.0)``. ``_score`` value is sum of them.

You can use the default scorer and custom scorer in the same
``--match_columns``. You can use the default scorer by just specifying
a match column:

.. groonga-command
.. include:: ../example/reference/scorer/usage_default_and_custom_scorers.log
.. select Memos \
..   --match_columns "title || scorer_tf_at_most(content, 2.0)" \
..   --query "Groonga" \
..   --output_columns "title, content, _score" \
..   --sort_keys "-_score"

The ``--match_columns`` uses the default scorer (TF) for ``title`` and
:doc:`/reference/scorers/scorer_tf_at_most` for
``content``. ``_score`` value is sum of them.

Built-in scorers
----------------

Here are built-in scores:

.. toctree::
   :maxdepth: 1
   :glob:

   scorers/*