Create IntervalQueryBuilder Filter

Hi,
How do I create an instance of IntervalQueryBuilder for the following snippet? I'm using ES 7.3.2.

  "query": {
    "intervals": {
      "Titles.Title.eng": {
        "all_of": {
          "intervals": [
            {
              "match": {
                "query": "audio"
              }
            },
            {
              "match": {
                "query": "method"
              }
            }
          ],
          "filter": {
            "script": {
              "source": "interval.gaps > 2"
            }
          }
        }
      }
    }
  }

I'm using Java API.
I managed to create something like this:

        IntervalsSourceProvider.IntervalFilter filter = createScriptFilter(pqe.getCalulatedDistance());
        return new IntervalsSourceProvider.Combine(Lists.newArrayList(left, right), false, pqe.getCalulatedDistance(), filter);

private IntervalsSourceProvider.IntervalFilter createScriptFilter(int distance){
    String scriptJson = "{\"script\": {\"source\": \"interval.gaps > distance\", \"params\": {\"distance\": "+distance+"}}}";

    try {
        JsonParser  parser  = factory.createParser(scriptJson);
        XContentParser p = new JsonXContentParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parser);
        p.nextToken();
        return IntervalsSourceProvider.IntervalFilter.fromXContent(p);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

However, there are 2 problems with this approach.

  1. I don't want to create the scriptJson string and then conver to Java object using parser. Surely there has to be an easier way.

  2. Once the object is ready, it can not serialize back to the JSON query. It throws NPE.

    threw exception java.lang.NullPointerException
    at org.elasticsearch.index.query.IntervalsSourceProvider$IntervalFilter.toXContent(IntervalsSourceProvider.java:777)
    at org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:857)
    at org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:850)
    at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:842)
    at org.elasticsearch.index.query.IntervalsSourceProvider$Combine.toXContent(IntervalsSourceProvider.java:422)

Could anyone please help?