# Understanding span queries in Amazon OpenSearch Service

DevFeed: [Understanding span queries in Amazon OpenSearch Service](<https://devfeed.tech/articles/understanding-span-queries-in-amazon-opensearch-service-133390.md>)

Original publisher: [Read original article](<https://aws.amazon.com/blogs/big-data/understanding-span-queries-in-amazon-opensearch-service/>)

Author: Jon Handler

Published: 2026-11-13T22:00:29Z

Content type: article

Language: en

Sources: [AWS Big Data Blog](<https://devfeed.tech/sources/aws-big-data-blog.md>)

Topics: [opensearch](<https://devfeed.tech/topics/opensearch.md>), [Amazon OpenSearch Service](<https://devfeed.tech/topics/amazon-opensearch-service.md>), [search-engines](<https://devfeed.tech/topics/search-engines.md>)

Tags: [amazon](<https://devfeed.tech/tags/amazon.md>), [amazon-opensearch-service](<https://devfeed.tech/tags/amazon-opensearch-service.md>), [aws](<https://devfeed.tech/tags/aws.md>), [aws-cloud](<https://devfeed.tech/tags/aws-cloud.md>), [edit-distance](<https://devfeed.tech/tags/edit-distance.md>), [expert-400](<https://devfeed.tech/tags/expert-400.md>), [opensearch](<https://devfeed.tech/tags/opensearch.md>), [queries](<https://devfeed.tech/tags/queries.md>), [query](<https://devfeed.tech/tags/query.md>), [span](<https://devfeed.tech/tags/span.md>), [technical-how-to](<https://devfeed.tech/tags/technical-how-to.md>)

## AI overview

Span queries let Amazon OpenSearch Service users express term-order, position, proximity, and containment constraints that phrase queries cannot reliably capture. Phrase queries match contiguous terms; `match_phrase` slop allows positional swaps, which can produce matches with terms in an unexpected order. Span queries combine span-only clauses such as `span_term`, `span_near`, `span_first`, `span_not`, `span_containing`, and `span_within`; `in_order` controls clause ordering, while containment queries differ in whether they return the larger or smaller span. The examples show anchoring a term near a field's start and requiring an exact phrase to occur inside a broader span. This precision can help legal, regulatory, patent, scientific, and compliance searches where placement matters, and offers a query-time option when text lacks a structured model. Span queries are more verbose and require careful composition.

## Source excerpt

Among OpenSearch query types, span queries give you precise control over where terms appear in a document and how they relate to one another. Learn how span queries work, when to use them instead of phrase queries, and how to compose them to enforce exact term order, position, and containment that phrase queries cannot express.