Skip to content

Latest commit

 

History

History
690 lines (487 loc) · 20 KB

full-text-queries.md

File metadata and controls

690 lines (487 loc) · 20 KB

Full Text Queries

Match All

You can use Elastic\ScoutDriverPlus\Support\Query::matchAll() to build a query that matches all documents:

$query = Query::matchAll();

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

Match None

You can use Elastic\ScoutDriverPlus\Support\Query::matchNone() to build a query that matches no documents:

$query = Query::matchNone();

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

Match Phrase Prefix

You can use Elastic\ScoutDriverPlus\Support\Query::matchPhrasePrefix() to build a query that matches documents, which contain the words of a provided text in the same order as provided:

$query = Query::matchPhrasePrefix()
    ->field('title')
    ->query('My boo');

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

Available methods:

analyzer

analyzer is used to convert the query text into tokens:

$query = Query::matchPhrasePrefix()
    ->field('title')
    ->query('My boo')
    ->analyzer('english');

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

field

Use field to specify the field you wish to search:

$query = Query::matchPhrasePrefix()
    ->field('title')
    ->query('My boo');

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

maxExpansions

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

$query = Query::matchPhrasePrefix()
    ->field('title')
    ->query('My boo')
    ->maxExpansions(50);

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

query

Use query to set the text you wish to find in the provided field:

$query = Query::matchPhrasePrefix()
    ->field('title')
    ->query('My boo');

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

slop

Use slop to define the maximum number of positions allowed between matching tokens:

$query = Query::matchPhrasePrefix()
    ->field('title')
    ->query('My boo')
    ->slop(0);

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

zeroTermsQuery

You can define what to return in case analyzer removes all tokens using zeroTermsQuery:

$query = Query::matchPhrasePrefix()
    ->field('title')
    ->query('My boo')
    ->zeroTermsQuery('none');

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

Match Phrase

You can use Elastic\ScoutDriverPlus\Support\Query::matchPhrase() to build a query that matches documents, which contain the given phrase:

$query = Query::matchPhrase()
    ->field('title')
    ->query('My book');

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

Available methods:

analyzer

analyzer is used to convert the query text into tokens:

$query = Query::matchPhrase()
    ->field('title')
    ->query('My book')
    ->analyzer('english');

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

field

Use field to specify the field you wish to search:

$query = Query::matchPhrase()
    ->field('title')
    ->query('My book');

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

query

Use query to set the text you wish to find in the provided field:

$query = Query::matchPhrase()
    ->field('title')
    ->query('My book');

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

slop

Use slop to define the maximum number of positions allowed between matching tokens:

$query = Query::matchPhrase()
    ->field('title')
    ->query('My book')
    ->slop(0);

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

zeroTermsQuery

You can define what to return in case analyzer removes all tokens with zeroTermsQuery:

$query = Query::matchPhrase()
    ->field('title')
    ->query('My book')
    ->zeroTermsQuery('none');

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

Match

You can use Elastic\ScoutDriverPlus\Support\Query::match() to build a query that matches documents, which contain a provided text, number, date or boolean value:

$query = Query::match()
    ->field('title')
    ->query('My book');

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

Available methods:

analyzer

analyzer is used to convert the query text into tokens:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->analyzer('english');

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

autoGenerateSynonymsPhraseQuery

autoGenerateSynonymsPhraseQuery allows you to define if match phrase queries have to be automatically created for multi-term synonyms:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->autoGenerateSynonymsPhraseQuery(true);

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

boost

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

$query = Query::match()
   ->field('title')
   ->query('My book')
   ->boost(2);

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

field

Use field to specify the field you wish to search:

$query = Query::match()
    ->field('title')
    ->query('My book');

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

fuzziness

fuzziness controls maximum edit distance allowed for matching:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->fuzziness('AUTO');

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

fuzzyRewrite

fuzzyRewrite is used to rewrite the query:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->fuzzyRewrite('constant_score');

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

fuzzyTranspositions

Use fuzzyTranspositions to allow transpositions for two adjacent characters:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->fuzziness('AUTO')
    ->fuzzyTranspositions(true);

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

lenient

Use lenient to ignore format-based errors:

$query = Query::match()
    ->field('price')
    ->query('My book')
    ->lenient(true);

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

maxExpansions

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

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->maxExpansions(50);

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

minimumShouldMatch

minimumShouldMatch defines minimum number of clauses that must match for a document to be returned:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->operator('OR')
    ->minimumShouldMatch(1);

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

operator

Use operator to define the boolean logic used to interpret the query text:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->operator('OR');

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

prefixLength

prefixLength is used to determine the number of beginning characters left unchanged for fuzzy matching:

$query = Query::match()
    ->field('title')
    ->query('My book')
    ->fuzziness('AUTO')
    ->prefixLength(0);

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

query

Use query to set the text you wish to find in the provided field:

$query = Query::match()
    ->field('title')
    ->query('My book');

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

zeroTermsQuery

You can define what to return in case analyzer removes all tokens with zeroTermsQuery:

$query = Query::match()
    ->field('title')
    ->query('My book')  
    ->zeroTermsQuery('none');

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

Multi-Match

You can use Elastic\ScoutDriverPlus\Support\Query::multiMatch() to build a query that matches documents, which contain a provided text, number, date or boolean value in multiple fields:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book');

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

Available methods:

analyzer

analyzer is used to convert the query text into tokens:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->analyzer('english');

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

autoGenerateSynonymsPhraseQuery

autoGenerateSynonymsPhraseQuery allows you to define, if match phrase queries have to be automatically created for multi-term synonyms:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->autoGenerateSynonymsPhraseQuery(true);

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

boost

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

$query = Query::multiMatch()
   ->fields(['title', 'description'])
   ->query('My book')
   ->boost(2);

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

fields

Use fields to define the fields you wish to search in:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book');

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

fuzziness

fuzziness controls maximum edit distance allowed for matching:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzziness('AUTO');

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

fuzzyRewrite

fuzzyRewrite is used to rewrite the query:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzzyRewrite('constant_score');

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

fuzzyTranspositions

Use fuzzyTranspositions to allow transpositions for two adjacent characters:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzziness('AUTO')
    ->fuzzyTranspositions(true);

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

lenient

Use lenient to ignore format-based errors:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->lenient(true);

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

maxExpansions

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

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->maxExpansions(50);

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

minimumShouldMatch

minimumShouldMatch defines minimum number of clauses that must match for a document to be returned:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->operator('OR')
    ->minimumShouldMatch(1);

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

operator

Use operator to define the boolean logic used to interpret the query text:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->operator('OR');

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

prefixLength

prefixLength is used to determine the number of beginning characters left unchanged for fuzzy matching:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzziness('AUTO')
    ->prefixLength(0);

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

query

Use query to set the text you wish to find in the provided fields:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book');

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

slop

Use slop to define the maximum number of positions allowed between matching tokens:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->slop(0);

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

tieBreaker

tieBreaker is used to increase the relevance scores of documents matching the query:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->tieBreaker(0.3);

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

type

Use type to define how the query must be executed:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->type('best_fields');

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

Note that not all available methods make sense with every type. Read the documentation carefully.

zeroTermsQuery

You can define what to return in case analyzer removes all tokens with zeroTermsQuery:

$query = Query::multiMatch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->zeroTermsQuery('none');

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