parent
210683d70b
commit
25d28f8afa
|
@ -272,7 +272,27 @@ public class JsonXContentGenerator implements XContentGenerator {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeRawField(String fieldName, BytesReference content, OutputStream bos) throws IOException {
|
||||
public final void writeRawField(String fieldName, BytesReference content, OutputStream bos) throws IOException {
|
||||
XContentType contentType = XContentFactory.xContentType(content);
|
||||
if (contentType != null) {
|
||||
writeObjectRaw(fieldName, content, bos);
|
||||
} else {
|
||||
writeFieldName(fieldName);
|
||||
// we could potentially optimize this to not rely on exception logic...
|
||||
String sValue = content.toUtf8();
|
||||
try {
|
||||
writeNumber(Long.parseLong(sValue));
|
||||
} catch (NumberFormatException e) {
|
||||
try {
|
||||
writeNumber(Double.parseDouble(sValue));
|
||||
} catch (NumberFormatException e1) {
|
||||
writeString(sValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeObjectRaw(String fieldName, BytesReference content, OutputStream bos) throws IOException {
|
||||
generator.writeRaw(", \"");
|
||||
generator.writeRaw(fieldName);
|
||||
generator.writeRaw("\" : ");
|
||||
|
|
|
@ -68,7 +68,7 @@ public class SmileXContentGenerator extends JsonXContentGenerator {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeRawField(String fieldName, BytesReference content, OutputStream bos) throws IOException {
|
||||
protected void writeObjectRaw(String fieldName, BytesReference content, OutputStream bos) throws IOException {
|
||||
writeFieldName(fieldName);
|
||||
SmileParser parser;
|
||||
if (content.hasArray()) {
|
||||
|
|
|
@ -68,7 +68,7 @@ public class YamlXContentGenerator extends JsonXContentGenerator {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void writeRawField(String fieldName, BytesReference content, OutputStream bos) throws IOException {
|
||||
protected void writeObjectRaw(String fieldName, BytesReference content, OutputStream bos) throws IOException {
|
||||
writeFieldName(fieldName);
|
||||
YAMLParser parser;
|
||||
if (content.hasArray()) {
|
||||
|
|
|
@ -206,8 +206,10 @@ public class CompletionFieldMapper extends AbstractFieldMapper<String> {
|
|||
XContentBuilder payloadBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
|
||||
payload = payloadBuilder.bytes().toBytesRef();
|
||||
payloadBuilder.close();
|
||||
} else if (token.isValue()) {
|
||||
payload = parser.bytesOrNull();
|
||||
} else {
|
||||
throw new MapperException("Payload must be an object");
|
||||
throw new MapperException("payload doesn't support type " + token);
|
||||
}
|
||||
} else if (token == XContentParser.Token.VALUE_STRING) {
|
||||
if ("output".equals(currentFieldName)) {
|
||||
|
|
|
@ -23,9 +23,11 @@ import org.elasticsearch.common.io.stream.StreamInput;
|
|||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.text.Text;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.search.suggest.Suggest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -87,6 +89,22 @@ public class CompletionSuggestion extends Suggest.Suggestion<CompletionSuggestio
|
|||
return payload;
|
||||
}
|
||||
|
||||
public String getPayloadAsString() {
|
||||
return payload.toUtf8();
|
||||
}
|
||||
|
||||
public long getPayloadAsLong() {
|
||||
return Long.parseLong(payload.toUtf8());
|
||||
}
|
||||
|
||||
public double getPayloadAsDouble() {
|
||||
return Double.parseDouble(payload.toUtf8());
|
||||
}
|
||||
|
||||
public Map<String, Object> getPayloadAsMap() {
|
||||
return XContentHelper.convertToMap(payload, false).v2();
|
||||
}
|
||||
|
||||
public void setScore(float score) {
|
||||
super.setScore(score);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@ import org.elasticsearch.action.suggest.SuggestResponse;
|
|||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||
import org.elasticsearch.index.mapper.MapperException;
|
||||
import org.elasticsearch.search.suggest.Suggest;
|
||||
import org.elasticsearch.search.suggest.completion.CompletionStats;
|
||||
|
@ -155,7 +154,7 @@ public class CompletionSuggestSearchTests extends AbstractSharedClusterTest {
|
|||
assertThat(prefixOption.getPayload(), is(notNullValue()));
|
||||
|
||||
// parse JSON
|
||||
Map<String, Object> jsonMap = JsonXContent.jsonXContent.createParser(prefixOption.getPayload()).mapAndClose();
|
||||
Map<String, Object> jsonMap = prefixOption.getPayloadAsMap();
|
||||
assertThat(jsonMap.size(), is(2));
|
||||
assertThat(jsonMap.get("foo").toString(), is("bar"));
|
||||
assertThat(jsonMap.get("test"), is(instanceOf(List.class)));
|
||||
|
@ -163,6 +162,60 @@ public class CompletionSuggestSearchTests extends AbstractSharedClusterTest {
|
|||
assertThat(listValues, hasItems("spam", "eggs"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPayloadAsNumeric() 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", 1)
|
||||
.endObject().endObject()
|
||||
).get();
|
||||
|
||||
refresh();
|
||||
|
||||
SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion(
|
||||
new CompletionSuggestionBuilder("testSuggestions").field(FIELD).text("foo").size(10)
|
||||
).execute().actionGet();
|
||||
|
||||
assertSuggestions(suggestResponse, "testSuggestions", "Boo Fighters");
|
||||
Suggest.Suggestion.Entry.Option option = suggestResponse.getSuggest().getSuggestion("testSuggestions").getEntries().get(0).getOptions().get(0);
|
||||
assertThat(option, is(instanceOf(CompletionSuggestion.Entry.Option.class)));
|
||||
CompletionSuggestion.Entry.Option prefixOption = (CompletionSuggestion.Entry.Option) option;
|
||||
assertThat(prefixOption.getPayload(), is(notNullValue()));
|
||||
|
||||
assertThat(prefixOption.getPayloadAsLong(), equalTo(1l));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPayloadAsString() 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", "test")
|
||||
.endObject().endObject()
|
||||
).get();
|
||||
|
||||
refresh();
|
||||
|
||||
SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion(
|
||||
new CompletionSuggestionBuilder("testSuggestions").field(FIELD).text("foo").size(10)
|
||||
).execute().actionGet();
|
||||
|
||||
assertSuggestions(suggestResponse, "testSuggestions", "Boo Fighters");
|
||||
Suggest.Suggestion.Entry.Option option = suggestResponse.getSuggest().getSuggestion("testSuggestions").getEntries().get(0).getOptions().get(0);
|
||||
assertThat(option, is(instanceOf(CompletionSuggestion.Entry.Option.class)));
|
||||
CompletionSuggestion.Entry.Option prefixOption = (CompletionSuggestion.Entry.Option) option;
|
||||
assertThat(prefixOption.getPayload(), is(notNullValue()));
|
||||
|
||||
assertThat(prefixOption.getPayloadAsString(), equalTo("test"));
|
||||
}
|
||||
|
||||
@Test(expected = MapperException.class)
|
||||
public void testThatExceptionIsThrownWhenPayloadsAreDisabledButInIndexRequest() throws Exception {
|
||||
createIndexAndMapping("simple", "simple", false, false, true);
|
||||
|
@ -176,19 +229,6 @@ public class CompletionSuggestSearchTests extends AbstractSharedClusterTest {
|
|||
).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
|
||||
public void testDisabledPreserveSeperators() throws Exception {
|
||||
createIndexAndMapping("simple", "simple", true, false, true);
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch.test.unit.common.xcontent.builder;
|
||||
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -45,12 +46,23 @@ public class BuilderRawFieldTests {
|
|||
testRawField(XContentType.SMILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testYamlRawField() throws IOException {
|
||||
testRawField(XContentType.YAML);
|
||||
}
|
||||
|
||||
private void testRawField(XContentType type) throws IOException {
|
||||
XContentBuilder builder = XContentFactory.contentBuilder(type);
|
||||
builder.startObject();
|
||||
builder.field("field1", "value1");
|
||||
builder.rawField("_source", XContentFactory.contentBuilder(type).startObject().field("s_field", "s_value").endObject().bytes());
|
||||
builder.field("field2", "value2");
|
||||
builder.rawField("payload_i", new BytesArray(Long.toString(1)));
|
||||
builder.field("field3", "value3");
|
||||
builder.rawField("payload_d", new BytesArray(Double.toString(1.1)));
|
||||
builder.field("field4", "value4");
|
||||
builder.rawField("payload_s", new BytesArray("test"));
|
||||
builder.field("field5", "value5");
|
||||
builder.endObject();
|
||||
|
||||
XContentParser parser = XContentFactory.xContent(type).createParser(builder.bytes());
|
||||
|
@ -73,6 +85,39 @@ public class BuilderRawFieldTests {
|
|||
assertThat(parser.currentName(), equalTo("field2"));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
|
||||
assertThat(parser.text(), equalTo("value2"));
|
||||
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.currentName(), equalTo("payload_i"));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_NUMBER));
|
||||
assertThat(parser.numberType(), equalTo(XContentParser.NumberType.INT));
|
||||
assertThat(parser.longValue(), equalTo(1l));
|
||||
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.currentName(), equalTo("field3"));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
|
||||
assertThat(parser.text(), equalTo("value3"));
|
||||
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.currentName(), equalTo("payload_d"));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_NUMBER));
|
||||
assertThat(parser.numberType(), equalTo(XContentParser.NumberType.DOUBLE));
|
||||
assertThat(parser.doubleValue(), equalTo(1.1d));
|
||||
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.currentName(), equalTo("field4"));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
|
||||
assertThat(parser.text(), equalTo("value4"));
|
||||
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.currentName(), equalTo("payload_s"));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
|
||||
assertThat(parser.text(), equalTo("test"));
|
||||
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.currentName(), equalTo("field5"));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
|
||||
assertThat(parser.text(), equalTo("value5"));
|
||||
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.END_OBJECT));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue