-->

sphinx search for partial keyword matches

2019-08-15 02:57发布

问题:

Ruby on Rails -> ThinkingSphinx -> SphinxSearch -> Mysql

I want to search the titles table. The expected user keyword will not be an exact match, it will be a partial match.

search_key = "android samsung galaxy ace black phones"

Searching against the titles table,

titles(table)

id | titles

1 | nokia c6
2 | samsung galaxy ace
3 | samsung galaxy ace y
4 | nokia lumia 800
5 | samsung monte
6 | samsung galaxy note

case - 1 :

Title.search search_key, :match_mode => :all

=>No results

Comment: Bad

case -2 :

Title.search search_key, :match_mode => :any

=>[samsung galaxy ace, samsung galaxy ace y, samsung monte, samsung galaxy note]

Comment: OK, but not relevant, the user wanted only "samsung galaxy ace" which she specified in her keywords explicitly. Why show other samsung mobiles ?

case -3 :

Title.search 'search_key/3',:match_mode => :extended

=> samsung galaxy ace

Comment: Bingo!, but hard coded.

Question:

Now, I should know how many number of keywords will get an exact match in the titles table. (For example, its 3 for "samsung galaxy ace" in "android samsung galaxy ace black phones")

How do I go around this?. Sphinx handles this?. If not what to do ?

回答1:

One way is to store a wordcount in the sphinx index as attribute.

sql_field_str2wordcount is a good way to do this http://sphinxsearch.com/docs/current.html#conf-sql-field-str2wordcount

You can then use it as a basis of a filter

$cl->setMatchMode(SPH_MATCH_EXTENDED);
$cl->setRankingMode(SPH_RANK_WORDCOUNT);
$cl->setSelect("*,IF(@weight=>titles,1,0) as myfilter");
$cl->setFilter("myfilter",array(1));
$cl->Query("\"$search_key\"/1",'Title');

(Sorry, dont know how to do this in thinking-sphinx particully. The above is the PHP API syntax)


Edit, checking http://freelancing-god.github.com/ts/en/searching.html and http://freelancing-god.github.com/ts/en/common_issues.html#or_attributes

looks like might be something like

with_display = "*, IF(@weight=>titles,1,0) AS display"
Title.search 'search_key/3',
  :match_mode => :extended,
  :rank_mode => :wordcount,
  :sphinx_select => with_display,
  :with          => {'display' => 1}


回答2:

ANSWER FOUND

This works if you install Sphinx 2.0.4-dev (rel20-r3193).

Follow this link,

http://sphinxsearch.com/forum/view.html?id=9387

Mr. Barry Hunter, thank you so much for your support.