Skip to content

Query String Query

A query string query parses and executes a search query based on a query string syntax. It allows for flexible and complex query expressions.

Example

import (
    es "github.com/elastic/go-elasticsearch/v8"

    "github.com/sdqri/effdsl/v2"
    mppq "github.com/sdqri/effdsl/v2/queries/matchphraseprefixquery"
)

query, err := effdsl.Define(
    effdsl.WithQuery(
        mppq.MatchPhrasePrefixQuery(
            "field_name",
            "some phrase prefix query",
            mppq.WithAnalyzer("my_analyzer"),
            mppq.WithSlop(2),
            mppq.WithMaxExpansions(10),
        ),
    ),
)

res, err := es.Search(
    es.Search.WithBody(strings.NewReader(query)),
)

Parameters

  • Field (string)
    (Required, positional) The field to search. This is a required parameter.

  • Query (string)
    (Required, positional) The text to search for in the provided field. This is a required parameter.

  • WithAnalyzer (string)
    (Optional, Functional option) Analyzer used to convert the text in the query value into tokens. Defaults to the index-time analyzer mapped for the field. If no analyzer is mapped, the index’s default analyzer is used.

  • WithSlop (int)
    (Optional, Functional option) Maximum number of positions allowed between matching tokens for phrases. Defaults to 0.

  • WithMaxExpansions (int)
    (Optional, Functional option) Maximum number of terms to which the last provided term will expand. Defaults to not expanding terms.

  • WithZeroTermsQuery (ZeroTerms)
    (Optional, Functional option) Indicates what to do when the analyzed text contains no terms. Valid values are:

    • none (Default): No documents are returned if the analyzer removes all tokens.
    • all: Returns all documents, similar to a match_all query.

Additional Information

For more details on the match phrase prefix query and its parameters, refer to the official Elasticsearch documentation on match phrase prefix queries.

"github.com/sdqri/effdsl/v2"
qs "github.com/sdqri/effdsl/v2/queries/querystring"

)

query, err := effdsl.Define( effdsl.WithQuery( qs.QueryString( "alice", qs.WithFields("first_name", "last_name") qs.WithBoost(1.5), qs.WithFuzziness("AUTO"), ), ), )

res, err := es.Search( es.Search.WithBody(strings.NewReader(query)), ) ```

Parameters

  • Query (string)
    (Required, positional) The query string to parse and use for search. This is a required parameter.

  • WithDefaultField (string)
    (Optional, Functional option) Default field to search if no field is provided in the query string.

  • WithAllowLeadingWildcard ()
    (Optional, Functional option) If true, wildcard characters * and ? are allowed as the first character in the query string. Defaults to true.

  • WithAnalyzeWildcard ()
    (Optional, Functional option) If true, the query attempts to analyze wildcard terms in the query string. Defaults to false.

  • WithAnalyzer (string)
    (Optional, Functional option) Analyzer used to convert the text in the query string into tokens.

  • WithAutoGenerateSynonymsPhrase (bool)
    (Optional, Functional option) If true, match phrase queries are automatically created for multi-term synonyms. Defaults to true.

  • WithBoost (float64)
    (Optional, Functional option) Floating point number used to adjust the relevance scores of the query.

  • WithDefaultOperator (Operator)
    (Optional, Functional option) Default boolean logic used to interpret text in the query string. Valid values are:

    • OR: Logical OR.
    • AND: Logical AND.
  • WithEnablePositionIncrements (bool)
    (Optional, Functional option) If true, enable position increments in queries constructed from the query string search.

  • WithFields (...string)
    (Optional, Functional option) Array of fields to search. Supports wildcards *.

  • WithFuzziness (string)
    (Optional, Functional option) Maximum edit distance allowed for fuzzy matching.

  • WithFuzzyMaxExpansions (int)
    (Optional, Functional option) Maximum number of terms for fuzzy matching expansion.

  • WithFuzzyPrefixLength (int)
    (Optional, Functional option) Number of beginning characters left unchanged for fuzzy matching.

  • WithFuzzyTranspositions (bool)
    (Optional, Functional option) If true, edits for fuzzy matching include transpositions of adjacent characters.

  • WithLenient (bool)
    (Optional, Functional option) If true, format-based errors are ignored.

  • WithMaxDeterminizedStates (int)
    (Optional, Functional option) Maximum number of automaton states required for the query.

  • WithMinimumShouldMatch (string)
    (Optional, Functional option) Minimum number of clauses that must match for a document to be returned.

  • WithQuoteAnalyzer (string)
    (Optional, Functional option) Analyzer used to convert quoted text in the query string into tokens.

  • WithPhraseSlop (int)
    (Optional, Functional option) Maximum number of positions allowed between matching tokens for phrases.

  • WithQuoteFieldSuffix (string)
    (Optional, Functional option) Suffix appended to quoted text in the query string.

  • WithRewrite (Rewrite)
    (Optional, Functional option) Method used to rewrite the query. Valid values are:

    • constant_score
    • scoring_boolean
    • constant_score_boolean
    • top_terms_N
    • top_terms_boost_N
    • top_terms_blended_freqs_N
  • WithTimeZone (string)
    (Optional, Functional option) UTC offset or IANA time zone used to convert date values in the query string to UTC.

Additional Information

For more details on the query string query and its parameters, refer to the official Elasticsearch documentation on query string queries.