SearchNew
Function: SearchNew
Creates and returns a new {@link SearchBuilder} wired to the active search provider.
The builder returned is the entry point for the bx-search fluent query API. When an {@code index} is supplied, {@code newSearch( ... )} is invoked automatically; otherwise the caller must call {@code builder.newSearch( collection )} before executing. When a {@code properties} struct is provided, it becomes the starting query DSL for the builder (equivalent to calling {@code setQuery( properties )}).
Method Signature
SearchNew(index=[string], properties=[struct])Arguments
index
string
false
Optional bare collection name. If omitted the caller must invoke {@code builder.newSearch(collection)} before executing.
properties
struct
false
Optional struct containing a raw query body. When provided, this becomes the starting DSL for the builder (equivalent to calling {@code setQuery(properties)}).
Examples
SearchNew / SearchBuilder
SearchNew() returns a fluent SearchBuilder that is pre-wired to the active search provider. The sections below document every builder method with a description and a usage example.
Initialization
newSearch( collection ) / newSearch( collection, properties )
Sets the target collection and, optionally, pre-populates the query from a raw DSL struct. Any previously configured query state is reset first.
reset()
Clears all query state, returning the builder to a blank slate. The collection and provider are retained.
Match methods
match( name, value )
Adds a standard match query clause against a single field (analyzed text). By default it is added to the must (AND) list. Supports optional boost, options, and occurrence.
Match with a relevance boost and additional options against any given occurrence:
mustMatch( name, value )
Adds a must match clause — documents must match to be returned.
mustNotMatch( name, value )
Adds a must_not match clause — documents must not match.
shouldMatch( name, value )
Adds a should match clause — documents may match (boosts their relevance score).
multiMatch( names, value )
Searches multiple fields at once using Elasticsearch's multi_match query. Supports optional boost, type (e.g. best_fields, cross_fields), and occurrence.
Full signature with boost, match type, and occurrence:
dateMatch( name, start, end )
Adds a date range filter clause on a date-mapped field. Supports ISO-8601 strings and Elasticsearch date math (e.g. now-7d). Only one bound is required.
Term / Wildcard
term( name, value )
Adds an exact-value term query — ideal for keyword fields that are not analyzed. Supports optional boost and occurrence.
wildcard( name, value )
Adds a wildcard query for partial matches on keyword-mapped fields. Note that wildcards are slower than term/match. The pattern is wrapped in *...* automatically.
Sorting
sort( field ) / sort( field, config )
Appends a sort directive. May be called multiple times for compound sorting.
Pagination
setFrom( from )
Sets the zero-based starting offset for pagination.
setSize( size )
Sets the maximum number of documents to return per page (default is 10).
Field projection
setFields( fields... )
Sets the list of fields to return in each hit, replacing the full _source with the projected fields. Accepts indexed and runtime field names.
addField( field )
Appends a single field to the projection list.
addScriptField( name, script )
Defines a script field, computed at search time for each hit. The script is an Elasticsearch script definition map.
addRuntimeMapping( name, mapping )
Defines a runtime field mapping evaluated at search time (type plus optional script).
Aggregations & Highlight
aggregation( name, options )
Adds an aggregation directive, retrievable afterwards from SearchResult#getAggregations().
highlight( options )
Sets the highlight configuration so matching fragments are returned with each hit.
Field collapse
collapseToField( field ) / collapseToField( field, options, includeOccurrences )
Collapses results to the single most-relevant document per unique value of the given field. When includeOccurrences is true, cardinality/terms aggregations are added so the collapsed counts are populated.
Raw DSL override
setQuery( dsl )
Sets the raw query body, overriding every built clause. Useful for passing a pre-built DSL struct directly (accepts a struct or a plain Java map).
Request parameters
param( name, value )
Appends a query-string parameter to the request URL (e.g. version).
bodyParam( name, value )
Appends a request body parameter merged into the top-level body (e.g. track_scores, min_score).
DSL assembly
getDSL() / getDSLAsStruct()
Assembles and returns the complete query body without executing it. Useful for debugging or passing the DSL elsewhere.
Execution
execute()
Executes the search and returns the full result set as a SearchResult.
count()
Executes the query and returns only the matched document count — more efficient than execute() when only a count is needed.
deleteAll()
Deletes all documents in the collection that match the current query.
getTermVectors( documentId, fields )
Retrieves term vectors (term frequency, etc.) for a document in the target collection. Optional options map is supported (e.g. min_word_length).
Accessors
getCollection() / getProvider()
Access the target collection name (bare, un-prefixed) and the backing provider.
Related
Last updated
Was this helpful?
