Guest User

Untitled

a guest
Jun 14th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. source post_source
  2. {
  3. type = mysql
  4.  
  5. sql_host = localhost
  6. sql_user = ***
  7. sql_pass = ***
  8. sql_db = ***
  9. sql_port = 3306
  10.  
  11. sql_query_pre = SET NAMES utf8
  12. # query before fetching rows to index
  13.  
  14. sql_query = SELECT *, id AS pid, CRC32(safetag) as safetag_crc32 FROM hb_posts
  15.  
  16.  
  17. sql_attr_uint = pid
  18. # pid (as 'sql_attr_uint') is necessary for sphinx
  19. # this field must be unique
  20.  
  21. # that is why I like sphinx
  22. # you can store custom string fields into indexes (memory) as well
  23. sql_field_string = title
  24. sql_field_string = slug
  25. sql_field_string = content
  26. sql_field_string = tags
  27.  
  28. sql_attr_uint = category
  29. # integer fields must be defined as sql_attr_uint
  30.  
  31. sql_attr_timestamp = date
  32. # timestamp fields must be defined as sql_attr_timestamp
  33.  
  34. sql_query_info_pre = SET NAMES utf8
  35. # if you need unicode support for sql_field_string, you need to patch the source
  36. # this param. is not supported natively
  37.  
  38. sql_query_info = SELECT * FROM my_posts WHERE id = $id
  39. }
  40.  
  41. index posts
  42. {
  43. source = post_source
  44. # source above
  45.  
  46. path = /var/data/posts
  47. # index location
  48.  
  49. charset_type = utf-8
  50. }
  51.  
  52. <?php
  53.  
  54. require "sphinxapi.php";
  55.  
  56. $safetag = $_GET["my_post_slug"];
  57. // $safetag = preg_replace("/[^a-z0-9-_]/i", "", $safetag);
  58.  
  59. $conf = getMyConf();
  60.  
  61. $cl = New SphinxClient();
  62.  
  63. $cl->SetServer($conf["server"], $conf["port"]);
  64. $cl->SetConnectTimeout($conf["timeout"]);
  65. $cl->setMaxQueryTime($conf["max"]);
  66.  
  67. # set search params
  68. $cl->SetMatchMode(SPH_MATCH_FULLSCAN);
  69. $cl->SetArrayResult(TRUE);
  70.  
  71. $cl->setLimits(0, 1, 1);
  72. # looking for the post (not searching a keyword)
  73.  
  74. $cl->SetFilter("safetag_crc32", array(crc32($safetag)));
  75.  
  76. # fetch results
  77. $post = $cl->Query(null, "post_1");
  78.  
  79. echo "<pre>";
  80. var_dump($post);
  81. echo "</pre>";
  82. exit("done");
  83. ?>
  84.  
  85. [array] =>
  86. "id" => 123,
  87. "title" => "My post title.",
  88. "content" => "My <p>post</p> content.",
  89. ...
  90. [ and other fields ]
  91.  
  92. 0.001 sec.
  93.  
  94. => 0.346 sec. (average)
  95. => 0.340 sec. (average of last 10 query)
  96.  
  97. "SELECT * FROM hb_posts WHERE id = 123;"
  98. => 0.001 sec.
  99.  
  100. "SELECT * FROM my_posts WHERE id = 123;"
  101. => 1.612 sec. (average)
  102. => 1.920 sec. (average of last 10 query)
Add Comment
Please, Sign In to add comment