Class: Ferret::Search::PhraseQuery

Inherits:
Object
  • Object
show all
Defined in:
ext/r_search.c

Overview

Summary

PhraseQuery matches phrases like “the quick brown fox”. Most people are familiar with phrase queries having used them in most internet search engines.

Slop

Ferret’s phrase queries a slightly more advanced. You can match phrases with a slop, ie the match isn’t exact but it is good enough. The slop is basically the word edit distance of the phrase. For example, “the quick brown fox” with a slop of 1 would match “the quick little brown fox”. With a slop of 2 it would match “the brown quick fox”.

query = PhraseQuery.new(:content)
query << "the" << "quick" << "brown" << "fox"

# matches => "the quick brown fox"

query.slop = 1
# matches => "the quick little brown fox"
                           |__1__^

query.slop = 2
# matches => "the brown quick _____ fox"
                    ^_____2_____|

Multi-PhraseQuery

Phrase queries can also have multiple terms in a single position. Let’s say for example that we want to match synonyms for quick like “fast” and “speedy”. You could the query like this;

query = PhraseQuery.new(:content)
query << "the" << ["quick", "fast", "speed"] << ["brown", "red"] << "fox"
# matches => "the quick red fox"
# matches => "the fast brown fox"

query.slop = 1
# matches => "the speedy little red fox"

You can also leave positions blank. Lets say you wanted to match “the quick <> fox” where “<>” could match anything (but not nothing). You’d build this query like this;

query = PhraseQuery.new(:content)
query.add_term("the").add_term("quick").add_term("fox", 2)
# matches => "the quick yellow fox"
# matches => "the quick alkgdhaskghaskjdh fox"

The second parameter to PhraseQuery#add_term is the position increment for the term. It is one by default meaning that every time you add a term it is expected to follow the previous term. But setting it to 2 or greater you are leaving empty spaces in the term.

There are also so tricks you can do by setting the position increment to

  1. With a little help from your analyzer you can actually tag bold or

italic text for example. If you want more information about this, ask on the mailing list.