Polymorphism

Polymorphism is a powerful feature of analysis JSON that lets you set each component- or stage-typed property to contain any definition, as long as it matches the required type.

To explain stage and component polymorphism, let's circle back to the basic document search request we introduced at the start of this tutorial:

{
  "stages": {
    "documents": {
      "type": "documents:byQuery",
      "query": {
        "type": "query:string",
        "query": "photon"
      },
      "limit": 50
    }
  }
}

The query property accepts any query component, that is a component whose type starts with the query: prefix. By replacing the string query with a different query component, we can turn the basic document search into example-based similar document search.

Filtering similar documents

Let's extend our request with filtering of the similar document list, so that the list contains only documents whose category field is not astro-ph. This helps to illustrate the concept of component composition, where you build a compound component from a number of components of the same type.

The similar stage of the original request was the following:

"similar": {
  "type": "documents:byQuery",
  "query": {
    "type": "query:forLabels",
    "labels": {
      "type": "labels:reference",
      "use": "seedLabels"
    }
  },
  "limit": 50
}

To apply filtering to the similar document search result, let's replace the query:​for​Labels component with query:​filter:

"similar": {
  "type": "documents:byQuery",
  "query": {
    "type": "query:filter",
    "query": {
      "type": "query:forLabels",
      "labels": {
        "type": "labels:reference",
        "use": "seedLabels"
      }
    },
    "filter": {
      "type": "query:complement",
      "query": {
        "type": "query:string",
        "query": "category:astro-ph"
      }
    }
  },
  "limit": 50
}

query:​filter composes two queries you provide in the query and filter properties. The former is the query for our unfiltered similar document list, the latter is the additional query each document must also meet. Note that our filter query is itself a composite that "negates" the provided string query.

Composition and polymorphism are powerful patterns you can use to build complex analysis requests. For more ideas around the search for semantically-similar document, see the Similar document retrieval chapter.