I’m not sure what I want to do is possible. I have data that looks like this:

{
    "Actor1Name": "PERSON",
    "Actor2Name": "OTHERPERSON"
}

I use copy_to in order to populate a secondary field, ActorNames, with both values.

I am trying to build a typeahead capability where a user can start to type a name and it will populate with the top hits for that prefix. I want it to search across both actor fields. The only problem is when I search across ActorNames, I get both values even if only one matches. That means if I’m searching for prefix O that I will get both OTHERPERSON (desired) and PERSON (undesired) in my results based on the above document.

My current solution is to run 2 aggregations and combine them client side, but is it possible to do this purely in ES?

Current query:

{
  "query": {
    "prefix": {
      "ActorNames": "O"
    }
  },
  "aggs": {
    "actor1": {
      "filter": {
        "prefix": {
          "Actor1Name": "O"
        }
      },
      "aggs": {
        "actor1": {
          "terms": {
            "field": "Actor1Name",
          }
        }
      }
    },
    "actor2": {
      "filter": {
        "prefix": {
          "Actor2Name": "O"
        }
      },
      "aggs": {
        "actor2": {
          "terms": {
            "field": "Actor2Name",
          }
        }
      }
    }
  }
}

If you want to check the prefix condition on both the fields, why not use ANDING of prefix on both fields? Like:

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "Actor1Name": "O"
          }
        },
        {
          "prefix": {
            "Actor2Name": "O"
          }
        }
      ]
    }
  }
}

1