<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">NAME
    ElasticSearch::SearchBuilder - A Perlish compact query language for
    ElasticSearch

VERSION
    Version 0.08

    Compatible with ElasticSearch version 0.17.5

DESCRIPTION
    The Query DSL for ElasticSearch (see Query DSL
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl&gt;), which is used
    to write queries and filters, is simple but verbose, which can make it
    difficult to write and understand large queries.

    ElasticSearch::SearchBuilder is an SQL::Abstract-like query language
    which exposes the full power of the query DSL, but in a more compact,
    Perlish way.

    This module is currently Beta - the API might yet change. If you have
    suggestions for improvements to the API or the documenation, please
    contact me.

SYNOPSIS
        my $sb = ElasticSearch::SearchBuilder-&gt;new();
        my $query = $sb-&gt;query({
            body    =&gt; 'interesting keywords',
            -filter =&gt; {
                status  =&gt; 'active',
                tags    =&gt; ['perl','python','ruby'],
                created =&gt; {
                    '&gt;=' =&gt; '2010-01-01',
                    '&lt;'  =&gt; '2011-01-01'
                },
            }
        })

    NOTE: "ElasticSearch::SearchBuilder" is fully integrated with the
    ElasticSearch API. Wherever you can specify "query", "filter" or
    "facet_filter" in ElasticSearch, you can automatically use SearchBuilder
    by specifying "queryb", "filterb", "facet_filterb" instead.

        $es-&gt;search( queryb  =&gt; { body =&gt; 'interesting keywords' } )

METHODS
  new()
        my $sb = ElastiSearch::SearchBuilder-&gt;new()

    Creates a new instance of the SearchBuilder - takes no parameters.

  query()
        my $es_query = $sb-&gt;query($compact_query)

    Returns a query in the ElasticSearch query DSL.

    $compact_query can be a scalar, a hash ref or an array ref.

        $sb-&gt;query('foo')
        # { "query" : { "text" : { "_all" : "foo" }}}

        $sb-&gt;query({ ... }) or $sb-&gt;query([ ... ])
        # { "query" : { ... }}

  filter()
        my $es_filter = $sb-&gt;filter($compact_filter)

    Returns a filter in the ElasticSearch query DSL.

    $compact_filter can be a scalar, a hash ref or an array ref.

        $sb-&gt;filter('foo')
        # { "filter" : { "term" : { "_all" : "foo" }}}

        $sb-&gt;filter({ ... }) or $sb-&gt;filter([ ... ])
        # { "filter" : { ... }}

INTRODUCTION
    IMPORTANT: If you are not familiar with ElasticSearch then you should
    read "ELASTICSEARCH CONCEPTS" before continuing.

    This module was inspired by SQL::Abstract but they are not compatible
    with each other.

    The easiest way to explain how the syntax works is to give examples:

  QUERY / FILTER CONTEXT
    There are two contexts:

    *   "filter" context

        Filter are fast and cacheable. They should be used to
        include/exclude docs, based on simple term values. For instance,
        exclude all docs that have neither tag "perl" nor "python".

        Typically, most of your clauses should be filters, which reduce the
        number of docs that need to be passed to the query.

    *   "query" context

        Queries are smarter than filters, but more expensive, as they have
        to calculate search relevance (ie "_score").

        They should be used where:

        *   relevance is important, eg: in a search for tags "perl" or
            "python", a doc that has BOTH tags is more relevant than a doc
            that has only one

        *   where search terms need to be analyzed as full text, eg: find me
            all docs where the "content" field includes the words "Perl is
            GREAT", no matter how those words are capitalized.

    The available operators (and the query/filter clauses that are
    generated) differ according to which context you are in.

    The initial context depends upon which method you use: "query()" puts
    you into "query" context, and "filter()" into "filter" context.

    However, you can switch from one context to another as follows:

        $sb-&gt;query({

            # query context
            foo     =&gt; 1,
            bar     =&gt; 2,

            -filter =&gt; {
                # filter context
                foo     =&gt; 1,
                bar     =&gt; 2,

                -query  =&gt; {
                    # query context
                    foo =&gt; 1
                }
            }
        })

   -filter | -not_filter
    Switch from query context to filter context:

        # query field content for 'brown cow', and filter documents
        # where status is 'active' and tags contains the term 'perl'
        {
            content =&gt; { text =&gt; 'brown cow' },
            -filter =&gt; {
                status =&gt; 'active',
                tags   =&gt; 'perl'
            }
        }


        # no query, just a filter:
        { -filter =&gt; { status =&gt; 'active' }}

    See Filtered Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.h
    tml&gt; and Constant Score Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-q
    uery.html&gt;

   -query | -not_query
    Use a query as a filter:

        # query field content for 'brown cow', and filter documents
        # where status is 'active', tags contains the term 'perl'
        # and a text query on field title contains 'important'
        {
            content =&gt; { text =&gt; 'brown cow' },
            -filter =&gt; {
                status =&gt; 'active',
                tags   =&gt; 'perl',
                -query =&gt; {
                    title =&gt; { text =&gt; 'important' }
                }
            }
        }

    See Query Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.htm
    l&gt;

  KEY-VALUE PAIRS
    Key-value pairs are equivalent to the "=" operator, discussed below.
    They are converted to "text" queries or "term" filters:

        # Field 'foo' contains term 'bar'
        # equiv: { foo =&gt; { '=' =&gt; 'bar' }}
        { foo =&gt; 'bar' }



        # Field 'foo' contains 'bar' or 'baz'
        # equiv: { foo =&gt; { '=' =&gt; ['bar','baz'] }}
        { foo =&gt; ['bar','baz']}


        # Field 'foo' contains terms 'bar' AND 'baz'
        # equiv: { foo =&gt; { '-and' =&gt; [ {'=' =&gt; 'bar'}, {'=' =&gt; 'baz'}] }}
        { foo =&gt; ['-and','bar','baz']}


        ### FILTER ONLY ###

        # Field 'foo' is missing ie has no value
        # equiv: { -missing =&gt; 'foo' }
        { foo =&gt; undef }

  AND|OR LOGIC
    Arrays are OR'ed, hashes are AND'ed:

        # tags = 'perl' AND status = 'active:
        {
            tags   =&gt; 'perl',
            status =&gt; 'active'
        }

        # tags = 'perl' OR status = 'active:
        [
            tags   =&gt; 'perl',
            status =&gt; 'active'
        ]

        # tags = 'perl' or tags = 'python':
        { tags =&gt; [ 'perl','python' ]}
        { tags =&gt; { '=' =&gt; [ 'perl','python' ] }}

        # tags begins with prefix 'p' or 'r'
        { tags =&gt; { '^' =&gt; [ 'p','r' ] }}

    The logic in an array can changed from "OR" to "AND" by making the first
    element of the array ref "-and":

        # tags has term 'perl' AND 'python'

        { tags =&gt; ['-and','perl','python']}

        {
            tags =&gt; [
                -and =&gt; { '=' =&gt; 'perl'},
                        { '=' =&gt; 'python'}
            ]
        }

    However, the first element in an array ref which is used as the value
    for a field operator (see &lt;/"FIELD OPERATORS"&gt;) is not special:

        # WRONG
        { tags =&gt; { '=' =&gt; [ '-and','perl','python' ] }}

        # RIGHT
        { tags =&gt; ['-and' =&gt; [ {'=' =&gt; 'perl'}, {'=' =&gt; 'python'} ] ]}

    ...otherwise you would never be able to search for the term "-and". So
    if you might possibly have the terms "-and" or "-or" in your data, use:

        { foo =&gt; {'=' =&gt; [....] }}

    instead of:

        { foo =&gt; [....]}

   -and | -or | -not
    These unary operators allow you apply "and", "or" and "not" logic to
    nested queries or filters.

        # Field foo has both terms 'bar' and 'baz'
        { -and =&gt; [
                foo =&gt; 'bar',
                foo =&gt; 'baz'
        ]}

        # Field 'name' contains 'john smith', or the name field is missing
        # and the 'desc' field contains 'john smith'

        { -or =&gt; [
            { name =&gt; 'John Smith' },
            {
                desc     =&gt; 'John Smith'
                -filter  =&gt; { -missing =&gt; 'name' },
            }
        ]}

    The "-and", "-or" and "-not" constructs emit "bool" queries when in
    query context, and "and", "or" and "not" clauses when in filter context.

    See also: "NAMED FILTERS", Bool Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html&gt;
    , And Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/and-filter.html&gt;
    , Or Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/or-filter.html&gt;
    and Not Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/not-filter.html&gt;

  FIELD OPERATORS
    Most operators (eg "=", "gt", "geo_distance" etc) are applied to a
    particular field. These are known as "Field Operators". For example:

        # Field foo contains the term 'bar'
        { foo =&gt; 'bar' }
        { foo =&gt; {'=' =&gt; 'bar' }}

        # Field created is between Jan 1 and Dec 31 2010
        { created =&gt; {
            '&gt;='  =&gt; '2010-01-01',
            '&lt;'   =&gt; '2011-01-01'
        }}

        # Field foo contains terms which begin with prefix 'a' or 'b' or 'c'
        { foo =&gt; { '^' =&gt; ['a','b','c' ]}}

    Some field operators are available as symbols (eg "=", "*", "^", "gt")
    and others as words (eg "geo_distance" or "-geo_distance" - the dash is
    optional).

    Multiple field operators can be applied to a single field. Use "{}" to
    imply "this AND that":

        # Field foo has any value from 100 to 200
        { foo =&gt; { gte =&gt; 100, lte =&gt; 200 }}

        # Field foo begins with 'p' but is not python
        { foo =&gt; {
            '^'  =&gt; 'p',
            '!=' =&gt; 'python'
        }}

    Or "[]" to imply "this OR that"

        # foo is 5 or foo greater than 10
        { foo =&gt; [
            { '='  =&gt; 5  },
            { 'gt' =&gt; 10 }
        ]}

    All word operators may be negated by adding "not_" to the beginning, eg:

        # Field foo does NOT contain a term beginning with 'bar' or 'baz'
        { foo =&gt; { not_prefix =&gt; ['bar','baz'] }}

  UNARY OPERATORS
    There are other operators which don't fit this "{ field =&gt; { op =&gt;
    value}}"model.

    For instance:

    *   An operator might apply to multiple fields:

            # Search fields 'title' and 'content' for text 'brown cow'
            {
                -query_string =&gt; {
                    query   =&gt; 'brown cow',
                    fields  =&gt; ['title','content']
                }
            }

    *   The field might BE the value:

            # Find documents where the field 'foo' is blank or undefined
            { -missing =&gt; 'foo' }

            # Find documents where the field 'foo' exists and has a value
            { -exists =&gt; 'foo' }

    *   For combining other queries or filters:

            # Field foo has terms 'bar' and 'baz' but not 'balloo'
            {
                -and =&gt; [
                    foo =&gt; 'bar',
                    foo =&gt; 'baz',
                    -not =&gt; { foo =&gt; 'balloo' }
                ]
            }

    *   Other:

            # Script query
            { -script =&gt; "doc['num1'].value &gt; 1" }

    These operators are called "unary operators" and ALWAYS begin with a
    dash "-" to distinguish them from field names.

    Unary operators may also be prefixed with "not_" to negate their
    meaning.

MATCH ALL
  -all
    The "-all" operator matches all documents:

        # match all
        { -all =&gt; 1  }
        { -all =&gt; 0  }
        { -all =&gt; {} }

    In query context, the "match_all" query usually scores all docs as 1 (ie
    having the same relevance). By specifying a "norms_field", the relevance
    can be read from that field (at the cost of a slower execution time):

        # Query context only
        { -all =&gt;{
            boost       =&gt; 1,
            norms_field =&gt; 'doc_boost'
        }}

EQUALITY
    These operators answer the question: "Does this field contain this
    term?"

    Filter equality operators work only with exact terms, while query
    equality operators (the "text" family of queries) will "do the right
    thing", ie work with terms for "not_analyzed" fields and with analyzed
    text for "analyzed" fields.

  EQUALITY (QUERIES)
   = | -text | != | &lt;&gt; | -not_text
    These operators all generate "text" queries:

        # Analyzed field 'title' contains the terms 'Perl is GREAT'
        # (which is analyzed to the terms 'perl','great')
        { title =&gt; 'Perl is GREAT' }
        { title =&gt; { '='  =&gt; 'Perl is GREAT' }}
        { title =&gt; { text =&gt; 'Perl is GREAT' }}

        # Not_analyzed field 'status' contains the EXACT term 'ACTIVE'
        { status =&gt; 'ACTIVE' }
        { status =&gt; { '='  =&gt; 'ACTIVE' }}
        { status =&gt; { text =&gt; 'ACTIVE' }}

        # Same as above but with extra parameters:
        { title =&gt; {
            text =&gt; {
                query          =&gt; 'Perl is GREAT',
                boost          =&gt; 2.0,
                operator       =&gt; 'and',
                analyzer       =&gt; 'default',
                fuzziness      =&gt; 0.5,
                max_expansions =&gt; 100,
                prefix_length  =&gt; 2,
            }
        }}

    Operators "&lt;&gt;", "!=" and "not_text" are synonyms for each other and just
    wrap the operator in a "not" clause.

    See Text Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/text-query.html&gt;

   == | -phrase | -not_phrase
    These operators look for a complete phrase.

    For instance, given the text

        The quick brown fox jumped over the lazy dog.

        # matches
        { content =&gt; { '==' =&gt; 'Quick Brown' }}

        # doesn't match
        { content =&gt; { '==' =&gt; 'Brown Quick' }}
        { content =&gt; { '==' =&gt; 'Quick Fox'   }}

    The "slop" parameter can be used to allow the phrase to match words in
    the same order, but further apart:

        # with other parameters
        { content =&gt; {
            phrase =&gt; {
                query    =&gt; 'Quick Fox',
                slop     =&gt; 3,
                analyzer =&gt; 'default'
                boost    =&gt; 1,
        }}

    See Text Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/text-query.html&gt;

   -term | -terms | -not_term | -not_terms
    The "term"/"terms" operators are provided for completeness. You should
    almost always use the "text"/"=" operator instead.

    There are only two use cases:

    *   To find the exact (ie not analyzed) term 'foo' in an analyzed field:

            { title =&gt; { term =&gt; 'foo' }}

    *   To match a list of possible terms, where more than 1 value must
        match:

            # match 2 or more of these tags
            { tags =&gt; {
                terms =&gt; {
                    value         =&gt; ['perl','python','php'],
                    minimum_match =&gt; 2,
                    boost         =&gt; 1,
                }
            }}

        The above can also be achieved with the "-bool" operator.

    "term" and "terms" are synonyms, as are "not_term" and "not_terms".

  EQUALITY (FILTERS)
   = | -term | -terms | &lt;&gt; | != | -not_term | -not_terms
    These operators result in "term" or "terms" filters, which look for
    fields which contain exactly the terms specified:

        # Field foo has the term 'bar':
        { foo =&gt; 'bar' }
        { foo =&gt; { '='    =&gt; 'bar' }}
        { foo =&gt; { 'term' =&gt; 'bar' }}

        # Field foo has the term 'bar' or 'baz'
        { foo =&gt; ['bar','baz'] }
        { foo =&gt; { '='     =&gt; ['bar','baz'] }}
        { foo =&gt; { 'term'  =&gt; ['bar','baz'] }}

    "&lt;&gt;" and "!=" are synonyms:

        # Field foo does not contain the term 'bar':
        { foo =&gt; { '!=' =&gt; 'bar' }}
        { foo =&gt; { '&lt;&gt;' =&gt; 'bar' }}

        # Field foo contains neither 'bar' nor 'baz'
        { foo =&gt; { '!=' =&gt; ['bar','baz'] }}
        { foo =&gt; { '&lt;&gt;' =&gt; ['bar','baz'] }}

    See Term Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/term-filter.html
    &gt; and Terms Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.htm
    l&gt;

RANGES
  lt | gt | lte | gte | &lt; | &lt;= | &gt;= | &gt; | -range | -not_range
    These operators imply a range query or filter, which can be numeric or
    alphabetical.

        # Field foo contains terms between 'alpha' and 'beta'
        { foo =&gt; {
            'gte'   =&gt; 'alpha',
            'lte'   =&gt; 'beta'
        }}

        # Field foo contains numbers between 10 and 20
        { foo =&gt; {
            'gte'   =&gt; '10',
            'lte'   =&gt; '20'
        }}

        # boost a range  *** query only ***
        { foo =&gt; {
            range =&gt; {
                gt      =&gt; 5,
                gte     =&gt; 5,
                lt      =&gt; 10,
                lte     =&gt; 10,
                boost   =&gt; 2.0
            }
        }}

    For queries, "&lt;" is a synonym for "lt", "&gt;" for "gt" etc.

    See Range Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/range-query.html
    &gt;

    Note: for filter clauses, the "gt","gte","lt" and "lte" operators imply
    a "range" filter, while the "&lt;", "&lt;=", "&gt;" and "&gt;=" operators imply a
    "numeric_range" filter.

    This does not mean that you should use the "numeric_range" version for
    any field which contains numbers!

    The "numeric_range" filter should be used for numbers/datetimes which
    have many distinct values, eg "ID" or "last_modified". If you have a
    numeric field with few distinct values, eg "number_of_fingers" then it
    is better to use a "range" filter.

    See Range Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/range-filter.htm
    l&gt; and Numeric Range Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/numeric-range-fi
    lter.html&gt;.

MISSING OR NULL VALUES
    *** Filter context only ***

  -missing | -exists
    You can use a "missing" or "exists" filter to select only docs where a
    particular field exists and has a value, or is undefined or has no
    value:

        # Field 'foo' has a value:
        { foo     =&gt; { exists  =&gt; 1 }}
        { foo     =&gt; { missing =&gt; 0 }}
        { -exists =&gt; 'foo'           }

        # Field 'foo' is undefined or has no value:
        { foo      =&gt; { missing =&gt; 1 }}
        { foo      =&gt; { exists  =&gt; 0 }}
        { -missing =&gt; 'foo'           }
        { foo      =&gt; undef           }

    See Missing Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.h
    tml&gt; and Exists Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/exists-filter.ht
    ml&gt;

FULL TEXT SEARCH
    *** Query context only ***

    For most full text search queries, the "text" queries are what you want.
    These analyze the search terms, and look for documents that contain one
    or more of those terms. (See "EQUALITY (QUERIES)").

  -qs | -query_string | -not_qs | -not_query_string
    However, there is a more advanced query string syntax (see Lucene Query
    Parser Syntax
    &lt;http://lucene.apache.org/java/3_3_0/queryparsersyntax.html&gt;) which
    understands search terms like:

       perl AND python tag:recent "must have this phrase" -apple

    It is useful for "power" users, but has the disadvantage that, if the
    syntax is incorrect, ES throws an error. You can use
    ElasticSearch::QueryParser to fix any syntax errors.

        # find docs whose 'title' field matches 'this AND that'
        { title =&gt; { qs           =&gt; 'this AND that' }}
        { title =&gt; { query_string =&gt; 'this AND that' }}

        # With other parameters
        { title =&gt; {
            field =&gt; {
                query                        =&gt; 'this that ',
                default_operator             =&gt; 'AND',
                analyzer                     =&gt; 'default',
                allow_leading_wildcard       =&gt; 0,
                lowercase_expanded_terms     =&gt; 1,
                enable_position_increments   =&gt; 1,
                fuzzy_prefix_length          =&gt; 2,
                fuzzy_min_sim                =&gt; 0.5,
                phrase_slop                  =&gt; 10,
                boost                        =&gt; 2,
                analyze_wildcard             =&gt; 1,
                auto_generate_phrase_queries =&gt; 0,
                rewrite                      =&gt; 'constant_score_default',
            }
        }}

    The unary form "-qs" or "-query_string" can be used when matching
    against multiple fields:

        { -qs =&gt; {
                query                        =&gt; 'this AND that ',
                fields                       =&gt; ['title','content'],
                default_operator             =&gt; 'AND',
                analyzer                     =&gt; 'default',
                allow_leading_wildcard       =&gt; 0,
                lowercase_expanded_terms     =&gt; 1,
                enable_position_increments   =&gt; 1,
                fuzzy_prefix_length          =&gt; 2,
                fuzzy_min_sim                =&gt; 0.5,
                phrase_slop                  =&gt; 10,
                boost                        =&gt; 2,
                analyze_wildcard             =&gt; 1,
                auto_generate_phrase_queries =&gt; 0,
                use_dis_max                  =&gt; 1,
                tie_breaker                  =&gt; 0.7
        }}

    See Query-string Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/query-string-que
    ry.html&gt;

  -mlt | -not_mlt
    An "mlt" or "more_like_this" query finds documents that are "like" the
    specified text, where "like" means that it contains some or all of the
    specified terms.

        # Field foo is like "brown cow"
        { foo =&gt; { mlt =&gt; "brown cow" }}

        # With other paramters:
        { foo =&gt; {
            mlt =&gt; {
                like_text               =&gt; 'brown cow',
                percent_terms_to_match  =&gt; 0.3,
                min_term_freq           =&gt; 2,
                max_query_terms         =&gt; 25,
                stop_words              =&gt; ['the','and'],
                min_doc_freq            =&gt; 5,
                max_doc_freq            =&gt; 1000,
                min_word_len            =&gt; 0,
                max_word_len            =&gt; 20,
                boost_terms             =&gt; 2,
                boost                   =&gt; 2.0,
                analyzer                =&gt; 'default'
            }
        }}

        # multi fields
        { -mlt =&gt; {
            like_text               =&gt; 'brown cow',
            fields                  =&gt; ['title','content']
            percent_terms_to_match  =&gt; 0.3,
            min_term_freq           =&gt; 2,
            max_query_terms         =&gt; 25,
            stop_words              =&gt; ['the','and'],
            min_doc_freq            =&gt; 5,
            max_doc_freq            =&gt; 1000,
            min_word_len            =&gt; 0,
            max_word_len            =&gt; 20,
            boost_terms             =&gt; 2,
            boost                   =&gt; 2.0,
            analyzer                =&gt; 'default'
        }}

    See MLT Field Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/mlt-field-query.
    html&gt; and MLT Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/mlt-query.html&gt;

  -flt | -not_flt
    An "flt" or "fuzzy_like_this" query fuzzifies all specified terms, then
    picks the best "max_query_terms" differentiating terms. It is a
    combination of "fuzzy" with "more_like_this".

        # Field foo is fuzzily similar to "brown cow"
        { foo =&gt; { flt =&gt; 'brown cow }}

        # With other parameters:
        { foo =&gt; {
            flt =&gt; {
                like_text       =&gt; 'brown cow',
                ignore_tf       =&gt; 0,
                max_query_terms =&gt; 10,
                min_similarity  =&gt; 0.5,
                prefix_length   =&gt; 3,
                boost           =&gt; 2.0,
                analyzer        =&gt; 'default'
            }
        }}

        # Multi-field
        flt =&gt; {
            like_text       =&gt; 'brown cow',
            fields          =&gt; ['title','content'],
            ignore_tf       =&gt; 0,
            max_query_terms =&gt; 10,
            min_similarity  =&gt; 0.5,
            prefix_length   =&gt; 3,
            boost           =&gt; 2.0,
            analyzer        =&gt; 'default'
        }}

    See FLT Field Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/flt-field-query.
    html&gt; and FLT Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/flt-query.html&gt;

PREFIX
  PREFIX (QUERIES)
   ^ | -phrase_prefix | -not_phrase_prefix
    These operators use the "text_phrase_prefix" query.

    For "analyzed" fields, it analyzes the search terms, and does a
    "text_phrase" query, with a "prefix" query on the last term. Think
    "auto-complete".

    For "not_analyzed" fields, this behaves the same as the term-based
    "prefix" query.

    For instance, given the phrase "The quick brown fox jumped over the lazy
    dog":

        # matches
        { content =&gt; { '^'             =&gt; 'qui'}}
        { content =&gt; { '^'             =&gt; 'quick br'}}
        { content =&gt; { 'phrase_prefix' =&gt; 'quick brown f'}}

        # doesn't match
        { content =&gt; { '^'             =&gt; 'quick fo' }}
        { content =&gt; { 'phrase_prefix' =&gt; 'fox brow'}}

    With extra options

        { content =&gt; {
            phrase_prefix =&gt; {
                query          =&gt; "Brown Fo",
                slop           =&gt; 3,
                analyzer       =&gt; 'default',
                boost          =&gt; 3.0,
                max_expansions =&gt; 100,
            }
        }}

    See
    http://www.elasticsearch.org/guide/reference/query-dsl/text-query.html

   -prefix | -not_prefix
    The "prefix" query is a term-based query - no analysis takes place, even
    on analyzed fields. Generally you should use "^" instead.

        # Field 'lang' contains terms beginning with 'p'
        { lang =&gt; { prefix =&gt; 'p' }}

        # With extra options
        { lang =&gt; {
            'prefix' =&gt; {
                value   =&gt; 'p',
                boost   =&gt; 2,
                rewrite =&gt; 'constant_score_default',

            }
        }}

    See Prefix Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/prefix-query.htm
    l&gt;.

  PREFIX (FILTERS)
   ^ | -prefix | -not_prefix
        # Field foo contains a term which begins with 'bar'
        { foo =&gt; { '^'      =&gt; 'bar' }}
        { foo =&gt; { 'prefix' =&gt; 'bar' }}

        # Field foo contains a term which begins with 'bar' or 'baz'
        { foo =&gt; { '^'      =&gt; ['bar','baz'] }}
        { foo =&gt; { 'prefix' =&gt; ['bar','baz'] }}

        # Field foo contains a term which begins with neither 'bar' nor 'baz'
        { foo =&gt; { 'not_prefix' =&gt; ['bar','baz'] }}

    See Prefix Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/prefix-filter.ht
    ml&gt;

WILDCARD AND FUZZY QUERIES
    *** Query context only ***

  * | -wildcard | -not_wildcard
    A "wildcard" is a term-based query (no analysis is applied), which does
    shell globbing to find matching terms. In other words "?" represents any
    single character, while "*" represents zero or more characters.

        # Field foo matches 'f?ob*'
        { foo =&gt; { '*'        =&gt; 'f?ob*' }}
        { foo =&gt; { 'wildcard' =&gt; 'f?ob*' }}

        # with a boost:
        { foo =&gt; {
            '*' =&gt; { value =&gt; 'f?ob*', boost =&gt; 2.0 }
        }}
        { foo =&gt; {
            'wildcard' =&gt; {
                value   =&gt; 'f?ob*',
                boost   =&gt; 2.0,
                rewrite =&gt; 'constant_score_default',
            }
        }}

    See Wildcard Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/wildcard-query.h
    tml&gt;

  -fuzzy | -not_fuzzy
    A "fuzzy" query is a term-based query (ie no analysis is done) which
    looks for terms that are similar to the the provided terms, where
    similarity is based on the Levenshtein (edit distance) algorithm:

        # Field foo is similar to 'fonbaz'
        { foo =&gt; { fuzzy =&gt; 'fonbaz' }}

        # With other parameters:
        { foo =&gt; {
            fuzzy =&gt; {
                value           =&gt; 'fonbaz',
                boost           =&gt; 2.0,
                min_similarity  =&gt; 0.2,
                max_expansions  =&gt; 10
            }
        }}

    Normally, you should rather use either the "EQUALITY" queries with the
    "fuzziness" parameter, or the -flt queries.

    See Fuzzy Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/fuzzy-query.html
    &gt;.

COMBINING QUERIES
    *** Query context only ***

    These constructs allow you to combine multiple queries.

  -dis_max | -dismax
    While a "bool" query adds together the scores of the nested queries, a
    "dis_max" query uses the highest score of any matching queries.

        # Run the two queries and use the best score
        { -dismax =&gt; [
            { foo =&gt; 'bar' },
            { foo =&gt; 'baz' }
        ] }

        # With other parameters
        { -dismax =&gt; {
            queries =&gt; [
                { foo =&gt; 'bar' },
                { foo =&gt; 'baz' }
            ],
            tie_breaker =&gt; 0.5,
            boost =&gt; 2.0
        ] }

    See DisMax Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/dis-max-query.ht
    ml&gt;

  -bool
    Normally, there should be no need to use a "bool" query directly, as
    these are autogenerated from eg "-and", "-or" and "-not" constructs.
    However, if you need to pass any of the other parameters to a "bool"
    query, then you can do the following:

        {
           -bool =&gt; {
               must          =&gt; [{ foo =&gt; 'bar' }],
               must_not      =&gt; { status =&gt; 'inactive' },
               should        =&gt; [
                    { tag    =&gt; 'perl'   },
                    { tag    =&gt; 'python' },
                    { tag    =&gt; 'ruby' },
               ],
               minimum_number_should_match =&gt; 2,
               disable_coord =&gt; 1,
               boost         =&gt; 2
           }
        }

    See Bool Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html&gt;

  -boosting
    The "boosting" query can be used to "demote" results that match a given
    query. Unlike the "must_not" clause of a "bool" query, the query still
    matches, but the results are "less relevant".

        { -boosting =&gt; {
            positive       =&gt; { title =&gt; { text =&gt; 'apple pear'     }},
            negative       =&gt; { title =&gt; { text =&gt; 'apple computer' }},
            negative_boost =&gt; 0.2
        }}

    See Boosting Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/boosting-query.h
    tml&gt;

NESTED QUERIES/FILTERS
    Nested queries/filters allow you to run queries/filters on nested docs.

    Normally, a doc like this would not allow you to associate the name
    "perl" with the number 5

       {
           title:  "my title",
           tags: [
            { name: "perl",   num: 5},
            { name: "python", num: 2}
           ]
       }

    However, if "tags" is mapped as a "nested" field, then you can run
    queries or filters on each sub-doc individually.

    See Nested Mapping
    &lt;http://github.com/elasticsearch/elasticsearch/issues/1095&gt;

  -nested (QUERY)
        {
            -nested =&gt; {
                path        =&gt; 'tags',
                score_mode  =&gt; 'avg',
                _scope      =&gt; 'my_tags',
                query       =&gt; {
                    tags.name    =&gt; 'perl',
                    tags.num     =&gt; { gt =&gt; 2},
                }
            }
        }

    See Nested Query
    &lt;http://github.com/elasticsearch/elasticsearch/issues/1095&gt;

  -nested (FILTER)
        {
            -nested =&gt; {
                path        =&gt; 'tags',
                score_mode  =&gt; 'avg',
                _cache      =&gt; 1,
                _name       =&gt; 'my_filter',
                filter      =&gt; {
                    tags.name    =&gt; 'perl',
                    tags.num     =&gt; { gt =&gt; 2},
                }
            }
        }

    See Nested Filter
    &lt;http://github.com/elasticsearch/elasticsearch/issues/1102&gt;

SCRIPTING
    ElasticSearch supports the use of scripts to customise query or filter
    behaviour. By default the query language is "mvel" but javascript,
    groovy, python and native java scripts are also supported.

    See Scripting
    &lt;http://www.elasticsearch.org/guide/reference/modules/scripting.html&gt;
    for more on scripting.

  -custom_score
    *** Query context only ***

    The "-custom_score" query allows you to customise the "_score" or
    relevance (and thus the order) of docs returned from a query.

        {
            -custom_score =&gt; {
                query  =&gt; { foo =&gt; 'bar' },
                lang    =&gt; 'mvel',
                script =&gt; "_score * doc['my_numeric_field'].value / pow(param1, param2)"
                params =&gt; {
                    param1 =&gt; 2,
                    param2 =&gt; 3.1
                },
            }
        }

    See Custom Score Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-que
    ry.html&gt;

  -custom_filters_score
    *** Query context only ***

    The "-custom_filters_score" query allows you to boost documents that
    match a filter, either with a "boost" parameter, or with a custom
    "script".

    This is a very powerful and efficient way to boost results which depend
    on matching unanalyzed fields, eg a "tag" or a "date". Also, these
    filters can be cached.

        {
            -custom_filters_score =&gt; {
                query       =&gt; { foo =&gt; 'bar' },
                score_mode  =&gt; 'first|max|total|avg',       # default 'first',
                filters     =&gt; [
                    {
                        filter =&gt; { tag =&gt; 'perl' },
                        boost  =&gt; 2,
                    },
                    {
                        filter =&gt; { tag =&gt; 'python' },
                        script =&gt; '_score * my_boost',
                        params =&gt; { my_boost =&gt; 2},
                        lang   =&gt; 'mvel'
                    },
                ]
            }
        }

    See Custom Filters Score Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-s
    core-query.html&gt;

  -script
    *** Filter context only ***

    The "-script" filter allows you to use a script as a filter. Return a
    true value to indicate that the filter matches.

        # Filter docs whose field 'foo' is greater than 5
        { -script =&gt; "doc['foo'].value &gt; 5 " }

        # With other params
        {
            -script =&gt; {
                script =&gt; "doc['foo'].value &gt; minimum ",
                params =&gt; { minimum =&gt; 5 },
                lang   =&gt; 'mvel'
            }
        }

    See Script Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/script-filter.ht
    ml&gt;

PARENT/CHILD
    Documents stored in ElasticSearch can be configured to have parent/child
    relationships.

    See Parent Field
    &lt;http://www.elasticsearch.org/guide/reference/mapping/parent-field.html&gt;
    for more.

  -has_child | -not_has_child
    Find parent documents that have child documents which match a query.

        # Find parent docs whose children of type 'comment' have the tag 'perl'
        {
            -has_child =&gt; {
                type   =&gt; 'comment',
                query  =&gt; { tag =&gt; 'perl' },
                _scope =&gt; 'my_scope',
                boost  =&gt; 1,                    # Query context only
            }
        }

    See Has Child Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/has-child-query.
    html&gt; and See Has Child Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter
    .html&gt;.

  -top_children
    *** Query context only ***

    The "top_children" query runs a query against the child docs, and
    aggregates the scores to find the parent docs whose children best match.

        {
            -top_children =&gt; {
                type                =&gt; 'blog_tag',
                query               =&gt; { tag =&gt; 'perl' },
                score               =&gt; 'max',
                factor              =&gt; 5,
                incremental_factor  =&gt; 2,
                _scope              =&gt; 'my_scope'
            }
        }

    See Top Children Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/top-children-que
    ry.html&gt;

GEO FILTERS
    For all the geo filters, the "normalize" parameter defaults to "true",
    meaning that the longitude value will be normalized to -180 to 180 and
    the latitude value to -90 to 90.

  -geo_distance | -not_geo_distance
    *** Filter context only ***

    The "geo_distance" filter will find locations within a certain distance
    of a given point:

        {
            my_location =&gt; {
                -geo_distance     =&gt; {
                    location      =&gt; { lat =&gt; 10, lon =&gt; 5 },
                    distance      =&gt; '5km',
                    normalize     =&gt; 1 | 0,
                    optimize_bbox =&gt; 1 | 0,
                }
            }
        }

    See Geo Distance Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-fil
    ter.html&gt;

  -geo_distance_range | -not_geo_distance_range
    *** Filter context only ***

    The "geo_distance_range" filter is similar to the -geo_distance filter,
    but expressed as a range:

        {
            my_location =&gt; {
                -geo_distance       =&gt; {
                    location        =&gt; { lat =&gt; 10, lon =&gt; 5 },
                    from            =&gt; '5km',
                    to              =&gt; '10km',
                    include_lower   =&gt; 1 | 0,
                    include_upper   =&gt; 0 | 1
                    normalize       =&gt; 1 | 0,
                    optimize_bbox   =&gt; 1 | 0,
                }
            }
        }

    or instead of "from", "to", "include_lower" and "include_upper" you can
    use "gt", "gte", "lt", "lte".

    See Geo Distance Range Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-ran
    ge-filter.html&gt;

  -geo_bounding_box | -geo_bbox | -not_geo_bounding_box | -not_geo_bbox
    *** Filter context only ***

    The "geo_bounding_box" filter finds points which lie within the given
    rectangle:

        {
            my_location =&gt; {
                -geo_bbox =&gt; {
                    top_left     =&gt; { lat =&gt; 9, lon =&gt; 4  },
                    bottom_right =&gt; { lat =&gt; 10, lon =&gt; 5 },
                    normalize    =&gt; 1 | 0
                }
            }
        }

    See Geo Bounding Box Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/geo-bounding-box
    -filter.html&gt;

  -geo_polygon | -not_geo_polygon
    *** Filter context only ***

    The "geo_polygon" filter is similar to the -geo_bounding_box filter,
    except that it allows you to specify a polygon instead of a rectangle:

        {
            my_location =&gt; {
                -geo_polygon =&gt; [
                    { lat =&gt; 40, lon =&gt; -70 },
                    { lat =&gt; 30, lon =&gt; -80 },
                    { lat =&gt; 20, lon =&gt; -90 },
                ]
            }
        }

    or:

        {
            my_location =&gt; {
                -geo_polygon =&gt; {
                    points  =&gt; [
                        { lat =&gt; 40, lon =&gt; -70 },
                        { lat =&gt; 30, lon =&gt; -80 },
                        { lat =&gt; 20, lon =&gt; -90 },
                    ],
                    normalize =&gt; 1 | 0,
                }
            }
        }

    See Geo Polygon Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/geo-polygon-filt
    er.html&gt;

TYPE/ID
    The "_id" field is not indexed by default, and thus isn't available for
    normal queries or filters.

  -ids
    Returns docs with the matching "_id" or "_type"/"_id" combination:

        # doc with ID 123
        { -ids =&gt; 123 }

        # docs with IDs 123 or 124
        { -ids =&gt; [123,124] }

        # docs of types 'blog' or 'comment' with IDs 123 or 124
        {
            -ids =&gt; {
                type    =&gt; ['blog','comment'],
                values  =&gt; [123,124]

            }
        }

    See IDs Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/ids-query.html&gt;
    abd IDs Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/ids-filter.html&gt;

  -type
    *** Filter context only ***

    Filters docs with matching "_type" fields.

    While the "_type" field is indexed by default, ElasticSearch provides
    the "type" filter which will work even if indexing of the "_type" field
    is disabled.

        # Filter docs of type 'comment'
        { -type =&gt; 'comment' }

        # Filter docs of type 'comment' or 'blog'
        { -type =&gt; ['blog','comment' ]}

    See Type Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/type-filter.html
    &gt;

LIMIT
    *** Filter context only ***

    The "limit" filter limits the number of documents (per shard) to execute
    on:

        {
            name    =&gt; { text =&gt; 'Joe Bloggs },
            -filter =&gt; { -limit =&gt; 100       }
        }

    See Limit Filter
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/limit-filter.htm
    l&gt;

NAMED FILTERS
    ElasticSearch allows you to name filters, in which each search result
    will include a "matched_filters" array containing the names of all
    filters that matched.

  -name | -not_name
    *** Filter context only ***

        { -name =&gt; {
            popular   =&gt; { user_rank =&gt; { 'gte' =&gt; 10 }},
            unpopular =&gt; { user_rank =&gt; { 'lt'  =&gt; 10 }},
        }}

    Multiple filters are joined with an "or" filter (as it doesn't make
    sense to join them with "and").

    See Named Filters
    &lt;http://www.elasticsearch.org/guide/reference/api/search/named-filters.h
    tml&gt; and "-and | -or | -not".

CACHING FILTERS
    Part of the performance boost that you get when using filters comes from
    the ability to cache the results of those filters. However, it doesn't
    make sense to cache all filters by default.

  -cache | -nocache
    *** Filter context only ***

    If you would like to override the default caching, then you can use
    "-cache" or "-nocache":

        # Don't cache the term filter for 'status'
        {
            content =&gt; { text =&gt; 'interesting post'},
            -filter =&gt; {
                -nocache =&gt; { status =&gt; 'active' }
            }
        }

        # Do cache the numeric range filter:
        {
            content =&gt; { text =&gt; 'interesting post'},
            -filter =&gt; {
                -cache =&gt; { created =&gt; {'&gt;' =&gt; '2010-01-01' } }
            }
        }

    See Query DSL &lt;http://www.elasticsearch.org/guide/reference/query-dsl/&gt;
    for more details about what is cached by default and what is not.

ELASTICSEARCH CONCEPTS
  FILTERS VS QUERIES
    ElasticSearch supports filters and queries:

    *   A filter just answers the question: "Does this field match? Yes/No",
        eg:

        *   Does this document have the tag "beta"?

        *   Was this document published in 2011?

    *   A query is used to calculate relevance ( known in ElasticSearch as
        "_score"):

        *   Give me all documents that include the keywords "Foo" and "Bar"
            and rank them in order of relevance.

        *   Give me all documents whose "tag" field contains "perl" or
            "ruby" and rank documents that contain BOTH tags more highly.

    Filters are lighter and faster, and the results can often be cached, but
    they don't contribute to the "_score" in any way.

    Typically, most of your clauses will be filters, and just a few will be
    queries.

  TERMS VS TEXT
    All data is stored in ElasticSearch as a "term", which is an exact
    value. The term "Foo" is not the same as "foo".

    While this is useful for fields that have discreet values (eg "active",
    "inactive"), it is not sufficient to support full text search.

    ElasticSearch has to *analyze* text to convert it into terms. This
    applies both to the text that the stored document contains, and to the
    text that the user tries to search on.

    The default analyzer will:

    *   split the text on (most) punctuation and remove that punctuation

    *   lowercase each word

    *   remove English stopwords

    For instance, "The 2 GREATEST widgets are foo-bar and fizz_buzz" would
    result in the terms " [2,'greatest','widgets','foo','bar','fizz_buzz']".

    It is important that the same analyzer is used both for the stored text
    and for the search terms, otherwise the resulting terms may be
    different, and the query won't succeed.

    For instance, a "term" query for "GREATEST" wouldn't work, but
    "greatest" would work. However, a "text" query for "GREATEST" would
    work, because the search text would be analyzed to produce the same
    terms that are stored in the index.

    See Analysis
    &lt;http://www.elasticsearch.org/guide/reference/index-modules/analysis/&gt;
    for the list of supported analyzers.

  "text" QUERIES
    ElasticSearch has a family of DWIM queries called "text" queries.

    Their action depends upon how the field has been defined. If a field is
    "analyzed" (the default for string fields) then the "text" queries
    analyze the search terms before doing the search:

        # Convert "Perl is GREAT" to the terms 'perl','great' and search
        # the 'content' field for those terms

        { text: { content: "Perl is GREAT" }}

    If a field is "not_analyzed", then it treats the search terms as a
    single term:

        # Find all docs where the 'status' field contains EXACTLY the term 'ACTIVE'
        { text: { status: "ACTIVE" }}

    Filters, on the other hand, don't have "text" queries - filters operate
    on simple terms instead.

    See Text Query
    &lt;http://www.elasticsearch.org/guide/reference/query-dsl/text-query.html&gt;
    for more about text queries.

AUTHOR
    Clinton Gormley, "&lt;drtech at cpan.org&gt;"

BUGS
    This is a beta module, so the API may well change in the future.

    If you have any suggestions for improvements, or find any bugs, please
    report them to
    &lt;https://github.com/clintongormley/ElasticSearch-SearchBuilder/issues&gt;.
    I will be notified, and then you'll automatically be notified of
    progress on your bug as I make changes.

TODO
    Add support for "span" queries.

SUPPORT
    You can find documentation for this module with the perldoc command.

        perldoc ElasticSearch::SearchBuilder

    You can also look for information at: &lt;http://www.elasticsearch.org&gt;

ACKNOWLEDGEMENTS
    Thanks to SQL::Abstract for providing the inspiration and some of the
    internals.

LICENSE AND COPYRIGHT
    Copyright 2011 Clinton Gormley.

    This program is free software; you can redistribute it and/or modify it
    under the terms of either: the GNU General Public License as published
    by the Free Software Foundation; or the Artistic License.

    See &lt;http://dev.perl.org/licenses/&gt; for more information.

</pre></body></html>