How to query Sphinx for an exact matching phrase?

两盒软妹~` 提交于 2019-12-08 17:13:33

问题


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
            [hits] => 0
        )
    )
)

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


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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")'); 



回答6:


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.




回答7:


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.




回答8:


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!!



来源:https://stackoverflow.com/questions/5424780/how-to-query-sphinx-for-an-exact-matching-phrase

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!