Checking for paylods as JSON objects in completion suggester

If the payload is not a JSON object on indexation, an exception will be thrown.
This commit is contained in:
Alexander Reelsen 2013-08-21 14:25:49 +02:00
parent cdddbb7585
commit 210683d70b
2 changed files with 17 additions and 2 deletions

View File

@ -206,6 +206,8 @@ public class CompletionFieldMapper extends AbstractFieldMapper<String> {
XContentBuilder payloadBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser); XContentBuilder payloadBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
payload = payloadBuilder.bytes().toBytesRef(); payload = payloadBuilder.bytes().toBytesRef();
payloadBuilder.close(); payloadBuilder.close();
} else {
throw new MapperException("Payload must be an object");
} }
} else if (token == XContentParser.Token.VALUE_STRING) { } else if (token == XContentParser.Token.VALUE_STRING) {
if ("output".equals(currentFieldName)) { if ("output".equals(currentFieldName)) {

View File

@ -176,6 +176,19 @@ public class CompletionSuggestSearchTests extends AbstractSharedClusterTest {
).get(); ).get();
} }
@Test(expected = MapperException.class)
public void testThatIndexingNonObjectAsPayloadThrowsException() throws Exception {
createIndexAndMapping();
client().prepareIndex(INDEX, TYPE, "1").setSource(jsonBuilder()
.startObject().startObject(FIELD)
.startArray("input").value("Foo Fighters").endArray()
.field("output", "Boo Fighters")
.field("payload", "does not work")
.endObject().endObject()
).get();
}
@Test @Test
public void testDisabledPreserveSeperators() throws Exception { public void testDisabledPreserveSeperators() throws Exception {
createIndexAndMapping("simple", "simple", true, false, true); createIndexAndMapping("simple", "simple", true, false, true);
@ -597,7 +610,7 @@ public class CompletionSuggestSearchTests extends AbstractSharedClusterTest {
.startObject().startObject(FIELD) .startObject().startObject(FIELD)
.startArray("input").value(input[i]).endArray() .startArray("input").value(input[i]).endArray()
.field("output",surface[i]) .field("output",surface[i])
.field("payload", "id: " + i) .startObject("payload").field("id", i).endObject()
.field("weight", 1) // WE FORCEFULLY INDEX A BOGUS WEIGHT .field("weight", 1) // WE FORCEFULLY INDEX A BOGUS WEIGHT
.endObject() .endObject()
.endObject() .endObject()
@ -611,7 +624,7 @@ public class CompletionSuggestSearchTests extends AbstractSharedClusterTest {
.startObject().startObject(FIELD) .startObject().startObject(FIELD)
.startArray("input").value(input[i]).endArray() .startArray("input").value(input[i]).endArray()
.field("output",surface[i]) .field("output",surface[i])
.field("payload", "id: " + i) .startObject("payload").field("id", i).endObject()
.field("weight", weight[i]) .field("weight", weight[i])
.endObject() .endObject()
.endObject() .endObject()