Skip to content

Latest commit

 

History

History
542 lines (400 loc) · 15.4 KB

term-queries.md

File metadata and controls

542 lines (400 loc) · 15.4 KB

Term Queries

Exists

existsSearch returns documents that contain an indexed value for a field:

$searchResult = Book::existsSearch()
    ->field('description')
    ->execute();

ExistsQueryBuilder doesn't provide any additional methods.

Fuzzy

fuzzySearch returns documents that contain terms similar to the search term:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->execute();

Available methods provided by FuzzyQueryBuilder:

field

Use field to specify the field you wish to search:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->execute();

fuzziness

fuzziness controls maximum edit distance allowed for matching:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->fuzziness('AUTO')
    ->execute();

maxExpansions

You can use maxExpansions to specify maximum number of terms to which the query will expand:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->maxExpansions(50)
    ->execute();

prefixLength

prefixLength is used to determine the number of beginning characters left unchanged when creating expansions:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->prefixLength(0)
    ->execute();

rewrite

rewrite is used to rewrite the query:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->rewrite('constant_score')
    ->execute();

transpositions

transpositions allows to include transpositions of two adjacent characters:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->transpositions(true)
    ->execute();

value

With value you can define a term you wish to find in the provided field:

$searchResult = Book::fuzzySearch()
    ->field('title')
    ->value('boko')
    ->execute();

Ids

idsSearch returns documents based on their IDs:

$searchResult = Book::idsSearch()
    ->values(['1', '2', '3'])
    ->execute();

Prefix

prefixSearch returns documents that contain a specific prefix in a provided field:

$searchResult = Book::prefixSearch()
    ->field('title')
    ->value('boo')
    ->execute();

Available methods provided by PrefixQueryBuilder:

field

Use field to specify the field you wish to search:

$searchResult = Book::prefixSearch()
    ->field('title')
    ->value('boo')
    ->execute();

rewrite

rewrite is used to rewrite the query:

$searchResult = Book::prefixSearch()
    ->field('title')
    ->value('boo')
    ->rewrite('constant_score')
    ->execute();

value

With value you can define beginning characters of terms you wish to find in the provided field:

$searchResult = Book::prefixSearch()
    ->field('title')
    ->value('boo')
    ->execute();

Range

rangeSearch returns documents that contain terms within a provided range:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->gt(100)
    ->execute();

Available methods provided by RangeQueryBuilder:

boost

boost method allows you to decrease or increase the relevance scores of a query:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->gt(100)
    ->boost(2)
    ->execute();

field

Use field to specify the field you wish to search:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->gt(100)
    ->execute();

format

format is used to convert date values in the query:

$searchResult = Book::rangeSearch()
    ->field('updated_at')
    ->gt('2020-10-18')
    ->format('yyyy-MM-dd')
    ->execute();

gt

gt defines a greater than range:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->gt(100)
    ->execute();

gte

gte defines a greater than or equal to range:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->gte(100)
    ->execute();

lt

lt defines a less than range:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->lt(100)
    ->execute();

lte

lte defines a less than or equal to range:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->lte(100)
    ->execute();

relation

You can use relation to specify how the range query matches values for range fields:

$searchResult = Book::rangeSearch()
    ->field('price')
    ->gt(50)
    ->lt(100)
    ->relation('INTERSECTS')
    ->execute();

timeZone

timeZone is used to convert date values in the query to UTC:

$searchResult = Book::rangeSearch()
    ->field('updated_at')
    ->gt('2020-10-18')
    ->timeZone('+01:00')
    ->execute();

Regexp

regexpSearch returns documents that contain terms matching a regular expression:

$searchResult = Book::regexpSearch()
    ->field('title')
    ->value('b.*k')
    ->execute();

Available methods provided by RegexpQueryBuilder:

field

Use field to specify the field you wish to search:

$searchResult = Book::regexpSearch()
    ->field('title')
    ->value('b.*k')
    ->execute();

flags

Use flags to enable optional operators for the regular expression:

$searchResult = Book::regexpSearch()
    ->field('title')
    ->value('b.*k')
    ->flags('ALL')
    ->execute();

maxDeterminizedStates

maxDeterminizedStates defines the maximum number of automation states required for the query:

$searchResult = Book::regexpSearch()
    ->field('title')
    ->value('b.*k')
    ->maxDeterminizedStates(10000)
    ->execute();

rewrite

rewrite is used to rewrite the query:

$searchResult = Book::regexpSearch()
    ->field('title')
    ->value('b.*k')
    ->rewrite('constant_score')
    ->execute();

value

With value you can define a regular expression for terms you wish to find in the provided field:

$searchResult = Book::regexpSearch()
    ->field('title')
    ->value('b.*k')
    ->execute();

Term

termSearch returns documents that contain an exact term in a provided field:

$searchResult = Book::termSearch()
    ->field('price')
    ->value('300')
    ->execute();

Available methods provided by TermQueryBuilder:

boost

boost method allows you to decrease or increase the relevance scores of a query:

$searchResult = Book::termSearch()
    ->field('price')
    ->value('300')
    ->boost(2)
    ->execute();

field

Use field to specify the field you wish to search:

$searchResult = Book::termSearch()
    ->field('price')
    ->value('300')
    ->execute();

value

With value you can define a term you wish to find in the provided field:

$searchResult = Book::termSearch()
    ->field('price')
    ->value('300')
    ->execute();

Terms

termsSearch returns documents that contain one or more exact terms in a provided field:

$searchResult = Book::termsSearch()
    ->terms('tags', ['available', 'new'])
    ->execute();

Available methods provided by TermsQueryBuilder:

boost

boost method allows you to decrease or increase the relevance scores of a query:

$searchResult = Book::termsSearch()
    ->terms('tags', ['available', 'new'])
    ->boost(2)
    ->execute();

terms

Use terms to define array of terms you wish to find in the provided field:

$searchResult = Book::termsSearch()
    ->terms('tags', ['available', 'new'])
    ->execute();

Wildcard

wildcardSearch returns documents that contain terms matching a wildcard pattern:

$searchResult = Book::wildcardSearch()
    ->field('title')
    ->value('bo*k')
    ->execute();

Available methods provided by WildcardQueryBuilder:

boost

boost method allows you to decrease or increase the relevance scores of a query:

$searchResult = Book::wildcardSearch()
    ->field('title')
    ->value('bo*k')
    ->boost(2)
    ->execute();

field

Use field to specify the field you wish to search:

$searchResult = Book::wildcardSearch()
    ->field('title')
    ->value('bo*k')
    ->execute();

rewrite

rewrite is used to rewrite the query:

$searchResult = Book::wildcardSearch()
    ->field('title')
    ->value('bo*k')
    ->rewrite('constant_score')
    ->execute();

value

With value you can define a wildcard pattern for terms you wish to find in the provided field:

$searchResult = Book::wildcardSearch()
    ->field('title')
    ->value('bo*k')
    ->execute();