Using Distance Feature query with matches as possible origins

If my index is comprised of documents with a "date" field, how can I find all the closest neighbors considering there may be multiple origins?

For example, we're interested in finding movies that were released around any movie the given "director" has directed.

The index:

{
  "mappings": {
    "properties": {
      "director": {"type": "keyword"},
      "release_date": {"type": "date"}
    }
  }
}

This would be the query to find all movies directed by Tarantino:

{
  "query": {
    "bool": {
      "filter": {"term": {"director": "Tarantino"}}
    }
  }
}

Then the query for finding neighbors of the results of the first query. I don't know what I should put in the "origin" in order to reference the other query.

{
  "query": {
    "bool": {
      "should": {
        "distance_feature": {
          "field": "release_date",
          "pivot": "30d",
          "origin": "how do I reference the release_dates of the first query?"
        }
      }
    }
  }
}

Is using a scripted query the right way to do this? The docs about distance-feature don't mention it, or how to use sub-queries.

As you rightly mentioned, the distance feature query only supports a single point of origin, as otherwise scoring would become pretty complex (with overlapping origins for example).

I think your use-case is either a classic n+1 problem, where each movie requires and additional query (or two, one above, one below the movie), or you could index that information for each movie in its document and thus prevent another query.

Maybe you can solve this by rethinking your indexing strategy.

--Alex

Thanks for the insight @spinscale. I will look into better ways to search my data.

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