# groonga - An open-source fulltext search engine and column store.

- 日本語

# 7.14.15. `prefix_rk_search`

## 7.14.15.1. Summary

`prefix_rk_search` selects records by Prefix RK search.

You need to create TABLE_PAT_KEY table for prefix RK search.

You can't use `prefix_rk_search` for sequential scan. It's a selector only procedure.

## 7.14.15.2. Syntax

`prefix_rk_search` requires two arguments. They are `column` and `query`:

    prefix_rk_search(column, query)

`column` must be `_key` for now.

`query` must be string.

## 7.14.15.3. Usage

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

Execution example:

    table_create Readings TABLE_PAT_KEY ShortText --normalizer NormalizerAuto
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    load --table Readings
    [
    {"_key": "ニホン"},
    {"_key": "ニッポン"},
    {"_key": "ローマジ"}
    ]
    # [[0, 1337566253.89858, 0.000355720520019531], 3]

Here is the simple usage of `prefix_rk_search()` function which selects `ニホン` and `ニッポン` by `ni`:

Execution example:

    select Readings --filter 'prefix_rk_search(_key, "ni")'
    # [
    #   [
    #     0,
    #     1337566253.89858,
    #     0.000355720520019531
    #   ],
    #   [
    #     [
    #       [
    #         2
    #       ],
    #       [
    #         [
    #           "_id",
    #           "UInt32"
    #         ],
    #         [
    #           "_key",
    #           "ShortText"
    #         ]
    #       ],
    #       [
    #         2,
    #         "ニッポン"
    #       ],
    #       [
    #         1,
    #         "ニホン"
    #       ]
    #     ]
    #   ]
    # ]

You can implement Completion like feature by combining sub_filter.

Create a table that has candidates of completion as records. Each records have zero or more readings. They are stored into `Readings` table. Don't forget define an index column for `Items.readings` in `Readings` table. The index column is needed for sub_filter:

Execution example:

    table_create Items TABLE_HASH_KEY ShortText
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    column_create Items readings COLUMN_VECTOR Readings
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    column_create Readings items_index COLUMN_INDEX Items readings
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    load --table Items
    [
    {"_key": "日本",     "readings": ["ニホン", "ニッポン"]},
    {"_key": "ローマ字", "readings": ["ローマジ"]},
    {"_key": "漢字",     "readings": ["カンジ"]}
    ]
    # [[0, 1337566253.89858, 0.000355720520019531], 3]

You can find `日本` record in `Items` table by `niho`. Because prefix RK search with `niho` selects `ニホン` reading and `ニホン` reading is one of readings of `日本` record:

Execution example:

    select Items \
     --filter 'sub_filter(readings, "prefix_rk_search(_key, \\"niho\\")")'
    # [
    #   [
    #     0,
    #     1337566253.89858,
    #     0.000355720520019531
    #   ],
    #   [
    #     [
    #       [
    #         1
    #       ],
    #       [
    #         [
    #           "_id",
    #           "UInt32"
    #         ],
    #         [
    #           "_key",
    #           "ShortText"
    #         ],
    #         [
    #           "readings",
    #           "Readings"
    #         ]
    #       ],
    #       [
    #         1,
    #         "日本",
    #         [
    #           "ニホン",
    #           "ニッポン"
    #         ]
    #       ]
    #     ]
    #   ]
    # ]

You need to combine Prefix search operator to support no reading completion targets.

Add one no reading completion target:

Execution example:

    load --table Items
    [
    {"_key": "nihon", "readings": []}
    ]
    # [[0, 1337566253.89858, 0.000355720520019531], 1]

Combine Prefix search operator to support no reading completion targets:

Execution example:

    select Items \
     --filter 'sub_filter(readings, "prefix_rk_search(_key, \\"niho\\")") || \
               _key @^ "niho"'
    # [
    #   [
    #     0,
    #     1337566253.89858,
    #     0.000355720520019531
    #   ],
    #   [
    #     [
    #       [
    #         2
    #       ],
    #       [
    #         [
    #           "_id",
    #           "UInt32"
    #         ],
    #         [
    #           "_key",
    #           "ShortText"
    #         ],
    #         [
    #           "readings",
    #           "Readings"
    #         ]
    #       ],
    #       [
    #         1,
    #         "日本",
    #         [
    #           "ニホン",
    #           "ニッポン"
    #         ]
    #       ],
    #       [
    #         4,
    #         "nihon",
    #         []
    #       ]
    #     ]
    #   ]
    # ]

Normally, you want to use case insensitive search for completion. Use `--normalizer NormalizerAuto` and `label` column for the case:

Execution example:

    table_create LooseItems TABLE_HASH_KEY ShortText --normalizer NormalizerAuto
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    column_create LooseItems label COLUMN_SCALAR ShortText
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    column_create LooseItems readings COLUMN_VECTOR Readings
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    column_create Readings loose_items_index COLUMN_INDEX LooseItems readings
    # [[0, 1337566253.89858, 0.000355720520019531], true]
    load --table LooseItems
    [
    {"_key": "日本",     "label": "日本",     "readings": ["ニホン", "ニッポン"]},
    {"_key": "ローマ字", "label": "ローマ字", "readings": ["ローマジ"]},
    {"_key": "漢字",     "label": "漢字",     "readings": ["カンジ"]},
    {"_key": "Nihon",    "label": "日本",     "readings": []}
    ]
    # [[0, 1337566253.89858, 0.000355720520019531], 4]

Use `LooseItems.label` for display:

Execution example:

    select LooseItems \
     --filter 'sub_filter(readings, "prefix_rk_search(_key, \\"nIhO\\")") || \
               _key @^ "nIhO"' \
     --output_columns '_key,label'
    # [
    #   [
    #     0,
    #     1337566253.89858,
    #     0.000355720520019531
    #   ],
    #   [
    #     [
    #       [
    #         2
    #       ],
    #       [
    #         [
    #           "_key",
    #           "ShortText"
    #         ],
    #         [
    #           "label",
    #           "ShortText"
    #         ]
    #       ],
    #       [
    #         "日本",
    #         "日本"
    #       ],
    #       [
    #         "nihon",
    #         "日本"
    #       ]
    #     ]
    #   ]
    # ]

## 7.14.15.4. Parameters

There are two required parameter, `column` and `query`.

### 7.14.15.4.1. `column`

Always specifies `_key` for now.

### 7.14.15.4.2. `query`

Specifies a query in romaji, katakana or hiragana as string.

## 7.14.15.5. Return value

`prefix_rk_search` function returns matched records.

## 7.14.15.6. See also

- Prefix RK search
- sub_filter