# Synonyms Exact match Multiword Search

**URL:** <https://discuss.elastic.co/t/synonyms-exact-match-multiword-search/325439>\
**Category:** Elasticsearch\
**Created:** [February 14, 2023, 6:40am UTC](https://discuss.elastic.co/t/synonyms-exact-match-multiword-search/325439 "2023-02-14T06:40:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil5](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@Sahil5](https://discuss.elastic.co/u/Sahil5)\
**Post date:** [February 14, 2023, 6:40am UTC](https://discuss.elastic.co/t/synonyms-exact-match-multiword-search/325439/1 "2023-02-14T06:40:28Z")

</div>

Hi Team,

We are looking for solution to search synonyms with exact match.

For Example User is searching string - **abc xyz**

**abc** has synonyms - **abc1 abc2**  
**xyz** has synonyms - **xyz1 xyz2**

**Data in Index**  
Article1 - test abc1 abc2  
Article2 - test abc1  
Article3 - test xyz1 xyz2  
Article4 - test xyz1  
Article4 - test xyz2  
Article5 - test xyz1 data xyz2  
Article6 - test abc1 data abc2  
Article7 - abc abc1

**Expected Result in Response-**

1. Article1
2. Article3

Please let me know which approach should we take to acheive this.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 14, 2023, 6:08pm UTC](https://discuss.elastic.co/t/synonyms-exact-match-multiword-search/325439/2 "2023-02-14T18:08:12Z")

</div>

Hi @Sahil5

I did a example but divide terms search.

ex:

```auto
GET test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": "abc"
          }
        },
        {
          "match_phrase": {
            "name": "xyz"
          }
        }
      ]
    }
  }
}

```

Index

```auto
PUT test
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym_analyzer": {
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "synonym_filter"
            ]
          }
        },
        "filter": {
          "synonym_filter": {
            "type": "synonym_graph",
            "expand": true,
            "synonyms": [
              "abc => abc1 abc2",
              "xyz => xyz1 xyz2"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "search_analyzer": "synonym_analyzer"
      }
    }
  }
}

```

Doc

```auto
POST test/_bulk
{"index":{}}
{"name":"Article1 - test abc1 abc2"}
{"index":{}}
{"name":"Article2 - test abc1"}
{"index":{}}
{"name":"Article3 - test xyz1 xyz2"}
{"index":{}}
{"name":"Article4 - test xyz1"}
{"index":{}}
{"name":"Article4 - test xyz2"}
{"index":{}}
{"name":"Article5 - test xyz1 data xyz2"}
{"index":{}}
{"name":"Article6 - test abc1 data abc2"}
{"index":{}}
{"name":"Article7 - abc abc1"}

```

Results:

````auto
    "hits": [
      {
        "_index": "test",
        "_id": "UiYaUYYBXRBApbJcGNqx",
        "_score": 1.9216721,
        "_source": {
          "name": "Article1 - test abc1 abc2"
        }
      },
      {
        "_index": "test",
        "_id": "VCYaUYYBXRBApbJcGNqx",
        "_score": 1.8387749,
        "_source": {
          "name": "Article3 - test xyz1 xyz2"
        }
      }
    ]```
````

---

<div class="post-metadata">

**Author:** ![Sahil5](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@Sahil5](https://discuss.elastic.co/u/Sahil5)\
**Post date:** [February 15, 2023, 11:51am UTC](https://discuss.elastic.co/t/synonyms-exact-match-multiword-search/325439/3 "2023-02-15T11:51:20Z")

</div>

Hi @RabBit_BR,

Thanks, but there are some performance issues with this approach.  
Suppose user has entered a string of 10 words then we needs to hit 10 match\_phrase queries which will slow down the response time or may leads to heavy load on server.

More the number of words the query will be more heavy.

Any thoughts on this.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 15, 2023, 12:34pm UTC](https://discuss.elastic.co/t/synonyms-exact-match-multiword-search/325439/4 "2023-02-15T12:34:42Z")

</div>

Did you measure the impact of the query with 10 words?  
I've written queries with up to 6 clauses and the execution time was quite good.  
It is also quite unusual for a user to search for a text with 10 words.

You can ignore the part of dividing the terms but with that you would ask for your requirement to obtain the Article1 and Article3 docs, also returning the Article7 doc.

Anyway, it was a quick simulation I did, you can improve what was presented.

---

<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:** [March 15, 2023, 12:34pm UTC](https://discuss.elastic.co/t/synonyms-exact-match-multiword-search/325439/5 "2023-03-15T12:34:44Z")

</div>

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