开发者

How to query Sphinx for an exact matching phrase?

It seems that Sphinx is searching the documents word by word. I don't know how to search the documents for an exact phrase. I tried SPH_MATCH_ALL, SPH_MATCH_PHRASE but all search the documents word by word. I'm using it in my PHP application.

How do I query Sphinx to match an exact string?

Here's 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);

The return result is this:

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
           开发者_StackOverflow中文版 [hits] => 0
        )
    )
)

As you can see, Sphinx is searching the documents word by word...


The best way is to use SPH_MATCH_EXTENDED2 syntax and take your query in double quotes.

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

Extended syntax


use:

$sphinx->SetMatchMode(SPH_MATCH_PHRASE);

SPH_MATCH_ALL Match all query words (default mode).

SPH_MATCH_ANY Match any of query words.

SPH_MATCH_PHRASE Match query as a phrase, requiring perfect match.

SPH_MATCH_BOOLEAN Match query as a boolean expression.

SPH_MATCH_EXTENDED Match query as an expression in Sphinx internal query language.

SPH_MATCH_FULLSCAN Enables fullscan.

SPH_MATCH_EXTENDED2 The same as SPH_MATCH_EXTENDED plus ranking and quorum searching support.


Currently I've found the best way to do it is by using the ^$ modifiers.

If you view here: Sphinx Extended Syntax you can see that you can do a match similar to something like:

^Exact String$

This should help resolve the issue.


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

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

the -e2 is extended match 2 mode.

Also don't forget to reindex the sphinx indexes:

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

And make sure that searchd is running.


I think best way...
1. using extended2 mode
and
2. using syntax this way -> (filed-start and filed-end) && double quot

For example

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


The best solution I have is this:

$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 the exact match will come first.


I believe what you are seeing are the statistics that are returned along with the search results. When sphinx completes, it returns stats on where the words were found so that you can adjust your search if necessary. In order to verify, you should do a search that returns results. You should also do some testing on a test index where you know what the results will be for any particular search.


if you tried all above and nothing worked check this params on your sphinx.conf file , on your index conf

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 dont forget to reindex again!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜