2017-07-26 70 views
0

我是新來Elasticserach和傑克遜JSON。有一個簡單的查詢(搜索按國家索引),我嘗試使用JsonGenerator,傑克遜jsonGenerator產生Elasticsearch查詢DSL

"{\n" + 
    " \"query\" : {\n" + 
    " \"match\": { \"exporting_country\":\"" + countryCode + "\"} \n" + 
    "} \n"+ 
    "}" 

JsonGenerator代碼,

 JsonGenerator jGenerator = jfactory.createGenerator(outputStream); 

     jGenerator.writeStartObject(); 
     jGenerator.writeFieldName("query"); 

     jGenerator.writeStartObject(); 

     jGenerator.writeFieldName("match"); 
     jGenerator.writeStringField("exporting_country", countryCode); 

     jGenerator.writeEndObject(); 
     jGenerator.writeEndObject(); 
     jGenerator.close(); 

它拋出了一個錯誤:

Can not write a field name, expecting a value 

回答

0

你」再次錯失接聽電話,以writeStartObject

JsonGenerator jGenerator = jfactory.createGenerator(outputStream); 

    jGenerator.writeStartObject(); 
    jGenerator.writeFieldName("query"); 

    jGenerator.writeStartObject(); 

    jGenerator.writeFieldName("match"); 
    jGenerator.writeStartObject();    <--- add this 
    jGenerator.writeStringField("exporting_country", countryCode); 
    jGenerator.writeEndObject();    <--- and this 

    jGenerator.writeEndObject(); 
    jGenerator.writeEndObject(); 
    jGenerator.close();