Support 'yaml' as a format for the Analyze API

Fixes #4311
This commit is contained in:
Lee Hinman 2013-12-08 15:08:00 -07:00
parent 2058a03547
commit bc9698a347
2 changed files with 20 additions and 38 deletions

View File

@ -48,11 +48,3 @@ mapping for `obj1.field1` (and if not, the default index analyzer).
Also, the text can be provided as part of the request body, and not as a Also, the text can be provided as part of the request body, and not as a
parameter. parameter.
[float]
[[format]]
=== Format
By default, the format the tokens are returned in are in json and its
called `detailed`. The `text` format value provides the analyzed data in
a text stream that is a bit more readable.

View File

@ -25,6 +25,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -119,37 +120,17 @@ public class AnalyzeResponse extends ActionResponse implements Iterable<AnalyzeR
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
String format = params.param("format", "detailed"); builder.startArray(Fields.TOKENS);
if ("detailed".equals(format)) { for (AnalyzeToken token : tokens) {
builder.startArray("tokens"); builder.startObject();
for (AnalyzeToken token : tokens) { builder.field(Fields.TOKEN, token.getTerm());
builder.startObject(); builder.field(Fields.START_OFFSET, token.getStartOffset());
builder.field("token", token.getTerm()); builder.field(Fields.END_OFFSET, token.getEndOffset());
builder.field("start_offset", token.getStartOffset()); builder.field(Fields.TYPE, token.getType());
builder.field("end_offset", token.getEndOffset()); builder.field(Fields.POSITION, token.getPosition());
builder.field("type", token.getType()); builder.endObject();
builder.field("position", token.getPosition());
builder.endObject();
}
builder.endArray();
} else if ("text".equals(format)) {
StringBuilder sb = new StringBuilder();
int lastPosition = 0;
for (AnalyzeToken token : tokens) {
if (lastPosition != token.getPosition()) {
if (lastPosition != 0) {
sb.append("\n").append(token.getPosition()).append(": \n");
}
lastPosition = token.getPosition();
}
sb.append('[')
.append(token.getTerm()).append(":")
.append(token.getStartOffset()).append("->").append(token.getEndOffset()).append(":")
.append(token.getType())
.append("]\n");
}
builder.field("tokens", sb);
} }
builder.endArray();
return builder; return builder;
} }
@ -171,4 +152,13 @@ public class AnalyzeResponse extends ActionResponse implements Iterable<AnalyzeR
token.writeTo(out); token.writeTo(out);
} }
} }
static final class Fields {
static final XContentBuilderString TOKENS = new XContentBuilderString("tokens");
static final XContentBuilderString TOKEN = new XContentBuilderString("token");
static final XContentBuilderString START_OFFSET = new XContentBuilderString("start_offset");
static final XContentBuilderString END_OFFSET = new XContentBuilderString("end_offset");
static final XContentBuilderString TYPE = new XContentBuilderString("type");
static final XContentBuilderString POSITION = new XContentBuilderString("position");
}
} }