Hi, I'm trying to parse "term query" as JSON string into TermQueryBuilder using TermQueryBuilder.fromXContent();
I looked up Java codes and tried to figure out the way I can parse Json string into TermQueryBuilder.
My intention was I wanted to handle json queries into Java vo objects using Elasticsearch itself which I think it's a sophisticated way.
Is anybody helps me what I've done wroing? it emmitted the exception below.
Thanks in advance.
public class QueryBuilderTest {
private final List<NamedWriteableRegistry.Entry> namedWriteables = new ArrayList<>();
private final List<NamedXContentRegistry.Entry> namedXContents = new ArrayList<>();
@Test
public void test() throws IOException {
String term = " {\n" +
" \"term\": {\n" +
" \"poc_category\": {\n" +
" \"value\": \"sample\"\n" +
" }\n" +
" }\n" +
" }";
System.out.println(term);
XContentType xContentType = XContentType.JSON;
registerQuery(new SearchPlugin.QuerySpec<>(TermQueryBuilder.NAME, TermQueryBuilder::new, TermQueryBuilder::fromXContent));
NamedXContentRegistry registry = new NamedXContentRegistry(namedXContents);
XContentParser parser = xContentType.xContent().createParser(registry, term);
QueryParseContext parseContext = new QueryParseContext(parser);
TermQueryBuilder.fromXContent(parseContext);
}
private void registerQuery(SearchPlugin.QuerySpec<?> spec) {
namedWriteables.add(new NamedWriteableRegistry.Entry(QueryBuilder.class, spec.getName().getPreferredName(), spec.getReader()));
// Using Optional here is fairly horrible, but in master we don't have to because the builders return QueryBuilder.
namedXContents.add(new NamedXContentRegistry.Entry(Optional.class, spec.getName(),
(XContentParser p, Object c) -> (Optional) spec.getParser().fromXContent((QueryParseContext) c)));
}
}
java.lang.NullPointerException: fieldName cannot be null
at java.util.Objects.requireNonNull(Objects.java:228)
at org.elasticsearch.common.ParseField.match(ParseField.java:108)
at org.elasticsearch.index.query.QueryParseContext.isDeprecatedSetting(QueryParseContext.java:60)
at org.elasticsearch.index.query.TermQueryBuilder.fromXContent(TermQueryBuilder.java:98)
at com.melon.QueryBuilderTest.test(QueryBuilderTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
java.lang.NullPointerException: fieldName cannot be null
at java.util.Objects.requireNonNull(Objects.java:228)
at org.elasticsearch.common.ParseField.match(ParseField.java:108)
at org.elasticsearch.index.query.QueryParseContext.isDeprecatedSetting(QueryParseContext.java:60)
at org.elasticsearch.index.query.TermQueryBuilder.fromXContent(TermQueryBuilder.java:98)
at com.melon.QueryBuilderTest.test(QueryBuilderTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.