example.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <html>
  2. <head>
  3. <title>Pali Full Text Search Example @ PostgreSQL</title>
  4. <style>
  5. * {
  6. font-family: "Noto Sans", "Noto Sans SC", "Noto Sans TC", "Padauk", "ATaiThamKHNewV3-Normal", Arial, Verdana;
  7. }
  8. td {
  9. border-right-style: solid;
  10. border-top-style: solid;
  11. }
  12. table {
  13. border-style: solid;
  14. }
  15. table span {
  16. background-color: yellow;
  17. font-size: 1.2em;
  18. }
  19. th {
  20. font-weight: bold;
  21. }
  22. input[name="q"] {
  23. width: 70%;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <?php
  29. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  30. // collect value of input field
  31. $q = $_POST['q'];
  32. }
  33. ?>
  34. <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  35. Name: <input type="text" name="q" value="<?php echo $q ?>">
  36. <input type="submit">
  37. </form>
  38. <?php
  39. if (empty($q)) {
  40. echo "Query is empty";
  41. } else {
  42. // Connecting, selecting database
  43. $dbconn = pg_connect("host=localhost dbname=pali user=postgres password=123456")
  44. or die('Could not connect: ' . pg_last_error());
  45. // Performing SQL query
  46. $query = "SELECT
  47. ts_rank('{0.1, 0.2, 0.4, 1}',
  48. full_text_search_weighted,
  49. websearch_to_tsquery('pali', '$q')) +
  50. ts_rank('{0.1, 0.2, 0.4, 1}',
  51. full_text_search_weighted_unaccent,
  52. websearch_to_tsquery('pali_unaccent', '$q'))
  53. AS rank,
  54. ts_headline('simple', content,
  55. websearch_to_tsquery('simple', '$q'),
  56. 'StartSel = <span>, StopSel = </span>')
  57. AS highlight,
  58. *
  59. FROM fts
  60. WHERE
  61. full_text_search_weighted
  62. @@ websearch_to_tsquery('pali', '$q') OR
  63. full_text_search_weighted_unaccent
  64. @@ websearch_to_tsquery('pali_unaccent', '$q')
  65. ORDER BY rank DESC
  66. LIMIT 20;";
  67. $result = pg_query($query) or die('Query failed: ' . pg_last_error());
  68. // Printing results in HTML
  69. echo "<table>\n";
  70. echo "<tr>
  71. <th>rank</th>
  72. <th>highlight</th>
  73. <th>paragraph</th>
  74. <th>book</th>
  75. <th>wid</th>
  76. <th>bold_single</th>
  77. <th>bold_double</th>
  78. <th>bold_multiple</th>
  79. <th>content</th>
  80. <th>TSVECTOR</th>
  81. <th>TSVECTOR (unaccent)</th>
  82. </tr>";
  83. while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  84. echo "\t<tr>\n";
  85. foreach ($line as $col_value) {
  86. echo "\t\t<td><div class='cell'>$col_value</div></td>\n";
  87. }
  88. echo "\t</tr>\n";
  89. }
  90. echo "</table>\n";
  91. // Free resultset
  92. pg_free_result($result);
  93. // Closing connection
  94. pg_close($dbconn);
  95. }
  96. ?>
  97. </body>
  98. </html>