Skip to main content

Query String Query

The QueryString query returns documents based on a provided query string, using a parser with a strict syntax.

To use the QueryString query, import the following:

import zio.elasticsearch.query.QueryStringQuery
import zio.elasticsearch.ElasticQuery._

You can create a QueryString query without specifying fields using the queryString method:

val query: QueryStringQuery[Any] = queryString(query = "name")

If you want to specify which fields should be searched, you can use the fields method:

val query: QueryStringQuery[Any] =
queryString(query = "name").fields("stringField1", "stringField2")

To define fields in a type-safe manner, use the overloaded fields method with field definitions from your document:

val query: QueryStringQuery[Document] =
queryString(query = "name").fields(Document.stringField1, Document.stringField2)

Alternatively, you can pass a Chunk of fields:

val query: QueryStringQuery[Document] =
queryString(query = "name").fields(Chunk(Document.stringField1, Document.stringField2))

You can also specify type-safe fields when constructing the query itself:

val query: QueryStringQuery[Document] =
queryString(query = "name", Document.stringField1, Document.stringField2)

If you want to define the boost parameter, use the boost method:

val query: QueryStringQuery[Any] =
queryString(query = "name").boost(2.0)

If you want to define the minimum_should_match parameter, use the minimumShouldMatch method:

val query: QueryStringQuery[Any] =
queryString(query = "name").minimumShouldMatch(2)

If you want to define the default_field parameter, use the defaultField method:

val query: QueryStringQuery[Any] =
queryString(query = "name").defaultField("stringField")

Elasticsearch does not allow default_field and fields to be used together, so they are mutually exclusive: calling defaultField clears any previously set fields, and calling fields clears any previously set default_field.

You can combine fields (or default_field) with the other parameters:

val query: QueryStringQuery[Document] =
queryString(query = "name", Document.stringField1, Document.stringField2)
.boost(2.0)
.minimumShouldMatch(2)