example.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = "SELECT
  49. ts_rank('{0.1, 0.2, 0.4, 1}',
  50. full_text_search_weighted,
  51. websearch_to_tsquery('jiebacfg', '$q')) as rank,
  52. ts_headline('jiebacfg', author,
  53. websearch_to_tsquery('jiebacfg', '$q'),
  54. 'StartSel = <span>, StopSel = </span>')
  55. AS highlight_author,
  56. ts_headline('jiebacfg', title,
  57. websearch_to_tsquery('jiebacfg', '$q'),
  58. 'StartSel = <span>, StopSel = </span>')
  59. AS highlight_title,
  60. ts_headline('jiebacfg', subtitle,
  61. websearch_to_tsquery('jiebacfg', '$q'),
  62. 'StartSel = <span>, StopSel = </span>')
  63. AS highlight_subtitle,
  64. ts_headline('jiebacfg', content,
  65. websearch_to_tsquery('jiebacfg', '$q'),
  66. 'StartSel = <span>, StopSel = </span>')
  67. AS highlight_content,
  68. author, title, subtitle, content, full_text_search_weighted
  69. FROM sample
  70. WHERE full_text_search_weighted @@ websearch_to_tsquery('jiebacfg', '$q')
  71. ORDER BY rank DESC LIMIT 20;";
  72. $result = pg_query($query) or die('Query failed: ' . pg_last_error());
  73. // Printing results in HTML
  74. echo "<table>\n";
  75. echo "<tr>
  76. <th>rank</th>
  77. <th>author (hl)</th>
  78. <th>title (hl)</th>
  79. <th>subtitle (hl)</th>
  80. <th>content (hl)</th>
  81. <th>author</th>
  82. <th>title</th>
  83. <th>subtitle</th>
  84. <th>content</th>
  85. <th>TSVECTOR</th>
  86. </tr>";
  87. while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  88. echo "\t<tr>\n";
  89. foreach ($line as $col_value) {
  90. echo "\t\t<td><div class='cell'>$col_value</div></td>\n";
  91. }
  92. echo "\t</tr>\n";
  93. }
  94. echo "</table>\n";
  95. // Free resultset
  96. pg_free_result($result);
  97. // Closing connection
  98. pg_close($dbconn);
  99. }
  100. ?>
  101. </body>
  102. </html>