# How to softly exclude ambiguous words?

**URL:** <https://discuss.elastic.co/t/how-to-softly-exclude-ambiguous-words/380249>\
**Category:** Elasticsearch\
**Created:** [July 18, 2025, 4:13am UTC](https://discuss.elastic.co/t/how-to-softly-exclude-ambiguous-words/380249 "2025-07-18T04:13:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-Dragon0302](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s-dragon0302/32/100509_2.png) [@S-Dragon0302](https://discuss.elastic.co/u/S-Dragon0302)\
**Post date:** [July 18, 2025, 4:13am UTC](https://discuss.elastic.co/t/how-to-softly-exclude-ambiguous-words/380249/1 "2025-07-18T04:13:51Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/9/4/9485c7f471ee11ecc0f64b49b107b9961c19e2bb.png)  
Because "apple" is hit, not just "apple care".How to implement this query\_string?

---

<div class="post-metadata">

**Author:** ![Tortoise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tortoise/32/147587_2.png) [@Tortoise](https://discuss.elastic.co/u/Tortoise)\
**Post date:** [July 18, 2025, 6:31am UTC](https://discuss.elastic.co/t/how-to-softly-exclude-ambiguous-words/380249/2 "2025-07-18T06:31:39Z")

</div>

Hello @S-Dragon0302

As per the table :

```auto
POST /fruits/_doc
{
  "message": ["usa", "apple"]
}

POST /fruits/_doc
{
  "message": ["usa", "apple", "banana"]
}

POST /fruits/_doc
{
  "message": ["usa", "apple", "apple care"]
}

POST /fruits/_doc
{
  "message": ["usa", "apple care"]
}

POST /fruits/_doc
{
  "message": ["usa", "apple", "banana", "apple care"]
}

#ambigous word = None
GET /fruits/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "message": "usa" }},
        { "match": { "message": "apple" }}
      ],
      "must_not": [
        { "match": { "message": "banana" }}
      ]
    }
  }
}

#ambigous word = apple care
GET /fruits/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "message": "usa" }},
        { "match": { "message": "apple" }}
      ],
      "must_not": [
        { "match": { "message": "banana" }}
      ],
      "filter": {
        "script": {
          "script": {
            "source": """
              def kws = doc['message.keyword'].size() == 0 ? [] : doc['message.keyword'];
              return !kws.contains('apple care') || kws.contains('apple');
            """,
            "lang": "painless"
          }
        }
      }
    }
  }
}

```

Thanks!!

---

<div class="post-metadata">

**Author:** ![S-Dragon0302](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s-dragon0302/32/100509_2.png) [@S-Dragon0302](https://discuss.elastic.co/u/S-Dragon0302)\
**Post date:** [July 18, 2025, 6:45am UTC](https://discuss.elastic.co/t/how-to-softly-exclude-ambiguous-words/380249/3 "2025-07-18T06:45:50Z")

</div>

Using scripts can achieve this, but the performance is too poor. I have hundreds of billions of data entries, and the storage is at the PB level.

---

<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:** [July 20, 2025, 8:47am UTC](https://discuss.elastic.co/t/how-to-softly-exclude-ambiguous-words/380249/4 "2025-07-20T08:47:13Z")

</div>

Maybe of interest: in [this demo](https://youtu.be/U1RGTMesCq0?t=1382&si=RMAUYUhl__29y7mU) users can choose which interpretation of the ambiguous search term “ice” they want and search for only that. This uses clustering and binary vectors so may require changes to both indexing and user behaviours but does solve this sort of problem.
