How to request Sphinx for an exact matching phrase?

Sphinx seems to be searching for documents by word. I do not know how to search for documents for the exact phrase. I tried SPH_MATCH_ALL , SPH_MATCH_PHRASE , but everyone was looking for documents by word. I use it in my PHP application.

How can I query Sphinx to match the exact string?

Here is my code:

 $sphinx = new SphinxClient(); $mode = SPH_MATCH_PHRASE; $sphinx->setServer('127.0.0.1', 9312); $sphinx->setLimits(0,1); $sphinx->setMaxQueryTime(5000); $sphinx->setMatchMode($mode); $sphinx->setFieldWeights(array('name' => 100)); $sphinx->setArrayResult(true); $result = $sphinx->query('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'); print_r($result); 

Return Result:

 Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => name [1] => company [2] => image [3] => price ) [attrs] => Array () [total] => 0 [total_found] => 0 [time] => 0.000 [words] => Array ( [lorem] => Array ( [docs] => 0 [hits] => 0 ) [ipsum] => Array ( [docs] => 0 [hits] => 0 ) [dolor] => Array ( [docs] => 0 [hits] => 0 ) [sit] => Array ( [docs] => 0 [hits] => 0 ) [amet] => Array ( [docs] => 0 [hits] => 0 ) [consectetur] => Array ( [docs] => 0 [hits] => 0 ) [adipiscing] => Array ( [docs] => 0 [hits] => 0 ) [elit] => Array ( [docs] => 0 [hits] => 0 ) ) ) 

As you can see, Sphinx searches for documents by word ...

+6
php sphinx
source share
8 answers

The best way is to use the syntax SPH_MATCH_EXTENDED2 and take the request in double quotes.

 $sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); $sphinx->Query('"Lorem ipsum dolor"'); 

Extended syntax

+5
source share

using:

 $sphinx->SetMatchMode(SPH_MATCH_PHRASE); 

SPH_MATCH_ALL Match all query words (default mode).

SPH_MATCH_ANY Match any query words.

SPH_MATCH_PHRASE Define the query as a phrase that requires perfect match.

SPH_MATCH_BOOLEAN Define the query as a Boolean expression.

SPH_MATCH_EXTENDED Compliance request as an expression in the Sphinx internal query language.

SPH_MATCH_FULLSCAN Enables full screen mode.

SPH_MATCH_EXTENDED2 Same as SPH_MATCH_EXTENDED plus search and quorum search support.

+3
source share

Currently, I have found a better way to do this using ^ $ modifiers.

If you look here: Sphinx extended syntax syntax , you can see that you can make a match that looks something like:

 ^Exact String$ 

This should help solve the problem.

+2
source share

I know I'm late for the party, but what happens when you do a search from the command line?

 sphinx/bin/search -i indexName Lorem ipsum -e2 

-e2 - advanced matching mode 2.

Also remember to reindex the sphinx indices:

 sphinx/bin/indexer --rotate --config sphinx/etc/sphinx.conf --all 

And make sure searchd is running.

+1
source share

I think the best way ...
1. using advanced mode 2
and
2. using the syntax in this way β†’ (filed-start and filed-end) && & & double value

for example

 $sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); $sphinx->Query('(^Lorem ipsum dolor$ "Lorem ipsum dolor")'); 
+1
source share

The best solution I have:

 $searchTemplate = '@(%s) "^%s$" | "^%s" | "%s" | (%s)'; $sqlToSearch .= sprintf($searchTemplate, "part_name", //Index to search in trim($stringToSearch), trim($stringToSearch), trim($stringToSearch), trim($stringToSearch)); 

In this case, an exact match will be the first.

+1
source share

I believe that you see statistics that come back with search results. When sphinx completes, it returns statistics about where the words were found so that you can adjust your search if necessary. To check, you must perform a search that returns results. You should also do some testing in the test index, where you know what results will be obtained for any particular search.

0
source share

if you tried everything above and nothing worked, check these parameters in the sphinx.conf file, specifying your conf index

 index lol { source = lol path = /var/lib/sphinxsearch/data/lol morphology = none min_word_len = 3 min_prefix_len = 0 min_infix_len = 0 ... 

set min_prefix_len to zero

and don't forget to reindex again !!

0
source share

All Articles