| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: tutorial-query-expansion |
| 5 |
|
| 6 |
Query expansion |
| 7 |
=============== |
| 8 |
|
| 9 |
Groonga accepts ``query_expander`` parameter for :doc:`/reference/commands/select` command. |
| 10 |
It enables you to extend your query string. |
| 11 |
|
| 12 |
For example, if user searches "theatre" instead of "theater", |
| 13 |
query expansion enables to return search results of "theatre OR theater". |
| 14 |
This kind of way reduces search leakages. This is what really user wants. |
| 15 |
|
| 16 |
Preparation |
| 17 |
----------- |
| 18 |
|
| 19 |
To use query expansion, you need to create table which stores documents, synonym table which stores query string and replacement string. |
| 20 |
In synonym table, primary key represents original string, the column of ShortText represents modified string. |
| 21 |
|
| 22 |
.. TODO: 文字列型のベクターカラムでも可能であり、その場合は各要素をORでつなげたものに置換されるということを記述する。 |
| 23 |
|
| 24 |
Let's create document table and synonym table. |
| 25 |
|
| 26 |
.. groonga-command |
| 27 |
.. include:: ../example/tutorial/query_expansion-1.log |
| 28 |
.. table_create Doc TABLE_PAT_KEY ShortText |
| 29 |
.. column_create Doc body COLUMN_SCALAR ShortText |
| 30 |
.. table_create Term TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto |
| 31 |
.. column_create Term Doc_body COLUMN_INDEX|WITH_POSITION Doc body |
| 32 |
.. table_create Synonym TABLE_PAT_KEY ShortText |
| 33 |
.. column_create Synonym body COLUMN_VECTOR ShortText |
| 34 |
.. load --table Doc |
| 35 |
.. [ |
| 36 |
.. {"_key": "001", "body": "Play all night in this theater."}, |
| 37 |
.. {"_key": "002", "body": "theatre is British spelling."}, |
| 38 |
.. ] |
| 39 |
.. load --table Synonym |
| 40 |
.. [ |
| 41 |
.. {"_key": "theater", "body": ["theater", "theatre"]}, |
| 42 |
.. {"_key": "theatre", "body": ["theater", "theatre"]}, |
| 43 |
.. ] |
| 44 |
|
| 45 |
In this case, it doesn't occur search leakage because it creates synonym table which accepts "theatre" and "theater" as query string. |
| 46 |
|
| 47 |
Search |
| 48 |
------ |
| 49 |
|
| 50 |
Then, let's use prepared synonym table. |
| 51 |
First, use select command without ``query_expander`` parameter. |
| 52 |
|
| 53 |
.. groonga-command |
| 54 |
.. include:: ../example/tutorial/query_expansion-2.log |
| 55 |
.. select Doc --match_columns body --query "theater" |
| 56 |
.. select Doc --match_columns body --query "theatre" |
| 57 |
|
| 58 |
Above query returns the record which completely equal to query string. |
| 59 |
|
| 60 |
Then, use ``query_expander`` parameter against ``body`` column of ``Synonym`` table. |
| 61 |
|
| 62 |
.. groonga-command |
| 63 |
.. include:: ../example/tutorial/query_expansion-3.log |
| 64 |
.. select Doc --match_columns body --query "theater" --query_expander Synonym.body |
| 65 |
.. select Doc --match_columns body --query "theatre" --query_expander Synonym.body |
| 66 |
|
| 67 |
In which cases, query string is replaced to "(theater OR theatre)", thus synonym is considered for full text search. |