Search code examples
sphinx

Sphinx query before and after a term


Is it possible to set up a query in sphinx with a term that has to either also match a word before OR after?

  • (TermBefore) (Term) (TermAfter)

so that both

  • TermBefore Term
  • Term TermAfter

would match but

  • Term

does not?


Solution

  • The proximity search operator is pretty much designed for this

     "Term TermAfter"~2
    

    http://sphinxsearch.com/docs/current.html#extended-syntax


    Ah, I thought you meant 'TermAfter' to be actully be the same word, just that it can be before or after.

    But if two different terms, possibly the easiest is just to do:

    "TermBefore Term" | "Term TermAfter"
    

    Just simple phrase operator, where either phrase must match.


    Edit again:

    If dont want the matchs adjecent use Strict order operator, rather htna phrase operator...

    (TermBefore << Term) | (Term << TermAfter)