Elastic search: Regex for matching longest string from list of strings in autocomplete suggester

I am very new to elastic search and trying to implement autocomplete suggester with regex queries. Once i receive a query, i am taking last 5 words of query and forming a list of tokens in this format

query - i am trying regex for elasticsearch
tokens - [elasticsearch, for elasticsearch, regex for elasticsearch ...]

My requirement is to identify indexed sentence which matches the longest string from a list of tokens I am having a tough time writing a regular expression for it. can someone please help?

My mapping:

 "mappings": {
"properties": {
  "keywords": {
    "type": "text",
    "fields": {
      "keywords_suggest": {
        "type": "completion"
      }
    }
  },
  "sections": {
    "type": "text",
    "fields": {
      "sections_suggest": {
        "type": "completion"
      }
    }
  },
  "title": {
    "type": "text",
    "fields": {
      "title_suggest": {
        "type": "completion"
      }
    }
  }

this is how I am making a search request

 body = {
        "from": 0, "size": size,
        "query": {
            "multi_match": {
                "query": query,
                "fields": ["title^3", "searchResultPreview^1", "body^5"], #ignore these fields as i only pasted mapping used for completion type
                "fuzziness": "AUTO"
            }
        },
        "suggest": {
            "title-suggest": {
                "regex": regex,
                "completion": {
                    "field": "title.title_suggest",
                    "skip_duplicates": True,
                }
            },
            "keyword-suggest": {
                "regex": regex,
                "completion": {
                    "field": "keywords.keywords_suggest",
                    "skip_duplicates": True,
                }
            },
            "section-suggest": {
                "regex": regex,
                "completion": {
                    "field": "sections.sections_suggest",
                    "skip_duplicates": True,
                }
            }
        }
    }

  search_result = self.es.search(index=index_name, body=body)
Indexed sentence1 - The Real purpose of elasticsearch is unknown
Indexed sentence2 - real function is not defined
query - i want to know the real
list of words - [ real, the real, know the real, to know the real]

I tried following regular expression -

(to know the real|know the real|the real|real)

required output - Indexed sentence1 needs to be matched as it is the longest word in the list that the sentence starts, with but it is showing matches only the sentences that start with real

can someone please tell me, where I am going wrong.

EDIT : I believe case sensitivity is not the issue as the matches for word real are case insensitive

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