Skip to content

Commit fab4439

Browse files
committed
Analysis: Add detail response support
add explain option fix char_filter bug Closes #11076 #15257
1 parent 1ef24d2 commit fab4439

File tree

10 files changed

+1055
-43
lines changed

10 files changed

+1055
-43
lines changed

core/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeRequest.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.elasticsearch.action.admin.indices.analyze;
2020

21+
import org.elasticsearch.Version;
2122
import org.elasticsearch.action.ActionRequestValidationException;
2223
import org.elasticsearch.action.support.single.shard.SingleShardRequest;
2324
import org.elasticsearch.common.Strings;
@@ -46,6 +47,10 @@ public class AnalyzeRequest extends SingleShardRequest<AnalyzeRequest> {
4647

4748
private String field;
4849

50+
private boolean explain = false;
51+
52+
private String[] attributes = Strings.EMPTY_ARRAY;
53+
4954
public AnalyzeRequest() {
5055
}
5156

@@ -86,6 +91,9 @@ public String tokenizer() {
8691
}
8792

8893
public AnalyzeRequest tokenFilters(String... tokenFilters) {
94+
if (tokenFilters == null) {
95+
throw new IllegalArgumentException("token filters must not be null");
96+
}
8997
this.tokenFilters = tokenFilters;
9098
return this;
9199
}
@@ -95,6 +103,9 @@ public String[] tokenFilters() {
95103
}
96104

97105
public AnalyzeRequest charFilters(String... charFilters) {
106+
if (charFilters == null) {
107+
throw new IllegalArgumentException("char filters must not be null");
108+
}
98109
this.charFilters = charFilters;
99110
return this;
100111
}
@@ -112,18 +123,33 @@ public String field() {
112123
return this.field;
113124
}
114125

126+
public AnalyzeRequest explain(boolean explain) {
127+
this.explain = explain;
128+
return this;
129+
}
130+
131+
public boolean explain() {
132+
return this.explain;
133+
}
134+
135+
public AnalyzeRequest attributes(String... attributes) {
136+
if (attributes == null) {
137+
throw new IllegalArgumentException("attributes must not be null");
138+
}
139+
this.attributes = attributes;
140+
return this;
141+
}
142+
143+
public String[] attributes() {
144+
return this.attributes;
145+
}
146+
115147
@Override
116148
public ActionRequestValidationException validate() {
117149
ActionRequestValidationException validationException = null;
118150
if (text == null || text.length == 0) {
119151
validationException = addValidationError("text is missing", validationException);
120152
}
121-
if (tokenFilters == null) {
122-
validationException = addValidationError("token filters must not be null", validationException);
123-
}
124-
if (charFilters == null) {
125-
validationException = addValidationError("char filters must not be null", validationException);
126-
}
127153
return validationException;
128154
}
129155

@@ -136,6 +162,10 @@ public void readFrom(StreamInput in) throws IOException {
136162
tokenFilters = in.readStringArray();
137163
charFilters = in.readStringArray();
138164
field = in.readOptionalString();
165+
if (in.getVersion().onOrAfter(Version.V_2_2_0)) {
166+
explain = in.readBoolean();
167+
attributes = in.readStringArray();
168+
}
139169
}
140170

141171
@Override
@@ -147,5 +177,9 @@ public void writeTo(StreamOutput out) throws IOException {
147177
out.writeStringArray(tokenFilters);
148178
out.writeStringArray(charFilters);
149179
out.writeOptionalString(field);
180+
if (out.getVersion().onOrAfter(Version.V_2_2_0)) {
181+
out.writeBoolean(explain);
182+
out.writeStringArray(attributes);
183+
}
150184
}
151185
}

core/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeRequestBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ public AnalyzeRequestBuilder setCharFilters(String... charFilters) {
7878
return this;
7979
}
8080

81+
/**
82+
* Sets explain
83+
*/
84+
public AnalyzeRequestBuilder setExplain(boolean explain) {
85+
request.explain(explain);
86+
return this;
87+
}
88+
89+
/**
90+
* Sets attributes that will include results
91+
*/
92+
public AnalyzeRequestBuilder setAttributes(String attributes){
93+
request.attributes(attributes);
94+
return this;
95+
}
96+
8197
/**
8298
* Sets texts to analyze
8399
*/

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

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.elasticsearch.action.admin.indices.analyze;
2020

21+
import org.elasticsearch.Version;
2122
import org.elasticsearch.action.ActionResponse;
2223
import org.elasticsearch.common.io.stream.StreamInput;
2324
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -30,28 +31,32 @@
3031
import java.util.ArrayList;
3132
import java.util.Iterator;
3233
import java.util.List;
34+
import java.util.Map;
3335

3436
/**
3537
*
3638
*/
3739
public class AnalyzeResponse extends ActionResponse implements Iterable<AnalyzeResponse.AnalyzeToken>, ToXContent {
3840

39-
public static class AnalyzeToken implements Streamable {
41+
public static class AnalyzeToken implements Streamable, ToXContent {
4042
private String term;
4143
private int startOffset;
4244
private int endOffset;
4345
private int position;
46+
private Map<String, Object> attributes;
4447
private String type;
4548

4649
AnalyzeToken() {
4750
}
4851

49-
public AnalyzeToken(String term, int position, int startOffset, int endOffset, String type) {
52+
public AnalyzeToken(String term, int position, int startOffset, int endOffset, String type,
53+
Map<String, Object> attributes) {
5054
this.term = term;
5155
this.position = position;
5256
this.startOffset = startOffset;
5357
this.endOffset = endOffset;
5458
this.type = type;
59+
this.attributes = attributes;
5560
}
5661

5762
public String getTerm() {
@@ -74,6 +79,27 @@ public String getType() {
7479
return this.type;
7580
}
7681

82+
public Map<String, Object> getAttributes(){
83+
return this.attributes;
84+
}
85+
86+
@Override
87+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
88+
builder.startObject();
89+
builder.field(Fields.TOKEN, term);
90+
builder.field(Fields.START_OFFSET, startOffset);
91+
builder.field(Fields.END_OFFSET, endOffset);
92+
builder.field(Fields.TYPE, type);
93+
builder.field(Fields.POSITION, position);
94+
if (attributes != null && !attributes.isEmpty()) {
95+
for (Map.Entry<String, Object> entity : attributes.entrySet()) {
96+
builder.field(entity.getKey(), entity.getValue());
97+
}
98+
}
99+
builder.endObject();
100+
return builder;
101+
}
102+
77103
public static AnalyzeToken readAnalyzeToken(StreamInput in) throws IOException {
78104
AnalyzeToken analyzeToken = new AnalyzeToken();
79105
analyzeToken.readFrom(in);
@@ -87,6 +113,9 @@ public void readFrom(StreamInput in) throws IOException {
87113
endOffset = in.readInt();
88114
position = in.readVInt();
89115
type = in.readOptionalString();
116+
if (in.getVersion().onOrAfter(Version.V_2_2_0)) {
117+
attributes = (Map<String, Object>) in.readGenericValue();
118+
}
90119
}
91120

92121
@Override
@@ -96,40 +125,52 @@ public void writeTo(StreamOutput out) throws IOException {
96125
out.writeInt(endOffset);
97126
out.writeVInt(position);
98127
out.writeOptionalString(type);
128+
if (out.getVersion().onOrAfter(Version.V_2_2_0)) {
129+
out.writeGenericValue(attributes);
130+
}
99131
}
100132
}
101133

134+
private DetailAnalyzeResponse detail;
135+
102136
private List<AnalyzeToken> tokens;
103137

104138
AnalyzeResponse() {
105139
}
106140

107-
public AnalyzeResponse(List<AnalyzeToken> tokens) {
141+
public AnalyzeResponse(List<AnalyzeToken> tokens, DetailAnalyzeResponse detail) {
108142
this.tokens = tokens;
143+
this.detail = detail;
109144
}
110145

111146
public List<AnalyzeToken> getTokens() {
112147
return this.tokens;
113148
}
114149

150+
public DetailAnalyzeResponse detail() {
151+
return this.detail;
152+
}
153+
115154
@Override
116155
public Iterator<AnalyzeToken> iterator() {
117156
return tokens.iterator();
118157
}
119158

120159
@Override
121160
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
122-
builder.startArray(Fields.TOKENS);
123-
for (AnalyzeToken token : tokens) {
124-
builder.startObject();
125-
builder.field(Fields.TOKEN, token.getTerm());
126-
builder.field(Fields.START_OFFSET, token.getStartOffset());
127-
builder.field(Fields.END_OFFSET, token.getEndOffset());
128-
builder.field(Fields.TYPE, token.getType());
129-
builder.field(Fields.POSITION, token.getPosition());
161+
if (tokens != null) {
162+
builder.startArray(Fields.TOKENS);
163+
for (AnalyzeToken token : tokens) {
164+
token.toXContent(builder, params);
165+
}
166+
builder.endArray();
167+
}
168+
169+
if (detail != null) {
170+
builder.startObject(Fields.DETAIL);
171+
detail.toXContent(builder, params);
130172
builder.endObject();
131173
}
132-
builder.endArray();
133174
return builder;
134175
}
135176

@@ -141,14 +182,24 @@ public void readFrom(StreamInput in) throws IOException {
141182
for (int i = 0; i < size; i++) {
142183
tokens.add(AnalyzeToken.readAnalyzeToken(in));
143184
}
185+
if (in.getVersion().onOrAfter(Version.V_2_2_0)) {
186+
detail = in.readOptionalStreamable(DetailAnalyzeResponse::new);
187+
}
144188
}
145189

146190
@Override
147191
public void writeTo(StreamOutput out) throws IOException {
148192
super.writeTo(out);
149-
out.writeVInt(tokens.size());
150-
for (AnalyzeToken token : tokens) {
151-
token.writeTo(out);
193+
if (tokens != null) {
194+
out.writeVInt(tokens.size());
195+
for (AnalyzeToken token : tokens) {
196+
token.writeTo(out);
197+
}
198+
} else {
199+
out.writeVInt(0);
200+
}
201+
if (out.getVersion().onOrAfter(Version.V_2_2_0)) {
202+
out.writeOptionalStreamable(detail);
152203
}
153204
}
154205

@@ -159,5 +210,6 @@ static final class Fields {
159210
static final XContentBuilderString END_OFFSET = new XContentBuilderString("end_offset");
160211
static final XContentBuilderString TYPE = new XContentBuilderString("type");
161212
static final XContentBuilderString POSITION = new XContentBuilderString("position");
213+
static final XContentBuilderString DETAIL = new XContentBuilderString("detail");
162214
}
163215
}

0 commit comments

Comments
 (0)