8
8
package org .elasticsearch .index .mapper ;
9
9
10
10
import org .apache .lucene .index .IndexableField ;
11
+ import org .apache .lucene .index .LeafReader ;
12
+ import org .apache .lucene .index .PostingsEnum ;
13
+ import org .apache .lucene .index .Term ;
11
14
import org .apache .lucene .search .Query ;
12
15
import org .elasticsearch .common .xcontent .XContentParserUtils ;
13
16
import org .elasticsearch .index .query .QueryShardException ;
14
17
import org .elasticsearch .index .query .SearchExecutionContext ;
18
+ import org .elasticsearch .xcontent .XContentBuilder ;
15
19
import org .elasticsearch .xcontent .XContentParser ;
16
20
17
21
import java .io .IOException ;
18
22
import java .util .Collections ;
23
+ import java .util .Map ;
24
+ import java .util .stream .Stream ;
19
25
20
26
/** Mapper for the doc_count field. */
21
27
public class DocCountFieldMapper extends MetadataFieldMapper {
@@ -25,6 +31,11 @@ public class DocCountFieldMapper extends MetadataFieldMapper {
25
31
26
32
private static final DocCountFieldMapper INSTANCE = new DocCountFieldMapper ();
27
33
34
+ /**
35
+ * The term that is the key to the postings list that stores the doc counts.
36
+ */
37
+ private static final Term TERM = new Term (NAME , NAME );
38
+
28
39
public static final TypeParser PARSER = new FixedTypeParser (c -> INSTANCE );
29
40
30
41
public static final class DocCountFieldType extends MappedFieldType {
@@ -115,4 +126,49 @@ protected String contentType() {
115
126
public static IndexableField field (int count ) {
116
127
return new CustomTermFreqField (NAME , NAME , count );
117
128
}
129
+
130
+ @ Override
131
+ public SourceLoader .SyntheticFieldLoader syntheticFieldLoader () {
132
+ return new SyntheticFieldLoader ();
133
+ }
134
+
135
+ /**
136
+ * The lookup for loading values.
137
+ */
138
+ public static PostingsEnum leafLookup (LeafReader reader ) throws IOException {
139
+ return reader .postings (TERM );
140
+ }
141
+
142
+ private class SyntheticFieldLoader implements SourceLoader .SyntheticFieldLoader {
143
+ private PostingsEnum postings ;
144
+ private boolean hasValue ;
145
+
146
+ @ Override
147
+ public Stream <Map .Entry <String , StoredFieldLoader >> storedFieldLoaders () {
148
+ return Stream .empty ();
149
+ }
150
+
151
+ @ Override
152
+ public DocValuesLoader docValuesLoader (LeafReader leafReader , int [] docIdsInLeaf ) throws IOException {
153
+ postings = leafLookup (leafReader );
154
+ if (postings == null ) {
155
+ hasValue = false ;
156
+ return null ;
157
+ }
158
+ return docId -> hasValue = docId == postings .advance (docId );
159
+ }
160
+
161
+ @ Override
162
+ public boolean hasValue () {
163
+ return hasValue ;
164
+ }
165
+
166
+ @ Override
167
+ public void write (XContentBuilder b ) throws IOException {
168
+ if (hasValue == false ) {
169
+ return ;
170
+ }
171
+ b .field (NAME , postings .freq ());
172
+ }
173
+ }
118
174
}
0 commit comments