|
19 | 19 |
|
20 | 20 | package org.elasticsearch.index.engine;
|
21 | 21 |
|
| 22 | +import com.carrotsearch.hppc.ObjectLongHashMap; |
22 | 23 | import org.apache.logging.log4j.Logger;
|
23 | 24 | import org.apache.logging.log4j.message.ParameterizedMessage;
|
24 | 25 | import org.apache.lucene.index.DirectoryReader;
|
| 26 | +import org.apache.lucene.index.FieldInfo; |
25 | 27 | import org.apache.lucene.index.IndexCommit;
|
26 | 28 | import org.apache.lucene.index.IndexFileNames;
|
27 | 29 | import org.apache.lucene.index.IndexReader;
|
|
32 | 34 | import org.apache.lucene.index.SegmentInfos;
|
33 | 35 | import org.apache.lucene.index.SegmentReader;
|
34 | 36 | import org.apache.lucene.index.Term;
|
| 37 | +import org.apache.lucene.index.Terms; |
35 | 38 | import org.apache.lucene.search.IndexSearcher;
|
36 | 39 | import org.apache.lucene.search.ReferenceManager;
|
| 40 | +import org.apache.lucene.search.suggest.document.CompletionTerms; |
37 | 41 | import org.apache.lucene.store.AlreadyClosedException;
|
38 | 42 | import org.apache.lucene.store.Directory;
|
39 | 43 | import org.apache.lucene.store.IOContext;
|
|
42 | 46 | import org.apache.lucene.util.SetOnce;
|
43 | 47 | import org.elasticsearch.ExceptionsHelper;
|
44 | 48 | import org.elasticsearch.common.CheckedRunnable;
|
| 49 | +import org.elasticsearch.common.FieldMemoryStats; |
45 | 50 | import org.elasticsearch.common.Nullable;
|
46 | 51 | import org.elasticsearch.common.bytes.BytesReference;
|
47 | 52 | import org.elasticsearch.common.collect.ImmutableOpenMap;
|
|
56 | 61 | import org.elasticsearch.common.lucene.uid.VersionsAndSeqNoResolver;
|
57 | 62 | import org.elasticsearch.common.lucene.uid.VersionsAndSeqNoResolver.DocIdAndVersion;
|
58 | 63 | import org.elasticsearch.common.metrics.CounterMetric;
|
| 64 | +import org.elasticsearch.common.regex.Regex; |
59 | 65 | import org.elasticsearch.common.unit.TimeValue;
|
60 | 66 | import org.elasticsearch.common.util.concurrent.ReleasableLock;
|
61 | 67 | import org.elasticsearch.index.VersionType;
|
|
71 | 77 | import org.elasticsearch.index.store.Store;
|
72 | 78 | import org.elasticsearch.index.translog.Translog;
|
73 | 79 | import org.elasticsearch.index.translog.TranslogStats;
|
| 80 | +import org.elasticsearch.search.suggest.completion.CompletionStats; |
74 | 81 |
|
75 | 82 | import java.io.Closeable;
|
76 | 83 | import java.io.FileNotFoundException;
|
@@ -176,6 +183,34 @@ public MergeStats getMergeStats() {
|
176 | 183 | /** Returns how many bytes we are currently moving from heap to disk */
|
177 | 184 | public abstract long getWritingBytes();
|
178 | 185 |
|
| 186 | + /** |
| 187 | + * Returns the {@link CompletionStats} for this engine |
| 188 | + */ |
| 189 | + public CompletionStats completionStats(String... fieldNamePatterns) throws IOException { |
| 190 | + try (Engine.Searcher currentSearcher = acquireSearcher("completion_stats", SearcherScope.INTERNAL)) { |
| 191 | + long sizeInBytes = 0; |
| 192 | + ObjectLongHashMap<String> completionFields = null; |
| 193 | + if (fieldNamePatterns != null && fieldNamePatterns.length > 0) { |
| 194 | + completionFields = new ObjectLongHashMap<>(fieldNamePatterns.length); |
| 195 | + } |
| 196 | + for (LeafReaderContext atomicReaderContext : currentSearcher.reader().leaves()) { |
| 197 | + LeafReader atomicReader = atomicReaderContext.reader(); |
| 198 | + for (FieldInfo info : atomicReader.getFieldInfos()) { |
| 199 | + Terms terms = atomicReader.terms(info.name); |
| 200 | + if (terms instanceof CompletionTerms) { |
| 201 | + // TODO: currently we load up the suggester for reporting its size |
| 202 | + long fstSize = ((CompletionTerms) terms).suggester().ramBytesUsed(); |
| 203 | + if (Regex.simpleMatch(fieldNamePatterns, info.name)) { |
| 204 | + completionFields.addTo(info.name, fstSize); |
| 205 | + } |
| 206 | + sizeInBytes += fstSize; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + return new CompletionStats(sizeInBytes, completionFields == null ? null : new FieldMemoryStats(completionFields)); |
| 211 | + } |
| 212 | + } |
| 213 | + |
179 | 214 | /**
|
180 | 215 | * Returns the {@link DocsStats} for this engine
|
181 | 216 | */
|
|
0 commit comments