18
18
*/
19
19
package org .elasticsearch .action .admin .indices .analyze ;
20
20
21
+ import org .elasticsearch .Version ;
21
22
import org .elasticsearch .action .ActionResponse ;
22
23
import org .elasticsearch .common .io .stream .StreamInput ;
23
24
import org .elasticsearch .common .io .stream .StreamOutput ;
30
31
import java .util .ArrayList ;
31
32
import java .util .Iterator ;
32
33
import java .util .List ;
34
+ import java .util .Map ;
33
35
34
36
/**
35
37
*
36
38
*/
37
39
public class AnalyzeResponse extends ActionResponse implements Iterable <AnalyzeResponse .AnalyzeToken >, ToXContent {
38
40
39
- public static class AnalyzeToken implements Streamable {
41
+ public static class AnalyzeToken implements Streamable , ToXContent {
40
42
private String term ;
41
43
private int startOffset ;
42
44
private int endOffset ;
43
45
private int position ;
46
+ private Map <String , Object > attributes ;
44
47
private String type ;
45
48
46
49
AnalyzeToken () {
47
50
}
48
51
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 ) {
50
54
this .term = term ;
51
55
this .position = position ;
52
56
this .startOffset = startOffset ;
53
57
this .endOffset = endOffset ;
54
58
this .type = type ;
59
+ this .attributes = attributes ;
55
60
}
56
61
57
62
public String getTerm () {
@@ -74,6 +79,27 @@ public String getType() {
74
79
return this .type ;
75
80
}
76
81
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
+
77
103
public static AnalyzeToken readAnalyzeToken (StreamInput in ) throws IOException {
78
104
AnalyzeToken analyzeToken = new AnalyzeToken ();
79
105
analyzeToken .readFrom (in );
@@ -87,6 +113,9 @@ public void readFrom(StreamInput in) throws IOException {
87
113
endOffset = in .readInt ();
88
114
position = in .readVInt ();
89
115
type = in .readOptionalString ();
116
+ if (in .getVersion ().onOrAfter (Version .V_2_2_0 )) {
117
+ attributes = (Map <String , Object >) in .readGenericValue ();
118
+ }
90
119
}
91
120
92
121
@ Override
@@ -96,40 +125,52 @@ public void writeTo(StreamOutput out) throws IOException {
96
125
out .writeInt (endOffset );
97
126
out .writeVInt (position );
98
127
out .writeOptionalString (type );
128
+ if (out .getVersion ().onOrAfter (Version .V_2_2_0 )) {
129
+ out .writeGenericValue (attributes );
130
+ }
99
131
}
100
132
}
101
133
134
+ private DetailAnalyzeResponse detail ;
135
+
102
136
private List <AnalyzeToken > tokens ;
103
137
104
138
AnalyzeResponse () {
105
139
}
106
140
107
- public AnalyzeResponse (List <AnalyzeToken > tokens ) {
141
+ public AnalyzeResponse (List <AnalyzeToken > tokens , DetailAnalyzeResponse detail ) {
108
142
this .tokens = tokens ;
143
+ this .detail = detail ;
109
144
}
110
145
111
146
public List <AnalyzeToken > getTokens () {
112
147
return this .tokens ;
113
148
}
114
149
150
+ public DetailAnalyzeResponse detail () {
151
+ return this .detail ;
152
+ }
153
+
115
154
@ Override
116
155
public Iterator <AnalyzeToken > iterator () {
117
156
return tokens .iterator ();
118
157
}
119
158
120
159
@ Override
121
160
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 );
130
172
builder .endObject ();
131
173
}
132
- builder .endArray ();
133
174
return builder ;
134
175
}
135
176
@@ -141,14 +182,24 @@ public void readFrom(StreamInput in) throws IOException {
141
182
for (int i = 0 ; i < size ; i ++) {
142
183
tokens .add (AnalyzeToken .readAnalyzeToken (in ));
143
184
}
185
+ if (in .getVersion ().onOrAfter (Version .V_2_2_0 )) {
186
+ detail = in .readOptionalStreamable (DetailAnalyzeResponse ::new );
187
+ }
144
188
}
145
189
146
190
@ Override
147
191
public void writeTo (StreamOutput out ) throws IOException {
148
192
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 );
152
203
}
153
204
}
154
205
@@ -159,5 +210,6 @@ static final class Fields {
159
210
static final XContentBuilderString END_OFFSET = new XContentBuilderString ("end_offset" );
160
211
static final XContentBuilderString TYPE = new XContentBuilderString ("type" );
161
212
static final XContentBuilderString POSITION = new XContentBuilderString ("position" );
213
+ static final XContentBuilderString DETAIL = new XContentBuilderString ("detail" );
162
214
}
163
215
}
0 commit comments