gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

139 lines
9.6KB

  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.2. Tables and Indexes</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-intro.html" title="12.1. Introduction" /><link rel="next" href="textsearch-controls.html" title="12.3. Controlling Text Search" /></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.2. Tables and Indexes</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="textsearch-intro.html" title="12.1. Introduction">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-controls.html" title="12.3. Controlling Text Search">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TEXTSEARCH-TABLES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">12.2. Tables and Indexes</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="textsearch-tables.html#TEXTSEARCH-TABLES-SEARCH">12.2.1. Searching a Table</a></span></dt><dt><span class="sect2"><a href="textsearch-tables.html#TEXTSEARCH-TABLES-INDEX">12.2.2. Creating Indexes</a></span></dt></dl></div><p>
  3. The examples in the previous section illustrated full text matching using
  4. simple constant strings. This section shows how to search table data,
  5. optionally using indexes.
  6. </p><div class="sect2" id="TEXTSEARCH-TABLES-SEARCH"><div class="titlepage"><div><div><h3 class="title">12.2.1. Searching a Table</h3></div></div></div><p>
  7. It is possible to do a full text search without an index. A simple query
  8. to print the <code class="structname">title</code> of each row that contains the word
  9. <code class="literal">friend</code> in its <code class="structfield">body</code> field is:
  10. </p><pre class="programlisting">
  11. SELECT title
  12. FROM pgweb
  13. WHERE to_tsvector('english', body) @@ to_tsquery('english', 'friend');
  14. </pre><p>
  15. This will also find related words such as <code class="literal">friends</code>
  16. and <code class="literal">friendly</code>, since all these are reduced to the same
  17. normalized lexeme.
  18. </p><p>
  19. The query above specifies that the <code class="literal">english</code> configuration
  20. is to be used to parse and normalize the strings. Alternatively we
  21. could omit the configuration parameters:
  22. </p><pre class="programlisting">
  23. SELECT title
  24. FROM pgweb
  25. WHERE to_tsvector(body) @@ to_tsquery('friend');
  26. </pre><p>
  27. This query will use the configuration set by <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TEXT-SEARCH-CONFIG">default_text_search_config</a>.
  28. </p><p>
  29. A more complex example is to
  30. select the ten most recent documents that contain <code class="literal">create</code> and
  31. <code class="literal">table</code> in the <code class="structname">title</code> or <code class="structname">body</code>:
  32. </p><pre class="programlisting">
  33. SELECT title
  34. FROM pgweb
  35. WHERE to_tsvector(title || ' ' || body) @@ to_tsquery('create &amp; table')
  36. ORDER BY last_mod_date DESC
  37. LIMIT 10;
  38. </pre><p>
  39. For clarity we omitted the <code class="function">coalesce</code> function calls
  40. which would be needed to find rows that contain <code class="literal">NULL</code>
  41. in one of the two fields.
  42. </p><p>
  43. Although these queries will work without an index, most applications
  44. will find this approach too slow, except perhaps for occasional ad-hoc
  45. searches. Practical use of text searching usually requires creating
  46. an index.
  47. </p></div><div class="sect2" id="TEXTSEARCH-TABLES-INDEX"><div class="titlepage"><div><div><h3 class="title">12.2.2. Creating Indexes</h3></div></div></div><p>
  48. We can create a <acronym class="acronym">GIN</acronym> index (<a class="xref" href="textsearch-indexes.html" title="12.9. GIN and GiST Index Types">Section 12.9</a>) to speed up text searches:
  49. </p><pre class="programlisting">
  50. CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', body));
  51. </pre><p>
  52. Notice that the 2-argument version of <code class="function">to_tsvector</code> is
  53. used. Only text search functions that specify a configuration name can
  54. be used in expression indexes (<a class="xref" href="indexes-expressional.html" title="11.7. Indexes on Expressions">Section 11.7</a>).
  55. This is because the index contents must be unaffected by <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TEXT-SEARCH-CONFIG">default_text_search_config</a>. If they were affected, the
  56. index contents might be inconsistent because different entries could
  57. contain <code class="type">tsvector</code>s that were created with different text search
  58. configurations, and there would be no way to guess which was which. It
  59. would be impossible to dump and restore such an index correctly.
  60. </p><p>
  61. Because the two-argument version of <code class="function">to_tsvector</code> was
  62. used in the index above, only a query reference that uses the 2-argument
  63. version of <code class="function">to_tsvector</code> with the same configuration
  64. name will use that index. That is, <code class="literal">WHERE
  65. to_tsvector('english', body) @@ 'a &amp; b'</code> can use the index,
  66. but <code class="literal">WHERE to_tsvector(body) @@ 'a &amp; b'</code> cannot.
  67. This ensures that an index will be used only with the same configuration
  68. used to create the index entries.
  69. </p><p>
  70. It is possible to set up more complex expression indexes wherein the
  71. configuration name is specified by another column, e.g.:
  72. </p><pre class="programlisting">
  73. CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector(config_name, body));
  74. </pre><p>
  75. where <code class="literal">config_name</code> is a column in the <code class="literal">pgweb</code>
  76. table. This allows mixed configurations in the same index while
  77. recording which configuration was used for each index entry. This
  78. would be useful, for example, if the document collection contained
  79. documents in different languages. Again,
  80. queries that are meant to use the index must be phrased to match, e.g.,
  81. <code class="literal">WHERE to_tsvector(config_name, body) @@ 'a &amp; b'</code>.
  82. </p><p>
  83. Indexes can even concatenate columns:
  84. </p><pre class="programlisting">
  85. CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));
  86. </pre><p>
  87. </p><p>
  88. Another approach is to create a separate <code class="type">tsvector</code> column
  89. to hold the output of <code class="function">to_tsvector</code>. To keep this
  90. column automatically up to date with its source data, use a stored
  91. generated column. This example is a
  92. concatenation of <code class="literal">title</code> and <code class="literal">body</code>,
  93. using <code class="function">coalesce</code> to ensure that one field will still be
  94. indexed when the other is <code class="literal">NULL</code>:
  95. </p><pre class="programlisting">
  96. ALTER TABLE pgweb
  97. ADD COLUMN textsearchable_index_col tsvector
  98. GENERATED ALWAYS AS (to_tsvector('english', coalesce(title, '') || ' ' || coalesce(body, ''))) STORED;
  99. </pre><p>
  100. Then we create a <acronym class="acronym">GIN</acronym> index to speed up the search:
  101. </p><pre class="programlisting">
  102. CREATE INDEX textsearch_idx ON pgweb USING GIN (textsearchable_index_col);
  103. </pre><p>
  104. Now we are ready to perform a fast full text search:
  105. </p><pre class="programlisting">
  106. SELECT title
  107. FROM pgweb
  108. WHERE textsearchable_index_col @@ to_tsquery('create &amp; table')
  109. ORDER BY last_mod_date DESC
  110. LIMIT 10;
  111. </pre><p>
  112. </p><p>
  113. One advantage of the separate-column approach over an expression index
  114. is that it is not necessary to explicitly specify the text search
  115. configuration in queries in order to make use of the index. As shown
  116. in the example above, the query can depend on
  117. <code class="varname">default_text_search_config</code>. Another advantage is that
  118. searches will be faster, since it will not be necessary to redo the
  119. <code class="function">to_tsvector</code> calls to verify index matches. (This is more
  120. important when using a GiST index than a GIN index; see <a class="xref" href="textsearch-indexes.html" title="12.9. GIN and GiST Index Types">Section 12.9</a>.) The expression-index approach is
  121. simpler to set up, however, and it requires less disk space since the
  122. <code class="type">tsvector</code> representation is not stored explicitly.
  123. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="textsearch-intro.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-controls.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">12.1. Introduction </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 12.3. Controlling Text Search</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1