Span queries

Span queries are low-level positional queries which provide expert control over the order and proximity of the specified terms. These are typically used to implement very specific queries on legal documents or patents.

Span queries cannot be mixed with non-span queries (with the exception of the span_multi query).

The queries in this group are:

span_term query
The equivalent of the term query but for use with other span queries.
span_multi query
Wraps a term, range, prefix, wildcard, regexp, or fuzzy query.
span_first query
Accepts another span query whose matches must appear within the first N positions of the field.
span_near query
Accepts multiple span queries whose matches must be within the specified distance of each other, and possibly in the same order.
span_or query
Combines multiple span queries — returns documents which match any of the specified queries.
span_not query
Wraps another span query, and excludes any documents which match that query.
span_containing query
Accepts a list of span queries, but only returns those spans which also match a second span query.
span_within query
The result from a single span query is returned as long is its span falls within the spans returned by a list of other span queries.

Span Term Query

See Span Term Query

QueryBuilder qb = spanTermQuery(
    "user",                                     
    "kimchy"                                    
);

field

value

Span Multi Term Query

See Span Multi Term Query

QueryBuilder qb = spanMultiTermQueryBuilder(
    prefixQuery("user", "ki")                   
);

Can be any builder extending the MultiTermQueryBuilder class. For example: FuzzyQueryBuilder, PrefixQueryBuilder, RangeQueryBuilder, RegexpQueryBuilder or WildcardQueryBuilder.

Span First Query

See Span First Query

QueryBuilder qb = spanFirstQuery(
    spanTermQuery("user", "kimchy"),            
    3                                           
);

query

max end position

Span Near Query

See Span Near Query

QueryBuilder qb = spanNearQuery()
    .clause(spanTermQuery("field","value1"))    
    .clause(spanTermQuery("field","value2"))    
    .clause(spanTermQuery("field","value3"))    
    .slop(12)                                   
    .inOrder(false)                             
    .collectPayloads(false);                    

span term queries

slop factor: the maximum number of intervening unmatched positions

whether matches are required to be in-order

collect payloads or not

Span Or Query

See Span Or Query

QueryBuilder qb = spanOrQuery()
    .clause(spanTermQuery("field","value1"))    
    .clause(spanTermQuery("field","value2"))    
    .clause(spanTermQuery("field","value3"));   

span term queries

Span Not Query

See Span Not Query

QueryBuilder qb = spanNotQuery()
    .include(spanTermQuery("field","value1"))   
    .exclude(spanTermQuery("field","value2"));  

span query whose matches are filtered

span query whose matches must not overlap those returned

Span Containing Query

See Span Containing Query

QueryBuilder qb = spanContainingQuery()
    .little(spanTermQuery("field1","foo"))      
    .big(spanNearQuery()                        
        .clause(spanTermQuery("field1","bar"))
        .clause(spanTermQuery("field1","baz"))
        .slop(5)
        .inOrder(true)
    );

little part

big part

Span Within Query

See Span Within Query

QueryBuilder qb = spanWithinQuery()
    .little(spanTermQuery("field1", "foo"))                 
    .big(spanNearQuery()                                    
                    .clause(spanTermQuery("field1", "bar"))
                    .clause(spanTermQuery("field1", "baz"))
                    .slop(5)
                    .inOrder(true)
    );

little part

big part