12.7. 組態範例

A text search configuration specifies all options necessary to transform a document into atsvector: the parser to use to break text into tokens, and the dictionaries to use to transform each token into a lexeme. Every call ofto_tsvectororto_tsqueryneeds a text search configuration to perform its processing. The configuration parameterdefault_text_search_configspecifies the name of the default configuration, which is the one used by text search functions if an explicit configuration parameter is omitted. It can be set inpostgresql.conf, or set for an individual session using theSETcommand.

Several predefined text search configurations are available, and you can create custom configurations easily. To facilitate management of text search objects, a set ofSQLcommands is available, and there are severalpsqlcommands that display information about text search objects (Section 12.10).

As an example we will create a configurationpg, starting by duplicating the built-inenglishconfiguration:

  1. CREATE TEXT SEARCH CONFIGURATION public.pg ( COPY = pg_catalog.english );

We will use a PostgreSQL-specific synonym list and store it in$SHAREDIR/tsearch_data/pg_dict.syn. The file contents look like:

  1. postgres pg
  2. pgsql pg
  3. postgresql pg

We define the synonym dictionary like this:

  1. CREATE TEXT SEARCH DICTIONARY pg_dict (
  2. TEMPLATE = synonym,
  3. SYNONYMS = pg_dict
  4. );

Next we register theIspelldictionaryenglish_ispell, which has its own configuration files:

  1. CREATE TEXT SEARCH DICTIONARY english_ispell (
  2. TEMPLATE = ispell,
  3. DictFile = english,
  4. AffFile = english,
  5. StopWords = english
  6. );

Now we can set up the mappings for words in configurationpg:

  1. ALTER TEXT SEARCH CONFIGURATION pg
  2. ALTER MAPPING FOR asciiword, asciihword, hword_asciipart,
  3. word, hword, hword_part
  4. WITH pg_dict, english_ispell, english_stem;

We choose not to index or search some token types that the built-in configuration does handle:

  1. ALTER TEXT SEARCH CONFIGURATION pg
  2. DROP MAPPING FOR email, url, url_path, sfloat, float;

Now we can test our configuration:

  1. SELECT * FROM ts_debug('public.pg', '
  2. PostgreSQL, the highly scalable, SQL compliant, open source object-relational
  3. database management system, is now undergoing beta testing of the next
  4. version of our software.
  5. ');

The next step is to set the session to use the new configuration, which was created in thepublicschema:

  1. =
  2. >
  3. \dF
  4. List of text search configurations
  5. Schema | Name | Description
  6. ---------+------+-------------
  7. public | pg |
  8. SET default_text_search_config = 'public.pg';
  9. SET
  10. SHOW default_text_search_config;
  11. default_text_search_config
  12. ----------------------------
  13. public.pg