|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Span queries |
| 4 | +has_children: true |
| 5 | +nav_order: 60 |
| 6 | +redirect_from: |
| 7 | + - /opensearch/query-dsl/span-query/ |
| 8 | + - /query-dsl/query-dsl/span-query/ |
| 9 | + - /query-dsl/span-query/ |
| 10 | + - /query-dsl/span/index/ |
| 11 | +--- |
| 12 | + |
| 13 | +# Span queries |
| 14 | + |
| 15 | +You can use span queries to perform precise positional searches. Span queries are low-level, specific queries that provide control over the order and proximity of specified query terms. They are primarily used to search legal documents and patents. |
| 16 | + |
| 17 | +Span queries include the following query types: |
| 18 | + |
| 19 | +- [**Span containing**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-containing/): Returns larger spans that contain smaller spans within them. Useful for finding specific terms or phrases within a broader context. The opposite of `span_within` query. |
| 20 | + |
| 21 | +- [**Span field masking**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-field-masking/): Allows span queries to work across different fields by making one field appear as another. Particularly useful when the same text is indexed using different analyzers. |
| 22 | + |
| 23 | +- [**Span first**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-first/): Matches terms or phrases that appear within a specified number of positions from the start of a field. Useful for finding content at the beginning of text. |
| 24 | + |
| 25 | +- [**Span multi-term**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-multi-term/): Enables multi-term queries (like prefix, wildcard, or fuzzy) to work within span queries. Allows for more flexible matching patterns in span searches. |
| 26 | + |
| 27 | +- [**Span near**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-near/): Finds terms or phrases that appear within a specified distance of each other. Can require matches to appear in a specific order and control how many words can appear between them. |
| 28 | + |
| 29 | +- [**Span not**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-not/): Excludes matches that overlap with another span query. Useful for finding terms except when they appear in specific phrases or contexts. |
| 30 | + |
| 31 | +- [**Span or**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-or/): Matches documents that satisfy any of the provided span queries. Combines multiple span patterns with OR logic. |
| 32 | + |
| 33 | +- [**Span term**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-term/): The basic building block for span queries. Matches a single term while maintaining position information for use in other span queries. |
| 34 | + |
| 35 | +- [**Span within**]({{site.url}}{{site.baseurl}}/query-dsl/span/span-within/): Returns smaller spans that are enclosed by larger spans. The opposite of `span_containing` query. |
| 36 | + |
| 37 | +## Setup |
| 38 | + |
| 39 | +To try the examples in this section, use the following steps to configure an example index. |
| 40 | + |
| 41 | +### Step 1: Create an index |
| 42 | + |
| 43 | +First, create an index for an e-commerce clothing website. The `description` field uses the default `standard` analyzer, while the `description.stemmed` subfield applies the `english` analyzer to enable stemming: |
| 44 | + |
| 45 | +```json |
| 46 | +PUT /clothing |
| 47 | +{ |
| 48 | + "mappings": { |
| 49 | + "properties": { |
| 50 | + "description": { |
| 51 | + "type": "text", |
| 52 | + "analyzer": "standard", |
| 53 | + "fields": { |
| 54 | + "stemmed": { |
| 55 | + "type": "text", |
| 56 | + "analyzer": "english" |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | +{% include copy-curl.html %} |
| 65 | + |
| 66 | +### Step 2: Index data |
| 67 | + |
| 68 | +Index sample documents into the index: |
| 69 | + |
| 70 | +```json |
| 71 | +POST /clothing/_doc/1 |
| 72 | +{ |
| 73 | + "description": "Long-sleeved dress shirt with a formal collar and button cuffs. " |
| 74 | +} |
| 75 | + |
| 76 | +``` |
| 77 | +{% include copy-curl.html %} |
| 78 | + |
| 79 | +```json |
| 80 | +POST /clothing/_doc/2 |
| 81 | +{ |
| 82 | + "description": "Beautiful long dress in red silk, perfect for formal events." |
| 83 | +} |
| 84 | +``` |
| 85 | +{% include copy-curl.html %} |
| 86 | + |
| 87 | +```json |
| 88 | +POST /clothing/_doc/3 |
| 89 | +{ |
| 90 | + "description": "Short-sleeved shirt with a button-down collar, can be dressed up or down." |
| 91 | +} |
| 92 | +``` |
| 93 | +{% include copy-curl.html %} |
| 94 | + |
| 95 | +```json |
| 96 | +POST /clothing/_doc/4 |
| 97 | +{ |
| 98 | + "description": "A set of two midi silk shirt dresses with long sleeves in black. " |
| 99 | +} |
| 100 | +``` |
| 101 | +{% include copy-curl.html %} |
0 commit comments