How to search by fullName field in OpenSearch?

I have an index in OpenSearch which contains documents like this:

{
  "employee": {
    "id": "4",
    "fullName": "Ivanov Ivan Ivanovich"
  }
}

I want to create a query which can search me documents by fullName field in way like this: “Ivanov Ivan” or “Ivanov I I”.

Let’s assume that index contains this documents:

{
  "employee": {
    "id": "4",
    "fullName": "Ivanov Ivan Ivanovich"
  }
},
{
  "employee": {
    "id": "5",
    "fullName": "Ivanov Gleb Glebovich"
  }
}

I wrote this query:

GET /contact/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase_prefix": {
            "employee.fullName": "Ivanov"
          }
        },
        {
          "match_phrase_prefix": {
            "employee.fullName": "Ivan"
          }
        }
      ]
    }
  }
}

I expect to get a single result of "fullName": "Ivanov Ivan Ivanovich"

But it returns me both results where fullNames are:

"fullName": "Ivanov Ivan Ivanovich"

"fullName": "Ivanov Gleb Glebovich"

It happens because both of these results contain “Ivan” as a part of first “Ivanov”.

Is there a way to modify my query to specify that order of appearance is important?

This application is just a phone book and I want to allow users to search contacts just specifying second name and two first letters of last name and second name.

Okay a few things to look into here. The first would be changing from a phrase_prefix query which can be quite rigid to one (or several) of the alternatives.

Phonetic Analysis: One of the popular ways to allow people to search for names without needing to spell it exactly right is with the analysis-phonetic plugin. So long as people spell a name how it sounds they should get a match. Eg. both Alexander and Aleksandar get transformed into A425 with the phonetic plugin and would result in a match.

N-Grams: These are another way to somewhat achieve what you are looking for with the prefix matching. It breaks the words up into smaller chunks and attempts to match on those. It’s important to mention here that OpenSearch is a search engine so you are looking to query for things that are similar to what you typed rather than using exact matches.

Fuzzyness: This may be another way to get what you are looking for. Say for example you used a match query with employee.fullName: “Ivan Ivan”. If you had a fuzzyness of 2 with that query only “Ivanov Ivan Ivanovich” would return as it’s 2 characters different from the search you typed.

I’d probably recommend against using single letters for search in OpenSearch as letters like ‘a’ ‘I’ often are filtered out by stop word token filters.