# Reverse word search in Elastic

**URL:** <https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173>\
**Category:** Elasticsearch\
**Created:** [May 24, 2023, 6:18am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173 "2023-05-24T06:18:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakhshunda\_Noorein\_J](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakhshunda_noorein_j/32/99407_2.png) [@Rakhshunda\_Noorein\_J](https://discuss.elastic.co/u/Rakhshunda_Noorein_J)\
**Post date:** [May 24, 2023, 6:18am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/1 "2023-05-24T06:18:21Z")

</div>

Hello,

I want to search words in reverse also in Elasticsearch. for Example - "Tpp Info" and "Info Tpp" should give the same result. while in my db the document present is "Tpp Infotech" in the field title.

My query is:

```auto
{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "info tpp",
            "fields": [
            "Title^6","Title._2gram","Title._index_prefix^12"
            ],
            "type": "bool_prefix",
            "operator": "and"
          }
        }
      ]
    }
  }
}

```

---

<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:** [May 24, 2023, 4:20pm UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/2 "2023-05-24T16:20:05Z")

</div>

Hi @Rakhshunda_Noorein_J

Did you try with tokenizer edge-ngram?

```auto
GET _analyze
{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "edge_ngram",
      "min_gram": 2,
      "max_gram": 4
    }
  ],
  "text": [
    "Tpp Infotech"
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Rakhshunda\_Noorein\_J](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakhshunda_noorein_j/32/99407_2.png) [@Rakhshunda\_Noorein\_J](https://discuss.elastic.co/u/Rakhshunda_Noorein_J)\
**Post date:** [May 31, 2023, 6:05am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/3 "2023-05-31T06:05:19Z")

</div>

> [@RabBit\_BR](#):
>
> `_analyze`

It is giving me like this..

```auto
{
    "tokens": [
        {
            "token": "tp",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "tpp",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "In",
            "start_offset": 4,
            "end_offset": 12,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "Inf",
            "start_offset": 4,
            "end_offset": 12,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "Info",
            "start_offset": 4,
            "end_offset": 12,
            "type": "<ALPHANUM>",
            "position": 1
        }
    ]
}

```

I don't understand what will I do with this result???

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 31, 2023, 6:07am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/4 "2023-05-31T06:07:16Z")

</div>

What is the index mapping for the field involved in the query?

The output of the \_analyze API shows you which tokens that are indexed in Elasticsearch. As you can see here the words are tokenized individually and strings from the start that are 2 to 4 characters long are indexed, which should give the result you wanted.

---

<div class="post-metadata">

**Author:** ![Rakhshunda\_Noorein\_J](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakhshunda_noorein_j/32/99407_2.png) [@Rakhshunda\_Noorein\_J](https://discuss.elastic.co/u/Rakhshunda_Noorein_J)\
**Post date:** [May 31, 2023, 6:15am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/5 "2023-05-31T06:15:08Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> result

> Data type of the field is - Search-as-you-type...

Myfield actual value is: `Tpp Infotech`

When i am searching - Tpp Infotech or Infotech Tpp , it is giving me the same result.

but when I am Typing Tpp info, it is giving me the result but not in Info Tpp...

```auto
i.e, Infotech Tpp <-> Tpp Infotech , but Tpp Info !<-> Info Tpp.

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 31, 2023, 6:44am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/6 "2023-05-31T06:44:31Z")

</div>

Please show the full mapping as well as a sample document so others can recreate the issue.

---

<div class="post-metadata">

**Author:** ![Rakhshunda\_Noorein\_J](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakhshunda_noorein_j/32/99407_2.png) [@Rakhshunda\_Noorein\_J](https://discuss.elastic.co/u/Rakhshunda_Noorein_J)\
**Post date:** [May 31, 2023, 7:00am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/7 "2023-05-31T07:00:12Z")

</div>

My document:

```auto
{
"Id":401
"Title": "Tpp Infotech Ltd.",
"Keys": 485rT,
"@timestamp": "2023-05-31T06:35:18.033239100Z"
}

```

Index mapping:

```auto
{
    "my_index_001": {
        "mappings": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "Id": {
                    "type": "long"
                },
                "Keys": {
                    "type": "search_as_you_type",
                    "doc_values": false,
                    "max_shingle_size": 3
                },
                "Title": {
                    "type": "search_as_you_type",
                    "doc_values": false,
                    "max_shingle_size": 3
                }            
            }
        }
    }
}

```

My Query:

```auto
{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "info tpp",
            "fields": [
            "Title^6","Title._2gram","Title._index_prefix^12"
            ],
            "type": "bool_prefix",
            "operator": "and"
          }
        }
      ]
    }
  }
}

```

---

<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:** [May 31, 2023, 10:09am UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/8 "2023-05-31T10:09:32Z")

</div>

> [@Rakhshunda\_Noorein\_J](#):
>
> Myfield actual value is: `Tpp Infotech`
> 
> When i am searching - Tpp Infotech or Infotech Tpp , it is giving me the same result.
> 
> but when I am Typing Tpp info, it is giving me the result but not in Info Tpp...

Below is an example that return the results you expect. I used edge-ngram .

```auto
PUT idx_test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ngram": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "ngram_filter"
          ]
        }
      },
      "filter": {
        "ngram_filter": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 4
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "fields": {
          "ngram": {
            "type": "text",
            "analyzer": "ngram"
          }
        }
      }
    }
  }
}

POST idx_test/_doc
{
  "title": "Tpp Infotech Ltd."
}

GET idx_test/_search
{
  "query": {
    "match": {
      "title.ngram": "info tpp"
    }
  }
}

```

---

<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:** [July 5, 2023, 1:04pm UTC](https://discuss.elastic.co/t/reverse-word-search-in-elastic/334173/10 "2023-07-05T13:04:52Z")

</div>

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