# Span\_or not applying higher score when matching multiple unique terms

**URL:** <https://discuss.elastic.co/t/span-or-not-applying-higher-score-when-matching-multiple-unique-terms/356027>\
**Category:** Elasticsearch\
**Created:** [March 23, 2024, 9:34am UTC](https://discuss.elastic.co/t/span-or-not-applying-higher-score-when-matching-multiple-unique-terms/356027 "2024-03-23T09:34:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![robmartin11](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robmartin11/32/99177_2.png) [@robmartin11](https://discuss.elastic.co/u/robmartin11)\
**Post date:** [March 23, 2024, 9:34am UTC](https://discuss.elastic.co/t/span-or-not-applying-higher-score-when-matching-multiple-unique-terms/356027/1 "2024-03-23T09:34:53Z")

</div>

Normally when applying multiple terms clauses, the relevancy scoring is applied to each term and then summed together, meaning matches on multiple unique terms get higher scores than multiple matches on the same term (because of the` k1` value in BM25).  
When using span\_term within span\_or it seems all terms are treated the same so that uniqueness of terms makes no difference.  
For example

```auto
POST /people
{
    "mappings": {
        "properties": {
            "name": {
                "type": "text"
            }
        }
    }
}

```

```auto
POST /people/_bulk
{ "index": { "_id": "1" } }
{ "name": "Rob" }
{ "index": { "_id": "2" } }
{ "name": "Rob Rob" }
{ "index": { "_id": "3" } }
{ "name": "Martin Martin" }
{ "index": { "_id": "4" } }
{ "name": "Rob J Martin" }
{ "index": { "_id": "5" } }
{ "name": "Martin" }

```

```auto
GET people/_search
{
    "query": {
        "span_near": {
            "clauses": [
                {
                    "span_or": {
                        "clauses": [
                            {
                                "span_term": {
                                    "name": "rob"
                                }
                            },
                            {
                                "span_term": {
                                    "name": "martin"
                                }
                            }
                        ]
                    }
                }
            ],
            "slop": 3,
            "in_order": false
        }
    }
}

```

This results in

```auto
{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.437324,
        "hits": [
            {
                "_index": "people",
                "_id": "2",
                "_score": 1.437324,
                "_source": {
                    "name": "Rob Rob"
                }
            },
            {
                "_index": "people",
                "_id": "3",
                "_score": 1.437324,
                "_source": {
                    "name": "Martin Martin"
                }
            },
            {
                "_index": "people",
                "_id": "1",
                "_score": 1.3175471,
                "_source": {
                    "name": "Rob"
                }
            },
            {
                "_index": "people",
                "_id": "5",
                "_score": 1.3175471,
                "_source": {
                    "name": "Martin"
                }
            },
            {
                "_index": "people",
                "_id": "4",
                "_score": 1.2482024,
                "_source": {
                    "name": "Rob J Martin"
                }
            }
        ]
    }
}

```

Id normally expect `Rob J Martin` to get the highest score as it contains both terms but it actually appears last!

The explain query confirms that for that record both `Rob` and `Martin` are treated as the same term (`phraseFreq=2.0`)

````auto
{
    "_index": "people",
    "_id": "4",
    "matched": true,
    "explanation": {
        "value": 1.2482024,
        "description": "weight(spanOr([name:rob, name:martin]) in 3) [PerFieldSimilarity], result of:",
        "details": [
            {
                "value": 1.2482024,
                "description": "score(freq=2.0), computed as boost * idf * tf from:",
                "details": [
                    {
                        "value": 2.2,
                        "description": "boost",
                        "details": []
                    },
                    {
                        "value": 1.077993,
                        "description": "idf, sum of:",
                        "details": [
                            {
                                "value": 0.5389965,
                                "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                                "details": [
                                    {
                                        "value": 3,
                                        "description": "n, number of documents containing term",
                                        "details": []
                                    },
                                    {
                                        "value": 5,
                                        "description": "N, total number of documents with field",
                                        "details": []
                                    }
                                ]
                            },
                            {
                                "value": 0.5389965,
                                "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                                "details": [
                                    {
                                        "value": 3,
                                        "description": "n, number of documents containing term",
                                        "details": []
                                    },
                                    {
                                        "value": 5,
                                        "description": "N, total number of documents with field",
                                        "details": []
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "value": 0.5263158,
                        "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                        "details": [
                            {
                                "value": 2.0,
                                "description": "phraseFreq=2.0",
                                "details": []
                            },
                            {
                                "value": 1.2,
                                "description": "k1, term saturation parameter",
                                "details": []
                            },
                            {
                                "value": 0.75,
                                "description": "b, length normalization parameter",
                                "details": []
                            },
                            {
                                "value": 3.0,
                                "description": "dl, length of field",
                                "details": []
                            },
                            {
                                "value": 1.8,
                                "description": "avgdl, average length of field",
                                "details": []
                            }
                        ]
                    }
                ]
            }
        ]
    }
}```
````

---

<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:** [April 20, 2024, 9:35am UTC](https://discuss.elastic.co/t/span-or-not-applying-higher-score-when-matching-multiple-unique-terms/356027/2 "2024-04-20T09:35:36Z")

</div>

Closed on April 20, 2024, 9:35am UTC

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