F.21. Модули словарей Hunspell

Эти модули предоставляют словари Hunspell для разных языков. После установки модуля в базу командой CREATE EXTENSION в схеме public появляются объекты конфигурации и словаря текстового поиска.

Таблица F.13. Модули

ЯзыкИмя расширенияИмя словаряИмя конфигурации
Американский английскийhunspell_en_usenglish_hunspellenglish_hunspell
Нидерландскийhunspell_nl_nldutch_hunspelldutch_hunspell
Французскийhunspell_frfrench_hunspellfrench_hunspell
Русскийhunspell_ru_rurussian_hunspellrussian_hunspell

F.21.1. Примеры

Объекты текстового поиска будут созданы после установки модуля словаря. Мы можем проверить созданную конфигурацию:

SELECT * FROM ts_debug('english_hunspell', 'abilities');
   alias   |   description   |   token   |          dictionaries           |    dictionary    |  lexemes  
-----------+-----------------+-----------+---------------------------------+------------------+-----------
 asciiword | Word, all ASCII | abilities | {english_hunspell,english_stem} | english_hunspell | {ability}
(1 row)

Либо вы можете создать собственную конфигурацию текстового поиска. Например, с созданными словарями и словарём Snowball вы можете получить смешанную русско-английскую конфигурацию:

CREATE TEXT SEARCH CONFIGURATION russian_en (
  COPY = simple
);

ALTER TEXT SEARCH CONFIGURATION russian_en
  ALTER MAPPING FOR asciiword, asciihword, hword_asciipart
  WITH english_hunspell, english_stem;

ALTER TEXT SEARCH CONFIGURATION russian_en
  ALTER MAPPING FOR word, hword, hword_part
  WITH russian_hunspell, russian_stem;

Создавать смешанные словари можно только для языков с различными алфавитами. Если у двух языков похожие алфавиты, Postgres Pro не сможет определить, какой словарь нужно использовать.

Конфигурация текстового поиска, созданная модулем словаря, непосредственно готова к использованию. Например, так можно искать определённые слова в этом тексте:

SELECT to_tsvector('english_hunspell', 'The blue whale is the largest animal');
               to_tsvector               
-----------------------------------------
 'animal':7 'blue':2 'large':6 'whale':3
(1 row)

Поисковый запрос может выглядеть так:

SELECT to_tsvector('english_hunspell', 'The blue whale is the largest animal')
  @@ to_tsquery('english_hunspell', 'large & whale');
 ?column? 
----------
 t
(1 row)

С такими конфигурациями можно искать текст, применяя индексы GIN или GIST. Например, если существует таблица с индексом GIN:

CREATE TABLE table1 (t varchar);
INSERT INTO table1 VALUES ('The blue whale is the largest animal');
CREATE INDEX t_idx ON table1 USING GIN (to_tsvector('english_hunspell', "t"));

Для неё можно выполнить следующий запрос:

SELECT * FROM table1 where to_tsvector('english_hunspell', t)
  @@ to_tsquery('english_hunspell', 'blue & animal');
                  t                   
--------------------------------------
 The blue whale is the largest animal
(1 row)

F.21. Hunspell Dictionaries Modules

These modules provide Hunspell dictionaries for various languages. Upon installation of the module into database using CREATE EXTENSION command, text search dictionary and configuration objects in the public schema appear.

Table F.13. Modules

LanguageExtension nameDictionary nameConfiguration name
American Englishhunspell_en_usenglish_hunspellenglish_hunspell
Dutchhunspell_nl_nldutch_hunspelldutch_hunspell
Frenchhunspell_frfrench_hunspellfrench_hunspell
Russianhunspell_ru_rurussian_hunspellrussian_hunspell

F.21.1. Examples

Text search objects will be created after installation of a dictionary module. We can test created configuration:

SELECT * FROM ts_debug('english_hunspell', 'abilities');
   alias   |   description   |   token   |          dictionaries           |    dictionary    |  lexemes  
-----------+-----------------+-----------+---------------------------------+------------------+-----------
 asciiword | Word, all ASCII | abilities | {english_hunspell,english_stem} | english_hunspell | {ability}
(1 row)

Or you can create your own text search configuration. For example, with the created dictionaries and with the Snowball dictionary you can create mixed russian-english configuration:

CREATE TEXT SEARCH CONFIGURATION russian_en (
  COPY = simple
);

ALTER TEXT SEARCH CONFIGURATION russian_en
  ALTER MAPPING FOR asciiword, asciihword, hword_asciipart
  WITH english_hunspell, english_stem;

ALTER TEXT SEARCH CONFIGURATION russian_en
  ALTER MAPPING FOR word, hword, hword_part
  WITH russian_hunspell, russian_stem;

You can create mixed dictionaries only for languages with different alphabets. If languages have similar alphabets then Postgres Pro can not decide which dictionary should be used.

A text search configuration which is created with a dictionary module is ready to use. For example, in this text you can search some words:

SELECT to_tsvector('english_hunspell', 'The blue whale is the largest animal');
               to_tsvector               
-----------------------------------------
 'animal':7 'blue':2 'large':6 'whale':3
(1 row)

Search query might looks like this:

SELECT to_tsvector('english_hunspell', 'The blue whale is the largest animal')
  @@ to_tsquery('english_hunspell', 'large & whale');
 ?column? 
----------
 t
(1 row)

With this configurations you can search a text using GIN or GIST indexes. For example, there is a table with GIN index:

CREATE TABLE table1 (t varchar);
INSERT INTO table1 VALUES ('The blue whale is the largest animal');
CREATE INDEX t_idx ON table1 USING GIN (to_tsvector('english_hunspell', "t"));

For this table you can execute the following query:

SELECT * FROM table1 where to_tsvector('english_hunspell', t)
  @@ to_tsquery('english_hunspell', 'blue & animal');
                  t                   
--------------------------------------
 The blue whale is the largest animal
(1 row)

FAQ