19
19
package org .elasticsearch .action .termvector ;
20
20
21
21
import org .apache .lucene .index .*;
22
+ import org .apache .lucene .search .CollectionStatistics ;
23
+ import org .apache .lucene .search .TermStatistics ;
22
24
import org .apache .lucene .util .BytesRef ;
23
25
import org .elasticsearch .action .termvector .TermVectorRequest .Flag ;
26
+ import org .elasticsearch .common .Nullable ;
24
27
import org .elasticsearch .common .bytes .BytesReference ;
25
28
import org .elasticsearch .common .io .stream .BytesStreamOutput ;
29
+ import org .elasticsearch .search .dfs .AggregatedDfs ;
26
30
27
31
import java .io .IOException ;
28
32
import java .util .ArrayList ;
@@ -45,7 +49,7 @@ final class TermVectorWriter {
45
49
response = termVectorResponse ;
46
50
}
47
51
48
- void setFields (Fields termVectorsByField , Set <String > selectedFields , EnumSet <Flag > flags , Fields topLevelFields ) throws IOException {
52
+ void setFields (Fields termVectorsByField , Set <String > selectedFields , EnumSet <Flag > flags , Fields topLevelFields , @ Nullable AggregatedDfs dfs ) throws IOException {
49
53
int numFieldsWritten = 0 ;
50
54
TermsEnum iterator = null ;
51
55
DocsAndPositionsEnum docsAndPosEnum = null ;
@@ -70,7 +74,11 @@ void setFields(Fields termVectorsByField, Set<String> selectedFields, EnumSet<Fl
70
74
boolean payloads = flags .contains (Flag .Payloads ) && fieldTermVector .hasPayloads ();
71
75
startField (field , fieldTermVector .size (), positions , offsets , payloads );
72
76
if (flags .contains (Flag .FieldStatistics )) {
73
- writeFieldStatistics (topLevelTerms );
77
+ if (dfs != null ) {
78
+ writeFieldStatistics (dfs .fieldStatistics ().get (field ));
79
+ } else {
80
+ writeFieldStatistics (topLevelTerms );
81
+ }
74
82
}
75
83
iterator = fieldTermVector .iterator (iterator );
76
84
final boolean useDocsAndPos = positions || offsets || payloads ;
@@ -81,7 +89,11 @@ void setFields(Fields termVectorsByField, Set<String> selectedFields, EnumSet<Fl
81
89
boolean foundTerm = topLevelIterator .seekExact (term );
82
90
startTerm (term );
83
91
if (flags .contains (Flag .TermStatistics )) {
84
- writeTermStatistics (topLevelIterator );
92
+ if (dfs != null ) {
93
+ writeTermStatistics (dfs .termStatistics ().get (new Term (field , term .utf8ToString ())));
94
+ } else {
95
+ writeTermStatistics (topLevelIterator );
96
+ }
85
97
}
86
98
if (useDocsAndPos ) {
87
99
// given we have pos or offsets
@@ -161,7 +173,6 @@ private void writePayload(BytesRef payload) throws IOException {
161
173
}
162
174
163
175
private void writeFreq (int termFreq ) throws IOException {
164
-
165
176
writePotentiallyNegativeVInt (termFreq );
166
177
}
167
178
@@ -205,7 +216,15 @@ private void writeTermStatistics(TermsEnum topLevelIterator) throws IOException
205
216
long ttf = topLevelIterator .totalTermFreq ();
206
217
assert (ttf >= -1 );
207
218
writePotentiallyNegativeVLong (ttf );
219
+ }
208
220
221
+ private void writeTermStatistics (TermStatistics termStatistics ) throws IOException {
222
+ int docFreq = (int ) termStatistics .docFreq ();
223
+ assert (docFreq >= -1 );
224
+ writePotentiallyNegativeVInt (docFreq );
225
+ long ttf = termStatistics .totalTermFreq ();
226
+ assert (ttf >= -1 );
227
+ writePotentiallyNegativeVLong (ttf );
209
228
}
210
229
211
230
private void writeFieldStatistics (Terms topLevelTerms ) throws IOException {
@@ -218,7 +237,18 @@ private void writeFieldStatistics(Terms topLevelTerms) throws IOException {
218
237
int dc = topLevelTerms .getDocCount ();
219
238
assert (dc >= -1 );
220
239
writePotentiallyNegativeVInt (dc );
240
+ }
221
241
242
+ private void writeFieldStatistics (CollectionStatistics fieldStats ) throws IOException {
243
+ long sttf = fieldStats .sumTotalTermFreq ();
244
+ assert (sttf >= -1 );
245
+ writePotentiallyNegativeVLong (sttf );
246
+ long sdf = fieldStats .sumDocFreq ();
247
+ assert (sdf >= -1 );
248
+ writePotentiallyNegativeVLong (sdf );
249
+ int dc = (int ) fieldStats .docCount ();
250
+ assert (dc >= -1 );
251
+ writePotentiallyNegativeVInt (dc );
222
252
}
223
253
224
254
private void writePotentiallyNegativeVInt (int value ) throws IOException {
0 commit comments