Class: Ferret::Search::PrefixQuery

Summary

A prefix query is like a TermQuery except that it matches any term with a specific prefix. PrefixQuery is expanded into a MultiTermQuery when submitted in a search.

Example

PrefixQuery is very useful for matching a tree structure category hierarchy. For example, let‘s say you have the categories;

  "cat1/"
  "cat1/sub_cat1"
  "cat1/sub_cat2"
  "cat2"
  "cat2/sub_cat1"
  "cat2/sub_cat2"

Lets say you want to match everything in category 2. You‘d build the query like this;

  query = PrefixQuery.new(:category, "cat2")
  # matches => "cat2"
  # matches => "cat2/sub_cat1"
  # matches => "cat2/sub_cat2"

Public Class Methods


PrefixQuery.new(field, prefix, options = {}) → prefix-query

Create a new PrefixQuery to search for all terms with the prefix prefix in the field field. There is one option that you can set to change the behaviour of this query. +:max_terms+ specifies the maximum number of terms to be added to the query when it is expanded into a MultiTermQuery. Let‘s say for example you search an index with a million terms for all terms beginning with the letter "s". You would end up with a very large query which would use a lot of memory and take a long time to get results, not to mention that it would probably match every document in the index. To prevent queries like this crashing your application you can set +:max_terms+ which limits the number of terms that get added to the query. By default it is set to 512.

/*
 *  call-seq:
 *     PrefixQuery.new(field, prefix, options = {}) -> prefix-query
 *
 *  Create a new PrefixQuery to search for all terms with the prefix +prefix+
 *  in the field +field+. There is one option that you can set to change the
 *  behaviour of this query. +:max_terms+ specifies the maximum number of
 *  terms to be added to the query when it is expanded into a MultiTermQuery.
 *  Let's say for example you search an index with a million terms for all
 *  terms beginning with the letter "s". You would end up with a very large
 *  query which would use a lot of memory and take a long time to get results,
 *  not to mention that it would probably match every document in the index.
 *  To prevent queries like this crashing your application you can set
 *  +:max_terms+ which limits the number of terms that get added to the query.
 *  By default it is set to 512.
 */
static VALUE
frt_prq_init(int argc, VALUE *argv, VALUE self)
{
    return frt_mtq_init_specific(argc, argv, self, &prefixq_new);
}