Skip to content

NullPointerException in IntervalQueryBuilder.toString() #50036

@prasad2kin

Description

@prasad2kin

Elasticsearch version (bin/elasticsearch --version): Version: 7.3.2, Build: oss/tar/1c1faf1/2019-09-06T14:40:30.409026Z, JVM: 1.8.0_162

Plugins installed: All defaults i.e [aggs-matrix-stats, analysis-common, ingest-common, ingest-geoip, ingest-user-agent, lang-expression, lang-mustache, lang-painless, mapper-extras, parent-join, percolator, rank-eval, reindex, repository-url, transport-netty4, advanced-scripts-plugin, analysis-kuromoji, analysis-smartcn]

JVM version (java -version): 1.8.0_162

OS version (uname -a if on a Unix-like system): Ubuntu 18.04

Description of the problem including expected versus actual behavior:

A NullPointerException is thrown when toString() method is called on an instance of IntervalQueryBuilder.

Steps to reproduce:
Create the following query using the Java code as follows:

{
	"intervals": {
		"test": {
			"all_of": {
				"intervals": [
					{
						"match": {
							"query": "audio"
						}
					},
					{
						"match": {
							"query": "method"
						}
					}
				],
				"filter": {
					"script": {
						"source": "interval.gaps > distance",
						"params": {
							"distance": 2
						}
					}
				}
			}
		}
	}
}

Use the java code as follows:

    private IntervalsSourceProvider createSource(int distance){
        try {
            XContentBuilder xb = jsonBuilder()
                            .startObject()
                                .startObject("all_of")
                                    .startArray("intervals")
                                        .startObject()
                                            .startObject("match")
                                                .field("query", "audio")
                                            .endObject()
                                        .endObject()
                                        .startObject()
                                        .startObject("match")
                                        .field("query", "method")
                                        .endObject()
                                        .endObject()
                                    .endArray()
                                    .startObject("filter")
                                        .startObject("script")
                                            .field("source", "interval.gaps > distance")
                                            .startObject("params")
                                                .field("distance", distance)
                                            .endObject()
                                        .endObject()
                                    .endObject()
                                .endObject()
                            .endObject();
            try(XContentParser parser = XContentFactory.xContent(XContentType.JSON)
                    .createParser(NamedXContentRegistry.EMPTY,
                            DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(xb).streamInput())) {

                parser.nextToken(); // advance past the first starting object
                parser.nextToken();
                IntervalsSourceProvider is = IntervalsSourceProvider.fromXContent(parser);
                System.out.println((new IntervalQueryBuilder("test", is)).toString());
                return is;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

You will see NPE during System.out.println((new IntervalQueryBuilder("test", is)).toString());

Provide logs (if relevant):

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)
	at org.elasticsearch.index.query.IntervalQueryBuilder.doXContent(IntervalQueryBuilder.java:69)
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:83)
	at org.elasticsearch.common.Strings.toString(Strings.java:778)
	at org.elasticsearch.index.query.AbstractQueryBuilder.toString(AbstractQueryBuilder.java:363)

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Search/SearchSearch-related issues that do not fall into other categories

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions