# Span\_Near and Span\_or query for two multiword match is not giving expected result

**URL:** <https://discuss.elastic.co/t/span-near-and-span-or-query-for-two-multiword-match-is-not-giving-expected-result/334862>\
**Category:** Elasticsearch\
**Created:** [May 31, 2023, 5:27pm UTC](https://discuss.elastic.co/t/span-near-and-span-or-query-for-two-multiword-match-is-not-giving-expected-result/334862 "2023-05-31T17:27:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chetab](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chetab/32/121685_2.png) [@chetab](https://discuss.elastic.co/u/chetab)\
**Post date:** [May 31, 2023, 5:27pm UTC](https://discuss.elastic.co/t/span-near-and-span-or-query-for-two-multiword-match-is-not-giving-expected-result/334862/1 "2023-05-31T17:27:55Z")

</div>

I need to write the query for below scenario: Ex: The car will be **getting close** to me but I am unable to **stop** it. or The car **is too close** to me but I am unable to **stop** it.

like:  
Span\_near(span\_or("getting close", "is too close"), span\_or(stopped, stop, stopping))

1. the distance between either " **getting close**" or " **is too close**" and from **stopped/stop/stopping** should be 10

The below query I tried:

```auto

GET /_search
{
  "query": {
  "bool":{
  "should":[
  {
          
  "span_near": {
    "clauses":
                [
                {"span_or": 
                        {"clauses":[
                        {"span_near": {
                                    "clauses": [
                                                { "span_term": { "textContent": "getting" }},
                                                { "span_term": { "textContent": "close"}}],
                                    "slop": 0,
                                    "in_order": true
                                    }},
                        {"span_near": {
                                    "clauses": [
                                                { "span_term": { "textContent": "is" }},
                                                { "span_term": { "textContent": "too" }},
                                                { "span_term": { "textContent": "close"}}],
                                    "slop": 0,
                                    "in_order": true
                                    }}
                        ]}},
                {"span_or": 
                {"clauses":[{"span_term": {"textContent": "stopped"}},
                            {"span_term": {"textContent": "stop"}},
                            {"span_term": {"textContent": "stopping"}}]}}       
                ],
    "slop":10,
    "in_order": false
  }  
  }
  ]
}
}
}

```

Can anyone suggest the alternate way to do it.  
Thanks in advance

---

<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:** [June 28, 2023, 5:28pm UTC](https://discuss.elastic.co/t/span-near-and-span-or-query-for-two-multiword-match-is-not-giving-expected-result/334862/2 "2023-06-28T17:28:36Z")

</div>

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