Skip to content

Commit bc9698a

Browse files
committed
Support 'yaml' as a format for the Analyze API
Fixes elastic#4311
1 parent 2058a03 commit bc9698a

File tree

2 files changed

+20
-38
lines changed

2 files changed

+20
-38
lines changed

docs/reference/indices/analyze.asciidoc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,3 @@ mapping for `obj1.field1` (and if not, the default index analyzer).
4848

4949
Also, the text can be provided as part of the request body, and not as a
5050
parameter.
51-
52-
[float]
53-
[[format]]
54-
=== Format
55-
56-
By default, the format the tokens are returned in are in json and its
57-
called `detailed`. The `text` format value provides the analyzed data in
58-
a text stream that is a bit more readable.

src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeResponse.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.io.stream.Streamable;
2626
import org.elasticsearch.common.xcontent.ToXContent;
2727
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.common.xcontent.XContentBuilderString;
2829

2930
import java.io.IOException;
3031
import java.util.ArrayList;
@@ -119,37 +120,17 @@ public Iterator<AnalyzeToken> iterator() {
119120

120121
@Override
121122
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
122-
String format = params.param("format", "detailed");
123-
if ("detailed".equals(format)) {
124-
builder.startArray("tokens");
125-
for (AnalyzeToken token : tokens) {
126-
builder.startObject();
127-
builder.field("token", token.getTerm());
128-
builder.field("start_offset", token.getStartOffset());
129-
builder.field("end_offset", token.getEndOffset());
130-
builder.field("type", token.getType());
131-
builder.field("position", token.getPosition());
132-
builder.endObject();
133-
}
134-
builder.endArray();
135-
} else if ("text".equals(format)) {
136-
StringBuilder sb = new StringBuilder();
137-
int lastPosition = 0;
138-
for (AnalyzeToken token : tokens) {
139-
if (lastPosition != token.getPosition()) {
140-
if (lastPosition != 0) {
141-
sb.append("\n").append(token.getPosition()).append(": \n");
142-
}
143-
lastPosition = token.getPosition();
144-
}
145-
sb.append('[')
146-
.append(token.getTerm()).append(":")
147-
.append(token.getStartOffset()).append("->").append(token.getEndOffset()).append(":")
148-
.append(token.getType())
149-
.append("]\n");
150-
}
151-
builder.field("tokens", sb);
123+
builder.startArray(Fields.TOKENS);
124+
for (AnalyzeToken token : tokens) {
125+
builder.startObject();
126+
builder.field(Fields.TOKEN, token.getTerm());
127+
builder.field(Fields.START_OFFSET, token.getStartOffset());
128+
builder.field(Fields.END_OFFSET, token.getEndOffset());
129+
builder.field(Fields.TYPE, token.getType());
130+
builder.field(Fields.POSITION, token.getPosition());
131+
builder.endObject();
152132
}
133+
builder.endArray();
153134
return builder;
154135
}
155136

@@ -171,4 +152,13 @@ public void writeTo(StreamOutput out) throws IOException {
171152
token.writeTo(out);
172153
}
173154
}
155+
156+
static final class Fields {
157+
static final XContentBuilderString TOKENS = new XContentBuilderString("tokens");
158+
static final XContentBuilderString TOKEN = new XContentBuilderString("token");
159+
static final XContentBuilderString START_OFFSET = new XContentBuilderString("start_offset");
160+
static final XContentBuilderString END_OFFSET = new XContentBuilderString("end_offset");
161+
static final XContentBuilderString TYPE = new XContentBuilderString("type");
162+
static final XContentBuilderString POSITION = new XContentBuilderString("position");
163+
}
174164
}

0 commit comments

Comments
 (0)