Skip to content

Latest commit

 

History

History
639 lines (483 loc) · 18.6 KB

full-text-queries.md

File metadata and controls

639 lines (483 loc) · 18.6 KB

Full Text Queries

Match All

Use matchAllSearch to perform a search request, which matches all documents:

$searchResult = Book::matchAllSearch()->execute();

MatchAllQueryBuilder doesn't provide any additional methods.

Match None

matchNoneSearch is the inverse of matchAllSearch:

$searchResult = Book::matchNoneSearch()->execute();

MatchNoneQueryBuilder doesn't provide any additional methods.

Match Phrase Prefix

Use matchPhrasePrefixSearch to search for documents, that contain the words of a provided text, in the same order as provided:

$searchResult = Book::matchPhrasePrefixSearch()
    ->field('title')
    ->query('My boo')
    ->execute();

Available methods provided by MatchPhrasePrefixQueryBuilder:

analyzer

analyzer is used to convert the query text into tokens:

$searchResult = Book::matchPhrasePrefixSearch()
    ->field('title')
    ->query('My boo')
    ->analyzer('english')
    ->execute();

field

Use field to specify the field you wish to search:

$searchResult = Book::matchPhrasePrefixSearch()
    ->field('title')
    ->query('My boo')
    ->execute();

maxExpansions

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

$searchResult = Book::matchPhrasePrefixSearch()
    ->field('title')
    ->query('My boo')
    ->maxExpansions(50)
    ->execute();

query

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

$searchResult = Book::matchPhrasePrefixSearch()
    ->field('title')
    ->query('My boo')
    ->execute();

slop

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

$searchResult = Book::matchPhrasePrefixSearch()
    ->field('title')
    ->query('My boo')
    ->slop(0)
    ->execute();

zeroTermsQuery

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

$searchResult = Book::matchPhrasePrefixSearch()
    ->field('title')
    ->query('My boo')
    ->zeroTermsQuery('none')
    ->execute();

Match Phrase

Use matchPhraseSearch to search for documents, which match the given phrase:

$searchResult = Book::matchPhraseSearch()
    ->field('title')
    ->query('My book')
    ->execute();

Available methods provided by MatchPhraseQueryBuilder:

analyzer

analyzer is used to convert the query text into tokens:

$searchResult = Book::matchPhraseSearch()
    ->field('title')
    ->query('My book')
    ->analyzer('english')
    ->execute();

field

Use field to specify the field you wish to search:

$searchResult = Book::matchPhraseSearch()
    ->field('title')
    ->query('My book')
    ->execute();

query

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

$searchResult = Book::matchPhraseSearch()
    ->field('title')
    ->query('My book')
    ->execute();

slop

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

$searchResult = Book::matchPhraseSearch()
    ->field('title')
    ->query('My book')
    ->slop(0)
    ->execute();

zeroTermsQuery

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

$searchResult = Book::matchPhraseSearch()
    ->field('title')
    ->query('My book')
    ->zeroTermsQuery('none')
    ->execute();

Match

Use matchSearch for full-text search:

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->execute();

Available methods provided by MatchQueryBuilder:

analyzer

analyzer is used to convert the query text into tokens:

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->analyzer('english')
    ->execute();

autoGenerateSynonymsPhraseQuery

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

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->autoGenerateSynonymsPhraseQuery(true)
    ->execute();

boost

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

$searchResult = Book::matchSearch()
   ->field('title')
   ->query('My book')
   ->boost(2)
   ->execute();

field

Use field to specify the field you wish to search:

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->execute();

fuzziness

fuzziness controls maximum edit distance allowed for matching:

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->fuzziness('AUTO')
    ->execute();

fuzzyRewrite

fuzzyRewrite is used to rewrite the query:

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->fuzzyRewrite('constant_score')
    ->execute();

fuzzyTranspositions

Use fuzzyTranspositions to allow transpositions for two adjacent characters:

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->fuzziness('AUTO')
    ->fuzzyTranspositions(true)
    ->execute();

lenient

Use lenient to ignore format-based errors:

$searchResult = Book::matchSearch()
    ->field('price')
    ->query('My book')
    ->lenient(true)
    ->execute();

maxExpansions

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

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->maxExpansions(50)
    ->execute();

minimumShouldMatch

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

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->operator('OR')
    ->minimumShouldMatch(1)
    ->execute();

operator

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

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->operator('OR')
    ->execute();

prefixLength

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

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')
    ->fuzziness('AUTO')
    ->prefixLength(0)
    ->execute();

query

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

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')  
    ->execute();

zeroTermsQuery

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

$searchResult = Book::matchSearch()
    ->field('title')
    ->query('My book')  
    ->zeroTermsQuery('none')
    ->execute();

Multi-Match

Use multiMatchSearch to preform full-text search in multiple fields:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->execute();

Available methods provided by MultiMatchQueryBuilder:

analyzer

analyzer is used to convert the query text into tokens:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->analyzer('english')
    ->execute();

autoGenerateSynonymsPhraseQuery

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->autoGenerateSynonymsPhraseQuery(true)
    ->execute();

boost

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

$searchResult = Book::multiMatchSearch()
   ->fields(['title', 'description'])
   ->query('My book')
   ->boost(2)
   ->execute();

fields

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->execute();

fuzziness

fuzziness controls maximum edit distance allowed for matching:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzziness('AUTO')
    ->execute();

fuzzyRewrite

fuzzyRewrite is used to rewrite the query:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzzyRewrite('constant_score')
    ->execute();

fuzzyTranspositions

Use fuzzyTranspositions to allow transpositions for two adjacent characters:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzziness('AUTO')
    ->fuzzyTranspositions(true)
    ->execute();

lenient

Use lenient to ignore format-based errors:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->lenient(true)
    ->execute();

maxExpansions

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->maxExpansions(50)
    ->execute();

minimumShouldMatch

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->operator('OR')
    ->minimumShouldMatch(1)
    ->execute();

operator

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->operator('OR')
    ->execute();

prefixLength

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->fuzziness('AUTO')
    ->prefixLength(0)
    ->execute();

query

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->execute();

slop

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->slop(0)
    ->execute();

tieBreaker

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

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->tieBreaker(0.3)
    ->execute();

type

Use type to define how the query must be executed:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->type('best_fields')
    ->execute();

Note, that not all the 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:

$searchResult = Book::multiMatchSearch()
    ->fields(['title', 'description'])
    ->query('My book')
    ->zeroTermsQuery('none')
    ->execute();