PHP - How to Configure Wildcard and Fuzzy Search in Sphinx w/ RT Indexes
- Is there any way to make wildcard search in sphinx rt indexes?
- How can I query rt index with sphinx client api for php?
The only way I fou开发者_StackOverflownd is to use mysql_connect()
and mysql_query()
with sphinxQL.
I asked this question on sphinxsearch forum and received this reply:
Hello.
barryhunter just replied to 'RT indexes wildcard search':
===cut===
1.Is there any way to make wildcard search in sphinx rt indexes?
I dont beleive so - not implemented yet. Check the bug Tracker, if not ther add it as a feature request.
2.How can I query rt index with sphinx client api for php?
Yes the Sphinx API can query RT indexes (it just cant update them)
(But because RT indexes doesnt index prefixes/infixes (required for wildcard searching) it wont give you any benefit. Everything can do with sphinx API can be done with sphinxQL now I beleive)
===cut===
1.) To configure wildcard and fuzzy search in your model, first set the enable_star and min_infix_len properties inside the define_index block:
class Post...
define_index do
...
set_property :enable_star => true
set_property :min_infix_len => 1
end
Optionally you can make the settings global by adding them to config/sphinx.yml:
production:
enable_star: true
min_infix_len: 1
Stop, configure, reindex and start Sphinx For Sphinx to pickup the changes we need to stop, configure, reindex and start Sphinx. Thinking Sphinx has some rake tasks that allow you to do this:
RAILS_ENV=xxx
rake ts:stop
rake ts:conf
rake ts:in
rake ts:start
Verify Sphinx configuration Now open the Sphinx configuration file in an editor:
$ vim config/production.sphinx.conf
Verify that you can see the correct settings:
...
index post_core
{
...
min_infix_len = 1
enable_star = true
}
...
Fire up the console and run some queries:
Post.search('xxx', :star => true)
Now, all that’s left is to create the search controller and view:
class SearchController...
def index
@query = params[:query]
options = {
:page => params[:page], :per_page => params[:per_page], :star => true,
:field_weights => { :title => 20, :tags => 10, :body => 5 }
}
@posts = Post.search(@query, options)
end
Note: To get relevant search results you need to assign different weights to fields.
And finally, here’s the front-end php to post the search results in view code:
<% @posts.each do |post| %>
content goes here...
<% end %>
2.) Here's an example of querying rt index with sphinx client api for php:
include('include/sphinxapi.php');
$_longitude = '42.358431';
$_latitude = '-71.059773';
$search = new SphinxClient();
$search->SetServer('[SERVER IP REMOVED]', 9312);
$search->SetConnectTimeout(1);
$search->SetArrayResult(true);
$search->SetMatchMode(SPH_MATCH_ALL);
$search->SetGeoAnchor('venue_latitude', 'venue_longitude', (float)deg2rad($_latitude), (float)deg2rad($_longitude));
$search->SetSelect('*');
$search->SetLimits(0, 100);
$result = $search->Query('b', 'rt_deals');
# results, print_r($result):
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => deal_types
)
[attrs] => Array
(
[venue_id] => 1
[venue_latitude] => 5
[venue_longitude] => 5
[dt_start] => 2
[dt_end] => 2
[@geodist] => 5
)
[matches] => Array
(
[0] => Array
(
[id] => 45
[weight] => 1
[attrs] => Array
(
[venue_id] => 42
[venue_latitude] => 0.73878991603851
[venue_longitude] => -1.2425578832626
[dt_start] => 0
[dt_end] => 0
[@geodist] => 15278498
)
)
[1] => Array
(
[id] => 46
[weight] => 1
[attrs] => Array
(
[venue_id] => 41
[venue_latitude] => 0.73908978700638
[venue_longitude] => -1.2415384054184
[dt_start] => 0
[dt_end] => 0
[@geodist] => 15278115
)
)
)
[total] => 2
[total_found] => 2
[time] => 0.000
[words] => Array
(
[b] => Array
(
[docs] => 2
[hits] => 2
)
)
)
For more info, you can check out the full example code of a GeoSpatial Search Using Sphinx Search w/ Php from God-Object.com
精彩评论