example.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8" />
  4. <title>中文全文检索样例 @ PostgreSQL</title>
  5. <style>
  6. * {
  7. font-family: "Noto Sans", "Noto Sans SC", "Noto Sans TC", "Padauk", "ATaiThamKHNewV3-Normal", Arial, Verdana;
  8. }
  9. td {
  10. border-right-style: solid;
  11. border-top-style: solid;
  12. }
  13. table {
  14. border-style: solid;
  15. }
  16. table span {
  17. background-color: yellow;
  18. font-size: 1.2em;
  19. }
  20. th {
  21. font-weight: bold;
  22. }
  23. input[name="q"] {
  24. width: 70%;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <?php
  30. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  31. // collect value of input field
  32. $q = $_POST['q'];
  33. }
  34. ?>
  35. <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  36. Text: <input type="text" name="q" value="<?php echo $q ?>">
  37. <input type="submit" value="Query">
  38. </form>
  39. <?php
  40. require_once '../../config.php';
  41. if (empty($q)) {
  42. echo "Query is empty";
  43. } else {
  44. // Connecting, selecting database
  45. $dbconn = pg_connect("host="._DB_HOST_." dbname="._DB_NAME_." user="._DB_USERNAME_." password="._DB_PASSWORD_)
  46. or die('Could not connect: ' . pg_last_error());
  47. // Performing SQL query
  48. $query = "WITH queries AS (
  49. SELECT
  50. '$q' AS q1,
  51. CASE
  52. WHEN '$q' = opencc_t2s('$q') THEN opencc_s2t('$q')
  53. ELSE opencc_t2s('$q')
  54. END AS q2
  55. )
  56. SELECT
  57. ts_rank('{0.1, 0.2, 0.4, 1}',
  58. full_text_search_weighted,
  59. websearch_to_tsquery('jiebacfg', (SELECT q1 FROM queries))) AS rank,
  60. ts_headline('jiebacfg', author,
  61. websearch_to_tsquery('jiebacfg', (SELECT q1 FROM queries)),
  62. 'StartSel = <span>, StopSel = </span>')
  63. AS highlight_author,
  64. ts_headline('jiebacfg', title,
  65. websearch_to_tsquery('jiebacfg', (SELECT q1 FROM queries)),
  66. 'StartSel = <span>, StopSel = </span>')
  67. AS highlight_title,
  68. ts_headline('jiebacfg', subtitle,
  69. websearch_to_tsquery('jiebacfg', (SELECT q1 FROM queries)),
  70. 'StartSel = <span>, StopSel = </span>')
  71. AS highlight_subtitle,
  72. ts_headline('jiebacfg', content,
  73. websearch_to_tsquery('jiebacfg', (SELECT q1 FROM queries)),
  74. 'StartSel = <span>, StopSel = </span>')
  75. AS highlight_content,
  76. author, title, subtitle, content, full_text_search_weighted
  77. FROM sample
  78. WHERE
  79. full_text_search_weighted @@ websearch_to_tsquery('jiebacfg', (SELECT q1 FROM queries))
  80. UNION
  81. SELECT
  82. ts_rank('{0.1, 0.2, 0.4, 1}',
  83. full_text_search_weighted,
  84. websearch_to_tsquery('jiebacfg', (SELECT q2 FROM queries))) - 0.1 AS rank,
  85. ts_headline('jiebacfg', author,
  86. websearch_to_tsquery('jiebacfg', (SELECT q2 FROM queries)),
  87. 'StartSel = <span>, StopSel = </span>')
  88. AS highlight_author,
  89. ts_headline('jiebacfg', title,
  90. websearch_to_tsquery('jiebacfg', (SELECT q2 FROM queries)),
  91. 'StartSel = <span>, StopSel = </span>')
  92. AS highlight_title,
  93. ts_headline('jiebacfg', subtitle,
  94. websearch_to_tsquery('jiebacfg', (SELECT q2 FROM queries)),
  95. 'StartSel = <span>, StopSel = </span>')
  96. AS highlight_subtitle,
  97. ts_headline('jiebacfg', content,
  98. websearch_to_tsquery('jiebacfg', (SELECT q2 FROM queries)),
  99. 'StartSel = <span>, StopSel = </span>')
  100. AS highlight_content,
  101. author, title, subtitle, content, full_text_search_weighted
  102. FROM sample
  103. WHERE
  104. full_text_search_weighted @@ websearch_to_tsquery('jiebacfg', (SELECT q2 FROM queries))
  105. ORDER BY rank DESC LIMIT 20;
  106. ";
  107. $result = pg_query($query) or die('Query failed: ' . pg_last_error());
  108. // Printing results in HTML
  109. echo "<table>\n";
  110. echo "<tr>
  111. <th>rank</th>
  112. <th>author (hl)</th>
  113. <th>title (hl)</th>
  114. <th>subtitle (hl)</th>
  115. <th>content (hl)</th>
  116. <th>author</th>
  117. <th>title</th>
  118. <th>subtitle</th>
  119. <th>content</th>
  120. <th>TSVECTOR</th>
  121. </tr>";
  122. while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  123. echo "\t<tr>\n";
  124. foreach ($line as $col_value) {
  125. echo "\t\t<td><div class='cell'>$col_value</div></td>\n";
  126. }
  127. echo "\t</tr>\n";
  128. }
  129. echo "</table>\n";
  130. // Free resultset
  131. pg_free_result($result);
  132. // Closing connection
  133. pg_close($dbconn);
  134. }
  135. ?>
  136. </body>
  137. </html>