This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

Prefix query

Use the prefix query to search for terms that begin with a specific prefix. For example, the following query searches for documents in which the speaker field contains a term that starts with KING H:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "prefix": {
  5. "speaker": "KING H"
  6. }
  7. }
  8. }

copy

To provide parameters, you can use a query equivalent to the preceding one with the following extended syntax:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "prefix": {
  5. "speaker": {
  6. "value": "KING H"
  7. }
  8. }
  9. }
  10. }

copy

Parameters

The query accepts the name of the field (<field>) as a top-level parameter:

  1. GET _search
  2. {
  3. "query": {
  4. "prefix": {
  5. "<field>": {
  6. "value": "sample",
  7. ...
  8. }
  9. }
  10. }
  11. }

copy

The <field> accepts the following parameters. All parameters except value are optional.

ParameterData typeDescription
valueStringThe term to search for in the field specified in <field>.
case_insensitiveBooleanIf true, allows case-insensitive matching of the value with the indexed field values. Default is false (case sensitivity is determined by the field’s mapping).
rewriteStringDetermines how OpenSearch rewrites and scores multi-term queries. Valid values are constant_score, scoring_boolean, constant_score_boolean, top_terms_N, top_terms_boost_N, and top_terms_blended_freqs_N. Default is constant_score.

If search.allow_expensive_queries is set to false, prefix queries are not run. If index_prefixes is enabled, the search.allow_expensive_queries setting is ignored and an optimized query is built and run.