# How to match spans near the beginning of a field

**URL:** <https://discuss.elastic.co/t/how-to-match-spans-near-the-beginning-of-a-field/288055>\
**Category:** Elasticsearch\
**Created:** [October 30, 2021, 6:55am UTC](https://discuss.elastic.co/t/how-to-match-spans-near-the-beginning-of-a-field/288055 "2021-10-30T06:55:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kev86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kev86/32/82948_2.png) [@Kev86](https://discuss.elastic.co/u/Kev86)\
**Post date:** [October 30, 2021, 6:55am UTC](https://discuss.elastic.co/t/how-to-match-spans-near-the-beginning-of-a-field/288055/1 "2021-10-30T06:55:47Z")

</div>

I have this query and want a span first in SearchField. So that ES shows the string that match closest to the beginning of the field first:

```auto
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "FullName": {
                            "query": "101"
                        }
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "SearchField": {
                                        "minimum_should_match": "100%",
                                        "query": "101"
                                    }
                                }
                            },
                            {
                                "match": {
                                    "__deleted": {
                                        "query": "false"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

```

I tried by adding span\_first but it didn't return the same result. Got less hits.

```auto
"bool": {
                        "must": [
                            {
                                "span_first": {
                                    "end": 1,
                                    "match": {
                                        "span_term": {
                                            "SearchField": {
                                                "value": "101"
                                            }
                                        }
                                    }
                                }
                            },
                            {
                                "match": {
                                    "__deleted": {
                                        "query": "false"
                                    }
                                }
                            }
                        ]
                    }

```

Any idea how to query it?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 27, 2021, 6:56am UTC](https://discuss.elastic.co/t/how-to-match-spans-near-the-beginning-of-a-field/288055/2 "2021-11-27T06:56:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
