SearchBuilderobject offers an expressive syntax for crafting detailed searches with ranked results. To perform a simple search for matching documents documents, using Elasticsearch's automatic scoring, we would use the
Documentobjects ( or an empty array if no results are found ), with a descending match score as the sort.
term()method allows a means of specifying an exact match of all documents in the search results. An example use case might be only to search for active documents:
match()method of the
SearchBuilderalso allows for a
boostargument. When provided, results which match the term will be ranked higher in the results:
namefield containing "Elasticsearch" would be boosted in score higher than those which only find the value in the short or long description.
keyword-mapped field in elasticsearch. The
wildcardmethod allows you to do this. Let's say I wanted to match any documents with a
Elastic. I could use the following method to match those documents:
namekeyword field containing
Elasticache. It is important to note that wildcard queries are exceptionally slow, compared to
shouldqueries, as they require recursion through the entire index of document values to obtain their matches.
operatorargument for the wildcard query to "should" to ensure that the match becomes an "or" for the short description or the wildcard. In addition, we boost the wildcard results 5 times above the short description matched results.
sort()method also allows you to specify custom sort options. To sort by author last name, instead of score, use:
sort()method also accepts a full sort config:
.sort()multiple times will append the sort configurations to allow fine-tuning the sort order:
shortDescriptionfields. We are also looking for a phrase match of "is awesome" and are boosting the score of the applicable document, if found.
collapseToFieldallows you to collapse the results of the search to a specific field. The data return includes the first matched, most relevant, document found with the collapsed field. When field collapsing is specified, an automatic aggregation will be run, which provides a pagination total for the collapsed document counts. When paginating collapsed fields, you will want to use the
getCollapsedCount()as your total record count rather than the usual
getHitCount()- which returns all documents matched to the query.
titlefield ( or, in this case
title.keyword, which is a dynamic keyword-typed field in our index ) to retrieve the most recent version of the book.
includeOccurrences=trueis passed to
collapseToFieldyou can retrieve a map of all collapsed key values and their corresponding document count by calling
collapseToField()also supports an
includeOccurrencesoption. By passing
collapseToField, you can retrieve a map of all collapsed key values and their corresponding document count by calling
count()method from the search builder ( or using the client ) to only return the number of matched documents and omit the result set and metadata:
highlightmethod. The struct should take the shape outlined on the ElasticSearch website.
new([string index], [string type], [struct properties])- Populates a new SearchBuilder object.
reset()- Clears the SearchBuilder and resets the DSL
deleteAll()- Deletes all documents matching the currently built search query.
execute()- Executes the built search
getDSL()- Returns a struct containing the assembled Elasticsearch query DSL
match(string name, any value, [numeric boost], [struct options], [string matchType='any'])- Applies a match requirement to the search builder query.
multiMatch( array names, any value, [numeric boost], [type="best_fields"])- Search an array of fields with a given search value.
dateMatch( string name, string start, string end, [numeric boost])- Adds a date range match.
mustMatch(string name, any value, [numeric boost])-
mustquery alias for match().
mustNotMatch(string name, any value, [numeric boost])-
must_notquery alias for match().
shouldMatch(string name, any value, [numeric boost])-
shouldquery alias for match().
sort(any sort, [any sortConfig])- Applies a custom sort to the search query.
term(string name, any value, [numeric boost])- Adds an exact value restriction ( elasticsearch: term ) to the query.
aggregation(string name, struct options)- Adds an aggregation directive to the search parameters.
collapseToField( string field, struct options, boolean includeOccurrences = false )- Collapses the results to the single field and returns only the most relevant/ordered document matched on that field.