# ORing a text field with a unique identifier keyword field leading to increased next\_doc count and poor performance

**URL:** <https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283>\
**Category:** Elasticsearch\
**Created:** [March 22, 2023, 6:11pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283 "2023-03-22T18:11:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![helderdias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/helderdias/32/118779_2.png) [@helderdias](https://discuss.elastic.co/u/helderdias)\
**Post date:** [March 22, 2023, 6:11pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/1 "2023-03-22T18:11:13Z")

</div>

TL;DR: I want to find documents that match a certain query (e.g. "my search") OR match the unique ID of a document.

When I search for the text field alone, the search is super fast. However, when I or the text field with the keyword field search becomes extremely slow. Looking at the profile data, it seems like the shard that contains the document ends up doing a lot more `next_doc` than the other shards. Why would that be the case?

Below are the mappings and queries I executed for reference.

Mapping:

```auto
{
  "mappings": {
    "properties": {
      "searchable_name_1": { "type": "text" },  
      "searchable_name_2": { "type": "text" }, 
      "unique_identifier": { "type": "keyword" }     
    }
  }
}

```

The following query executes super fast:

```auto
{
  "profile": true,
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "my search",
            "fields": [
              "searchable_name_1",
              "searchable_name_2"
            ]
          }
        }
      ]
    }
  }
}

```

 ![Screenshot 2023-03-22 at 11.03.51 AM](https://us1.discourse-cdn.com/elastic/original/3X/6/b/6bed05b8d86c2a9440052b90d3944a108e7ba49d.png)

However, when I or the unique identifier with the text query, it becomes super slow:

```auto
{
  "profile": true,
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "my search",
            "fields": [
              "searchable_name_1",
              "searchable_name_2"
            ]
          }
        },
        {
          "term": {
            "unique_identifier": {
              "value": "13590585",
              "boost": 10
            }
          }
        }
      ]
    }
  }
}

```

 ![Screenshot 2023-03-22 at 11.02.44 AM](https://us1.discourse-cdn.com/elastic/original/3X/8/8/883f6c35964f1bd4fdd223c2cfb7bee74f9c7372.png)  
 ![Screenshot 2023-03-22 at 11.12.02 AM](https://us1.discourse-cdn.com/elastic/original/3X/b/f/bff16a5bb27c5983a01bb73e3d4e5a5f587ed276.png)

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [March 22, 2023, 7:36pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/2 "2023-03-22T19:36:24Z")

</div>

Hi  
Sanity check - the mapped field is called “unique\_identifier” and the query is “unique\_id” in your example

---

<div class="post-metadata">

**Author:** ![helderdias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/helderdias/32/118779_2.png) [@helderdias](https://discuss.elastic.co/u/helderdias)\
**Post date:** [March 22, 2023, 7:49pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/3 "2023-03-22T19:49:19Z")

</div>

Thanks for your reply. It was a copy-paste mistake while anonymizing the use case. Fixed in the original post.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [March 22, 2023, 8:30pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/4 "2023-03-22T20:30:49Z")

</div>

Thanks. How many docs have this unique ID of 13590585?

---

<div class="post-metadata">

**Author:** ![helderdias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/helderdias/32/118779_2.png) [@helderdias](https://discuss.elastic.co/u/helderdias)\
**Post date:** [March 22, 2023, 8:44pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/5 "2023-03-22T20:44:07Z")

</div>

Just one. That's why I'm confused why ORing the two queries would result in such latency since the posting list for that ID has only one doc.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [March 22, 2023, 9:03pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/6 "2023-03-22T21:03:23Z")

</div>

Hmm. Then I expect that the addition of a clause is causing a change to scoring that means [an optimisation](https://www.elastic.co/blog/faster-retrieval-of-top-hits-in-elasticsearch-with-block-max-wand) is not kicking in.

If you set “track\_total\_hits” to true in both queries then this might help compare speeds with the shortcut optimisation disabled.  
(It won’t solve your problem but would help with the diagnostics here)

---

<div class="post-metadata">

**Author:** ![helderdias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/helderdias/32/118779_2.png) [@helderdias](https://discuss.elastic.co/u/helderdias)\
**Post date:** [March 22, 2023, 9:28pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/7 "2023-03-22T21:28:03Z")

</div>

I enabled `track_total_hits` on both queries.

Fast one (just match query): 5323035  
Slow one (match query OR keyword term): 5323036

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [March 22, 2023, 11:00pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/8 "2023-03-22T23:00:16Z")

</div>

I presume that’s the number of hits. How do the response times compare?

---

<div class="post-metadata">

**Author:** ![helderdias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/helderdias/32/118779_2.png) [@helderdias](https://discuss.elastic.co/u/helderdias)\
**Post date:** [March 22, 2023, 11:24pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/9 "2023-03-22T23:24:18Z")

</div>

That's correct.

Shard response time for match only:

 ![Screenshot 2023-03-22 at 4.22.59 PM](https://us1.discourse-cdn.com/elastic/original/3X/8/c/8ca6db7fd6018534ac114026a69becef006080ac.png)

Shard response time for match OR keyword term:

 ![Screenshot 2023-03-22 at 4.22.42 PM](https://us1.discourse-cdn.com/elastic/original/3X/9/5/95706d941fdb50362c530977fe9ca0cdb2b4bb63.png)

By the way, thanks in advance for your help, Mark!

---

<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 19, 2023, 11:25pm UTC](https://discuss.elastic.co/t/oring-a-text-field-with-a-unique-identifier-keyword-field-leading-to-increased-next-doc-count-and-poor-performance/328283/10 "2023-04-19T23:25:10Z")

</div>

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