# Create IntervalQueryBuilder Filter

**URL:** <https://discuss.elastic.co/t/create-intervalquerybuilder-filter/210536>\
**Category:** Elasticsearch\
**Created:** [December 4, 2019, 12:47pm UTC](https://discuss.elastic.co/t/create-intervalquerybuilder-filter/210536 "2019-12-04T12:47:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasad.deshpande](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@prasad.deshpande](https://discuss.elastic.co/u/prasad.deshpande)\
**Post date:** [December 4, 2019, 12:47pm UTC](https://discuss.elastic.co/t/create-intervalquerybuilder-filter/210536/1 "2019-12-04T12:47:20Z")

</div>

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.

Could anyone please help?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 1, 2020, 12:47pm UTC](https://discuss.elastic.co/t/create-intervalquerybuilder-filter/210536/2 "2020-01-01T12:47:20Z")

</div>

Closed on January 1, 2020, 12:47pm UTC

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