Nick Wellnhofer > Lucy > Lucy::Search::QueryParser

Download:
Lucy-0.5.1.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.005001   Source  

NAME ^

Lucy::Search::QueryParser - Transform a string into a Query object.

SYNOPSIS ^

    my $query_parser = Lucy::Search::QueryParser->new(
        schema => $searcher->get_schema,
        fields => ['body'],
    );
    my $query = $query_parser->parse( $query_string );
    my $hits  = $searcher->hits( query => $query );

DESCRIPTION ^

QueryParser accepts search strings as input and produces Query objects, suitable for feeding into IndexSearcher and other Searcher subclasses.

The following syntactical constructs are recognized by QueryParser:

Additionally, the following syntax can be enabled via set_heed_colons():

CONSTRUCTORS ^

new

    my $query_parser = Lucy::Search::QueryParser->new(
        schema         => $searcher->get_schema,    # required
        analyzer       => $analyzer,                # overrides schema
        fields         => ['bodytext'],             # default: indexed fields
        default_boolop => 'AND',                    # default: 'OR'
    );

Constructor.

METHODS ^

parse

    my $query = $query_parser->parse($query_string);
    my $query = $query_parser->parse();  # default: undef

Build a Query object from the contents of a query string. At present, implemented internally by calling tree(), expand(), and prune().

Returns: a Query.

tree

    my $query = $query_parser->tree($query_string);

Parse the logical structure of a query string, building a tree comprised of Query objects. Leaf nodes in the tree will most often be LeafQuery objects but might be MatchAllQuery or NoMatchQuery objects as well. Internal nodes will be objects which subclass PolyQuery: ANDQuery, ORQuery, NOTQuery, and RequiredOptionalQuery.

The output of tree() is an intermediate form which must be passed through expand() before being used to feed a search.

Returns: a Query.

expand

    my $query = $query_parser->expand($query);

Walk the hierarchy of a Query tree, descending through all PolyQuery nodes and calling expand_leaf() on any LeafQuery nodes encountered.

Returns: A Query – usually the same one that was supplied after in-place modification, but possibly another.

expand_leaf

    my $query = $query_parser->expand_leaf($query);

Convert a LeafQuery into either a TermQuery, a PhraseQuery, or an ORQuery joining multiple TermQueries/PhraseQueries to accommodate multiple fields. LeafQuery text will be passed through the relevant Analyzer for each field. Quoted text will be transformed into PhraseQuery objects. Unquoted text will be converted to either a TermQuery or a PhraseQuery depending on how many tokens are generated.

Returns: A Query.

prune

    my $query = $query_parser->prune($query);
    my $query = $query_parser->prune();  # default: undef

Prevent certain Query structures from returning too many results. Query objects built via tree() and expand() can generate “return the world” result sets, such as in the case of NOT a_term_not_in_the_index; prune() walks the hierarchy and eliminates such branches.

     'NOT foo'               => [NOMATCH]
     'foo OR NOT bar'        => 'foo'
     'foo OR (-bar AND -baz) => 'foo'

prune() also eliminates some double-negative constructs – even though such constructs may not actually return the world:

     'foo AND -(-bar)'      => 'foo'

In this example, safety is taking precedence over logical consistency. If you want logical consistency instead, call tree() then expand(), skipping prune().

Returns: a Query; in most cases, the supplied Query after in-place modification.

make_term_query

    my $query = $query_parser->make_term_query(
        field => $field  # required
        term  => $term   # required
    );

Factory method creating a TermQuery.

Returns: A Query.

make_phrase_query

    my $query = $query_parser->make_phrase_query(
        field => $field  # required
        terms => $terms  # required
    );

Factory method creating a PhraseQuery.

Returns: A Query.

make_or_query

    my $query = $query_parser->make_or_query($children);
    my $query = $query_parser->make_or_query();  # default: undef

Factory method creating an ORQuery.

Returns: A Query.

make_and_query

    my $query = $query_parser->make_and_query($children);
    my $query = $query_parser->make_and_query();  # default: undef

Factory method creating an ANDQuery.

Returns: A Query.

make_not_query

    my $query = $query_parser->make_not_query($negated_query);

Factory method creating a NOTQuery.

Returns: A Query.

make_req_opt_query

    my $query = $query_parser->make_req_opt_query(
        required_query => $required_query  # required
        optional_query => $optional_query  # required
    );

Factory method creating a RequiredOptionalQuery.

Returns: A Query.

set_heed_colons

    $query_parser->set_heed_colons($heed_colons);

Enable/disable parsing of fieldname:foo constructs.

INHERITANCE ^

Lucy::Search::QueryParser isa Clownfish::Obj.

syntax highlighting: