gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

253 行
18KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>12.8. Testing and Debugging Text Search</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="textsearch-configuration.html" title="12.7. Configuration Example" /><link rel="next" href="textsearch-indexes.html" title="12.9. GIN and GiST Index Types" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">12.8. Testing and Debugging Text Search</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="textsearch-configuration.html" title="12.7. Configuration Example">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="textsearch.html" title="Chapter 12. Full Text Search">Up</a></td><th width="60%" align="center">Chapter 12. Full Text Search</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="textsearch-indexes.html" title="12.9. GIN and GiST Index Types">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TEXTSEARCH-DEBUGGING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">12.8. Testing and Debugging Text Search</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="textsearch-debugging.html#TEXTSEARCH-CONFIGURATION-TESTING">12.8.1. Configuration Testing</a></span></dt><dt><span class="sect2"><a href="textsearch-debugging.html#TEXTSEARCH-PARSER-TESTING">12.8.2. Parser Testing</a></span></dt><dt><span class="sect2"><a href="textsearch-debugging.html#TEXTSEARCH-DICTIONARY-TESTING">12.8.3. Dictionary Testing</a></span></dt></dl></div><p>
  3. The behavior of a custom text search configuration can easily become
  4. confusing. The functions described
  5. in this section are useful for testing text search objects. You can
  6. test a complete configuration, or test parsers and dictionaries separately.
  7. </p><div class="sect2" id="TEXTSEARCH-CONFIGURATION-TESTING"><div class="titlepage"><div><div><h3 class="title">12.8.1. Configuration Testing</h3></div></div></div><p>
  8. The function <code class="function">ts_debug</code> allows easy testing of a
  9. text search configuration.
  10. </p><a id="id-1.5.11.11.3.3" class="indexterm"></a><pre class="synopsis">
  11. ts_debug([<span class="optional"> <em class="replaceable"><code>config</code></em> <code class="type">regconfig</code>, </span>] <em class="replaceable"><code>document</code></em> <code class="type">text</code>,
  12. OUT <em class="replaceable"><code>alias</code></em> <code class="type">text</code>,
  13. OUT <em class="replaceable"><code>description</code></em> <code class="type">text</code>,
  14. OUT <em class="replaceable"><code>token</code></em> <code class="type">text</code>,
  15. OUT <em class="replaceable"><code>dictionaries</code></em> <code class="type">regdictionary[]</code>,
  16. OUT <em class="replaceable"><code>dictionary</code></em> <code class="type">regdictionary</code>,
  17. OUT <em class="replaceable"><code>lexemes</code></em> <code class="type">text[]</code>)
  18. returns setof record
  19. </pre><p>
  20. <code class="function">ts_debug</code> displays information about every token of
  21. <em class="replaceable"><code>document</code></em> as produced by the
  22. parser and processed by the configured dictionaries. It uses the
  23. configuration specified by <em class="replaceable"><code>config</code></em>,
  24. or <code class="varname">default_text_search_config</code> if that argument is
  25. omitted.
  26. </p><p>
  27. <code class="function">ts_debug</code> returns one row for each token identified in the text
  28. by the parser. The columns returned are
  29. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: bullet; "><li class="listitem" style="list-style-type: disc"><p>
  30. <em class="replaceable"><code>alias</code></em> <code class="type">text</code> — short name of the token type
  31. </p></li><li class="listitem" style="list-style-type: disc"><p>
  32. <em class="replaceable"><code>description</code></em> <code class="type">text</code> — description of the
  33. token type
  34. </p></li><li class="listitem" style="list-style-type: disc"><p>
  35. <em class="replaceable"><code>token</code></em> <code class="type">text</code> — text of the token
  36. </p></li><li class="listitem" style="list-style-type: disc"><p>
  37. <em class="replaceable"><code>dictionaries</code></em> <code class="type">regdictionary[]</code> — the
  38. dictionaries selected by the configuration for this token type
  39. </p></li><li class="listitem" style="list-style-type: disc"><p>
  40. <em class="replaceable"><code>dictionary</code></em> <code class="type">regdictionary</code> — the dictionary
  41. that recognized the token, or <code class="literal">NULL</code> if none did
  42. </p></li><li class="listitem" style="list-style-type: disc"><p>
  43. <em class="replaceable"><code>lexemes</code></em> <code class="type">text[]</code> — the lexeme(s) produced
  44. by the dictionary that recognized the token, or <code class="literal">NULL</code> if
  45. none did; an empty array (<code class="literal">{}</code>) means it was recognized as a
  46. stop word
  47. </p></li></ul></div><p>
  48. </p><p>
  49. Here is a simple example:
  50. </p><pre class="screen">
  51. SELECT * FROM ts_debug('english','a fat cat sat on a mat - it ate a fat rats');
  52. alias | description | token | dictionaries | dictionary | lexemes
  53. -----------+-----------------+-------+----------------+--------------+---------
  54. asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
  55. blank | Space symbols | | {} | |
  56. asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat}
  57. blank | Space symbols | | {} | |
  58. asciiword | Word, all ASCII | cat | {english_stem} | english_stem | {cat}
  59. blank | Space symbols | | {} | |
  60. asciiword | Word, all ASCII | sat | {english_stem} | english_stem | {sat}
  61. blank | Space symbols | | {} | |
  62. asciiword | Word, all ASCII | on | {english_stem} | english_stem | {}
  63. blank | Space symbols | | {} | |
  64. asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
  65. blank | Space symbols | | {} | |
  66. asciiword | Word, all ASCII | mat | {english_stem} | english_stem | {mat}
  67. blank | Space symbols | | {} | |
  68. blank | Space symbols | - | {} | |
  69. asciiword | Word, all ASCII | it | {english_stem} | english_stem | {}
  70. blank | Space symbols | | {} | |
  71. asciiword | Word, all ASCII | ate | {english_stem} | english_stem | {ate}
  72. blank | Space symbols | | {} | |
  73. asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
  74. blank | Space symbols | | {} | |
  75. asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat}
  76. blank | Space symbols | | {} | |
  77. asciiword | Word, all ASCII | rats | {english_stem} | english_stem | {rat}
  78. </pre><p>
  79. </p><p>
  80. For a more extensive demonstration, we
  81. first create a <code class="literal">public.english</code> configuration and
  82. Ispell dictionary for the English language:
  83. </p><pre class="programlisting">
  84. CREATE TEXT SEARCH CONFIGURATION public.english ( COPY = pg_catalog.english );
  85. CREATE TEXT SEARCH DICTIONARY english_ispell (
  86. TEMPLATE = ispell,
  87. DictFile = english,
  88. AffFile = english,
  89. StopWords = english
  90. );
  91. ALTER TEXT SEARCH CONFIGURATION public.english
  92. ALTER MAPPING FOR asciiword WITH english_ispell, english_stem;
  93. </pre><pre class="screen">
  94. SELECT * FROM ts_debug('public.english','The Brightest supernovaes');
  95. alias | description | token | dictionaries | dictionary | lexemes
  96. -----------+-----------------+-------------+-------------------------------+----------------+-------------
  97. asciiword | Word, all ASCII | The | {english_ispell,english_stem} | english_ispell | {}
  98. blank | Space symbols | | {} | |
  99. asciiword | Word, all ASCII | Brightest | {english_ispell,english_stem} | english_ispell | {bright}
  100. blank | Space symbols | | {} | |
  101. asciiword | Word, all ASCII | supernovaes | {english_ispell,english_stem} | english_stem | {supernova}
  102. </pre><p>
  103. In this example, the word <code class="literal">Brightest</code> was recognized by the
  104. parser as an <code class="literal">ASCII word</code> (alias <code class="literal">asciiword</code>).
  105. For this token type the dictionary list is
  106. <code class="literal">english_ispell</code> and
  107. <code class="literal">english_stem</code>. The word was recognized by
  108. <code class="literal">english_ispell</code>, which reduced it to the noun
  109. <code class="literal">bright</code>. The word <code class="literal">supernovaes</code> is
  110. unknown to the <code class="literal">english_ispell</code> dictionary so it
  111. was passed to the next dictionary, and, fortunately, was recognized (in
  112. fact, <code class="literal">english_stem</code> is a Snowball dictionary which
  113. recognizes everything; that is why it was placed at the end of the
  114. dictionary list).
  115. </p><p>
  116. The word <code class="literal">The</code> was recognized by the
  117. <code class="literal">english_ispell</code> dictionary as a stop word (<a class="xref" href="textsearch-dictionaries.html#TEXTSEARCH-STOPWORDS" title="12.6.1. Stop Words">Section 12.6.1</a>) and will not be indexed.
  118. The spaces are discarded too, since the configuration provides no
  119. dictionaries at all for them.
  120. </p><p>
  121. You can reduce the width of the output by explicitly specifying which columns
  122. you want to see:
  123. </p><pre class="screen">
  124. SELECT alias, token, dictionary, lexemes
  125. FROM ts_debug('public.english','The Brightest supernovaes');
  126. alias | token | dictionary | lexemes
  127. -----------+-------------+----------------+-------------
  128. asciiword | The | english_ispell | {}
  129. blank | | |
  130. asciiword | Brightest | english_ispell | {bright}
  131. blank | | |
  132. asciiword | supernovaes | english_stem | {supernova}
  133. </pre><p>
  134. </p></div><div class="sect2" id="TEXTSEARCH-PARSER-TESTING"><div class="titlepage"><div><div><h3 class="title">12.8.2. Parser Testing</h3></div></div></div><p>
  135. The following functions allow direct testing of a text search parser.
  136. </p><a id="id-1.5.11.11.4.3" class="indexterm"></a><pre class="synopsis">
  137. ts_parse(<em class="replaceable"><code>parser_name</code></em> <code class="type">text</code>, <em class="replaceable"><code>document</code></em> <code class="type">text</code>,
  138. OUT <em class="replaceable"><code>tokid</code></em> <code class="type">integer</code>, OUT <em class="replaceable"><code>token</code></em> <code class="type">text</code>) returns <code class="type">setof record</code>
  139. ts_parse(<em class="replaceable"><code>parser_oid</code></em> <code class="type">oid</code>, <em class="replaceable"><code>document</code></em> <code class="type">text</code>,
  140. OUT <em class="replaceable"><code>tokid</code></em> <code class="type">integer</code>, OUT <em class="replaceable"><code>token</code></em> <code class="type">text</code>) returns <code class="type">setof record</code>
  141. </pre><p>
  142. <code class="function">ts_parse</code> parses the given <em class="replaceable"><code>document</code></em>
  143. and returns a series of records, one for each token produced by
  144. parsing. Each record includes a <code class="varname">tokid</code> showing the
  145. assigned token type and a <code class="varname">token</code> which is the text of the
  146. token. For example:
  147. </p><pre class="screen">
  148. SELECT * FROM ts_parse('default', '123 - a number');
  149. tokid | token
  150. -------+--------
  151. 22 | 123
  152. 12 |
  153. 12 | -
  154. 1 | a
  155. 12 |
  156. 1 | number
  157. </pre><p>
  158. </p><a id="id-1.5.11.11.4.6" class="indexterm"></a><pre class="synopsis">
  159. ts_token_type(<em class="replaceable"><code>parser_name</code></em> <code class="type">text</code>, OUT <em class="replaceable"><code>tokid</code></em> <code class="type">integer</code>,
  160. OUT <em class="replaceable"><code>alias</code></em> <code class="type">text</code>, OUT <em class="replaceable"><code>description</code></em> <code class="type">text</code>) returns <code class="type">setof record</code>
  161. ts_token_type(<em class="replaceable"><code>parser_oid</code></em> <code class="type">oid</code>, OUT <em class="replaceable"><code>tokid</code></em> <code class="type">integer</code>,
  162. OUT <em class="replaceable"><code>alias</code></em> <code class="type">text</code>, OUT <em class="replaceable"><code>description</code></em> <code class="type">text</code>) returns <code class="type">setof record</code>
  163. </pre><p>
  164. <code class="function">ts_token_type</code> returns a table which describes each type of
  165. token the specified parser can recognize. For each token type, the table
  166. gives the integer <code class="varname">tokid</code> that the parser uses to label a
  167. token of that type, the <code class="varname">alias</code> that names the token type
  168. in configuration commands, and a short <code class="varname">description</code>. For
  169. example:
  170. </p><pre class="screen">
  171. SELECT * FROM ts_token_type('default');
  172. tokid | alias | description
  173. -------+-----------------+------------------------------------------
  174. 1 | asciiword | Word, all ASCII
  175. 2 | word | Word, all letters
  176. 3 | numword | Word, letters and digits
  177. 4 | email | Email address
  178. 5 | url | URL
  179. 6 | host | Host
  180. 7 | sfloat | Scientific notation
  181. 8 | version | Version number
  182. 9 | hword_numpart | Hyphenated word part, letters and digits
  183. 10 | hword_part | Hyphenated word part, all letters
  184. 11 | hword_asciipart | Hyphenated word part, all ASCII
  185. 12 | blank | Space symbols
  186. 13 | tag | XML tag
  187. 14 | protocol | Protocol head
  188. 15 | numhword | Hyphenated word, letters and digits
  189. 16 | asciihword | Hyphenated word, all ASCII
  190. 17 | hword | Hyphenated word, all letters
  191. 18 | url_path | URL path
  192. 19 | file | File or path name
  193. 20 | float | Decimal notation
  194. 21 | int | Signed integer
  195. 22 | uint | Unsigned integer
  196. 23 | entity | XML entity
  197. </pre><p>
  198. </p></div><div class="sect2" id="TEXTSEARCH-DICTIONARY-TESTING"><div class="titlepage"><div><div><h3 class="title">12.8.3. Dictionary Testing</h3></div></div></div><p>
  199. The <code class="function">ts_lexize</code> function facilitates dictionary testing.
  200. </p><a id="id-1.5.11.11.5.3" class="indexterm"></a><pre class="synopsis">
  201. ts_lexize(<em class="replaceable"><code>dict</code></em> <code class="type">regdictionary</code>, <em class="replaceable"><code>token</code></em> <code class="type">text</code>) returns <code class="type">text[]</code>
  202. </pre><p>
  203. <code class="function">ts_lexize</code> returns an array of lexemes if the input
  204. <em class="replaceable"><code>token</code></em> is known to the dictionary,
  205. or an empty array if the token
  206. is known to the dictionary but it is a stop word, or
  207. <code class="literal">NULL</code> if it is an unknown word.
  208. </p><p>
  209. Examples:
  210. </p><pre class="screen">
  211. SELECT ts_lexize('english_stem', 'stars');
  212. ts_lexize
  213. -----------
  214. {star}
  215. SELECT ts_lexize('english_stem', 'a');
  216. ts_lexize
  217. -----------
  218. {}
  219. </pre><p>
  220. </p><div class="note"><h3 class="title">Note</h3><p>
  221. The <code class="function">ts_lexize</code> function expects a single
  222. <span class="emphasis"><em>token</em></span>, not text. Here is a case
  223. where this can be confusing:
  224. </p><pre class="screen">
  225. SELECT ts_lexize('thesaurus_astro','supernovae stars') is null;
  226. ?column?
  227. ----------
  228. t
  229. </pre><p>
  230. The thesaurus dictionary <code class="literal">thesaurus_astro</code> does know the
  231. phrase <code class="literal">supernovae stars</code>, but <code class="function">ts_lexize</code>
  232. fails since it does not parse the input text but treats it as a single
  233. token. Use <code class="function">plainto_tsquery</code> or <code class="function">to_tsvector</code> to
  234. test thesaurus dictionaries, for example:
  235. </p><pre class="screen">
  236. SELECT plainto_tsquery('supernovae stars');
  237. plainto_tsquery
  238. -----------------
  239. 'sn'
  240. </pre><p>
  241. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="textsearch-configuration.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="textsearch.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="textsearch-indexes.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">12.7. Configuration Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 12.9. GIN and GiST Index Types</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1