Search code examples
symfonysphinx

Sphinx Search missing results


I am using Sphinx with Symfony2 to serach people in my db.
The strange behavior is: I search for Jim J and the result is:

  • James 'Jim' Singer
  • Jim Abrahams
  • Jim Anderson
  • Jim Backus
  • Jim Bennon
  • ...

If i search for Jim Ja the result is:

  • James 'Jim' Singer
  • Jim Jansen
  • Jim Jarmusch
  • ...

Why am i missing the "Ja" results if i just search for Jim J, but i get all the others?

Here is the source and index code:

source peopleautocomplete {

sql_query           = SELECT id, firstName, lastName, pseudonym FROM Person

sql_attr_string     = pseudonym
sql_attr_string     = firstName
sql_attr_string     = lastName
}

index peopleautocomplete
{
source              = peopleautocomplete
path                = /var/lib/sphinxsearch/data/peopleautocomplete
docinfo             = extern
charset_type        = utf-8
min_word_len        = 2
enable_star         = 1
min_prefix_len      = 1
}

And the php code:

<?php
    $q = $request->query->get('q');
    $aq = explode(' ', $q);
    if (strlen($aq[count($aq) - 1]) < 2) {
        $query = $q;
    } else {
        $query = $q . '*';
    }
    $result = $this->_getSearchEngine()->searchEx($query, 'peopleautocomplete');
?>

Any idea?


Solution

  • Why am i missing the "Ja" results if i just search for Jim J, but i get all the others?

    Both search words 'Jim' and 'J' match Jim. So Jim Abrahams is valid (it matches both query words). The others are not 'missing' they just dropped off the bottom because so many matches.

    Can just use "Jim J" to search them as as phrase (your code should add the stars needed) - will require two matching words.

    But that would disallow middle names. To allow for that couple of options http://sphinxsearch.com/docs/current.html#extended-syntax

    perhaps the easiest is the strict order operator.

    Jim << J

    Or if want to allow people to enter Surname, followed by surname, could just use proximity or even NEAR


    Edit: looking again, see your code wont actually add stars correctly. Its adding to every space seperated token, even if itself is a operator.

    <?php $query = preg_replace('/(\w{2,}\b)/','$1*',$query); 
    

    is more effective, and more compact!

    Or expand_keywords option on the index to do it transparently.