2
0

example.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. Text: <input type="text" name="q" value="<?php echo $q ?>">
  36. <input type="submit" value="Query">
  37. </form>
  38. <?php
  39. require_once '../path.php';
  40. if (empty($q)) {
  41. echo "Query is empty";
  42. } else {
  43. // Connecting, selecting database
  44. $dbconn = pg_connect("host="._DB_HOST_." dbname="._DB_NAME_." user="._DB_USERNAME_." password="._DB_PASSWORD_)
  45. or die('Could not connect: ' . pg_last_error());
  46. // Performing SQL query
  47. $query = "SELECT
  48. ts_rank('{0.1, 0.2, 0.4, 1}',
  49. full_text_search_weighted,
  50. websearch_to_tsquery('pali', '$q')) +
  51. ts_rank('{0.1, 0.2, 0.4, 1}',
  52. full_text_search_weighted_unaccent,
  53. websearch_to_tsquery('pali_unaccent', '$q'))
  54. AS rank,
  55. ts_headline('pali', content,
  56. websearch_to_tsquery('pali', '$q'),
  57. 'StartSel = <span>, StopSel = </span>')
  58. AS highlight,
  59. *
  60. FROM fts_texts
  61. WHERE
  62. full_text_search_weighted
  63. @@ websearch_to_tsquery('pali', '$q') OR
  64. full_text_search_weighted_unaccent
  65. @@ websearch_to_tsquery('pali_unaccent', '$q')
  66. ORDER BY rank DESC
  67. LIMIT 20;";
  68. $result = pg_query($query) or die('Query failed: ' . pg_last_error());
  69. // Printing results in HTML
  70. echo "<table>\n";
  71. echo "<tr>
  72. <th>rank</th>
  73. <th>highlight</th>
  74. <th>paragraph</th>
  75. <th>book</th>
  76. <th>wid</th>
  77. <th>bold_single</th>
  78. <th>bold_double</th>
  79. <th>bold_multiple</th>
  80. <th>content</th>
  81. <th>TSVECTOR</th>
  82. <th>TSVECTOR (unaccent)</th>
  83. </tr>";
  84. while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  85. echo "\t<tr>\n";
  86. foreach ($line as $col_value) {
  87. echo "\t\t<td><div class='cell'>$col_value</div></td>\n";
  88. }
  89. echo "\t</tr>\n";
  90. }
  91. echo "</table>\n";
  92. // Free resultset
  93. pg_free_result($result);
  94. // Closing connection
  95. pg_close($dbconn);
  96. }
  97. ?>
  98. </body>
  99. </html>