Skip to content

Latest commit

 

History

History
661 lines (458 loc) · 19.2 KB

term-queries.md

File metadata and controls

661 lines (458 loc) · 19.2 KB

Term Queries

Exists

You can use Elastic\ScoutDriverPlus\Support\Query::exists() to build a query that matches documents, which contain an indexed value for a field:

$query = Query::exists()->field('description');

$searchResult = Book::searchQuery($query)->execute();

Fuzzy

You can use Elastic\ScoutDriverPlus\Support\Query::fuzzy() to build a query that matches documents, which contain terms similar to the search term:

$query = Query::fuzzy()
    ->field('title')
    ->value('boko');

$searchResult = Book::searchQuery($query)->execute();

Available methods:

field

Use field to specify the field you wish to search:

$query = Query::fuzzy()
    ->field('title')
    ->value('boko');

$searchResult = Book::searchQuery($query)->execute();

fuzziness

fuzziness controls maximum edit distance allowed for matching:

$query = Query::fuzzy()
    ->field('title')
    ->value('boko')
    ->fuzziness('AUTO');

$searchResult = Book::searchQuery($query)->execute();

maxExpansions

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

$query = Query::fuzzy()
    ->field('title')
    ->value('boko')
    ->maxExpansions(50);

$searchResult = Book::searchQuery($query)->execute();

prefixLength

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

$query = Query::fuzzy()
    ->field('title')
    ->value('boko')
    ->prefixLength(0);

$searchResult = Book::searchQuery($query)->execute();

rewrite

rewrite is used to rewrite the query:

$query = Query::fuzzy()
    ->field('title')
    ->value('boko')
    ->rewrite('constant_score');

$searchResult = Book::searchQuery($query)->execute();

transpositions

transpositions allows to include transpositions of two adjacent characters:

$query = Query::fuzzy()
    ->field('title')
    ->value('boko')
    ->transpositions(true);

$searchResult = Book::searchQuery($query)->execute();

value

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

$query = Query::fuzzy()
    ->field('title')
    ->value('boko');

$searchResult = Book::searchQuery($query)->execute();

Ids

You can use Elastic\ScoutDriverPlus\Support\Query::ids() to build a query that matches documents based on their IDs:

$query = Query::ids()->values(['1', '2', '3']);

$searchResult = Book::searchQuery($query)->execute();

Prefix

You can use Elastic\ScoutDriverPlus\Support\Query::prefix() to build a query that matches documents, which contain a specific prefix in a provided field:

$query = Query::prefix()
    ->field('title')
    ->value('boo');

$searchResult = Book::searchQuery($query)->execute();

Available methods:

caseInsensitive

caseInsensitive is used to allow ASCII case insensitive matching:

$query = Query::prefix()
    ->field('title')
    ->value('boo')
    ->caseInsensitive(true);

$searchResult = Book::searchQuery($query)->execute();

field

Use field to specify the field you wish to search:

$query = Query::prefix()
    ->field('title')
    ->value('boo');

$searchResult = Book::searchQuery($query)->execute();

rewrite

rewrite is used to rewrite the query:

$query = Query::prefix()
    ->field('title')
    ->value('boo')
    ->rewrite('constant_score');

$searchResult = Book::searchQuery($query)->execute();

value

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

$query = Query::prefix()
    ->field('title')
    ->value('boo');

$searchResult = Book::searchQuery($query)->execute();

Range

You can use Elastic\ScoutDriverPlus\Support\Query::range() to build a query that matches documents, which contain terms within a provided range:

$query = Query::range()
    ->field('price')
    ->gt(100);

$searchResult = Book::searchQuery($query)->execute();

Available methods:

boost

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

$query = Query::range()
    ->field('price')
    ->gt(100)
    ->boost(2);

$searchResult = Book::searchQuery($query)->execute();

field

Use field to specify the field you wish to search:

$query = Query::range()
    ->field('price')
    ->gt(100);

$searchResult = Book::searchQuery($query)->execute();

format

format is used to convert date values in the query:

$query = Query::range()
    ->field('updated_at')
    ->gt('2020-10-18')
    ->format('yyyy-MM-dd');

$searchResult = Book::searchQuery($query)->execute();

gt

gt defines a greater than range:

$query = Query::range()
    ->field('price')
    ->gt(100);

$searchResult = Book::searchQuery($query)->execute();

gte

gte defines a greater than or equal to range:

$query = Query::range()
    ->field('price')
    ->gte(100);

$searchResult = Book::searchQuery($query)->execute();

lt

lt defines a less than range:

$query = Query::range()
    ->field('price')
    ->lt(100);

$searchResult = Book::searchQuery($query)->execute();

lte

lte defines a less than or equal to range:

$query = Query::range()
    ->field('price')
    ->lte(100);

$searchResult = Book::searchQuery($query)->execute();

relation

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

$query = Query::range()
    ->field('price')
    ->gt(50)
    ->lt(100)
    ->relation('INTERSECTS');

$searchResult = Book::searchQuery($query)->execute();

timeZone

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

$query = Query::range()
    ->field('updated_at')
    ->gt('2020-10-18')
    ->timeZone('+01:00');

$searchResult = Book::searchQuery($query)->execute();

Regexp

You can use Elastic\ScoutDriverPlus\Support\Query::regexp() to build a query that matches documents, which contain terms corresponding to regular expression:

$query = Query::regexp()
    ->field('title')
    ->value('b.*k');

$searchResult = Book::searchQuery($query)->execute();

Available methods:

caseInsensitive

caseInsensitive is used to allow case insensitive matching:

$query = Query::regexp()
    ->field('title')
    ->value('b.*k')
    ->caseInsensitive(true);

$searchResult = Book::searchQuery($query)->execute();

field

Use field to specify the field you wish to search:

$query = Query::regexp()
    ->field('title')
    ->value('b.*k');

$searchResult = Book::searchQuery($query)->execute();

flags

Use flags to enable optional operators for the regular expression:

$query = Query::regexp()
    ->field('title')
    ->value('b.*k')
    ->flags('ALL');

$searchResult = Book::searchQuery($query)->execute();

maxDeterminizedStates

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

$query = Query::regexp()
    ->field('title')
    ->value('b.*k')
    ->maxDeterminizedStates(10000);

$searchResult = Book::searchQuery($query)->execute();

rewrite

rewrite is used to rewrite the query:

$query = Query::regexp()
    ->field('title')
    ->value('b.*k')
    ->rewrite('constant_score');

$searchResult = Book::searchQuery($query)->execute();

value

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

$query = Query::regexp()
    ->field('title')
    ->value('b.*k');

$searchResult = Book::searchQuery($query)->execute();

Term

You can use Elastic\ScoutDriverPlus\Support\Query::term() to build a query that matches documents, which contain an exact term in a provided field:

$query = Query::term()
    ->field('price')
    ->value(300);

$searchResult = Book::searchQuery($query)->execute();

Available methods:

boost

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

$query = Query::term()
    ->field('price')
    ->value(300)
    ->boost(2);

$searchResult = Book::searchQuery($query)->execute();

caseInsensitive

caseInsensitive is used to allow ASCII case insensitive matching:

$query = Query::term()
    ->field('price')
    ->value(300)
    ->caseInsensitive(true);

$searchResult = Book::searchQuery($query)->execute();

field

Use field to specify the field you wish to search:

$query = Query::term()
    ->field('price')
    ->value(300);

$searchResult = Book::searchQuery($query)->execute();

value

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

$query = Query::term()
    ->field('price')
    ->value(300);

$searchResult = Book::searchQuery($query)->execute();

Terms

You can use Elastic\ScoutDriverPlus\Support\Query::terms() to build a query that matches documents, which contain one or more exact terms in a provided field:

$query = Query::terms()
    ->field('tags')
    ->values(['available', 'new']);

$searchResult = Book::searchQuery($query)->execute();

Available methods:

boost

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

$query = Query::terms()
    ->field('tags')
    ->values(['available', 'new'])
    ->boost(2);

$searchResult = Book::searchQuery($query)->execute();

field

Use field to specify the field you wish to search:

$query = Query::terms()
    ->field('tags')
    ->values(['available', 'new']);

$searchResult = Book::searchQuery($query)->execute();

values

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

$query = Query::terms()
    ->field('tags')
    ->values(['available', 'new']);

$searchResult = Book::searchQuery($query)->execute();

Wildcard

You can use Elastic\ScoutDriverPlus\Support\Query::wildcard() to build a query that matches documents, which contain terms corresponding to wildcard pattern:

$query = Elastic\ScoutDriverPlus\Support\Query::wildcard()
    ->field('title')
    ->value('bo*k');

$searchResult = Book::searchQuery($query)->execute();

Available methods:

boost

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

$query = Elastic\ScoutDriverPlus\Support\Query::wildcard()
    ->field('title')
    ->value('bo*k')
    ->boost(2);

$searchResult = Book::searchQuery($query)->execute();

caseInsensitive

caseInsensitive is used to allow case insensitive matching:

$query = Elastic\ScoutDriverPlus\Support\Query::wildcard()
    ->field('title')
    ->value('bo*k')
    ->caseInsensitive(true);

$searchResult = Book::searchQuery($query)->execute();

field

Use field to specify the field you wish to search:

$query = Elastic\ScoutDriverPlus\Support\Query::wildcard()
    ->field('title')
    ->value('bo*k');

$searchResult = Book::searchQuery($query)->execute();

rewrite

rewrite is used to rewrite the query:

$query = Elastic\ScoutDriverPlus\Support\Query::wildcard()
    ->field('title')
    ->value('bo*k')
    ->rewrite('constant_score');

$searchResult = Book::searchQuery($query)->execute();

value

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

$query = Elastic\ScoutDriverPlus\Support\Query::wildcard()
    ->field('title')
    ->value('bo*k');

$searchResult = Book::searchQuery($query)->execute();