use of org.elasticsearch.common.FieldMemoryStats in project elasticsearch by elastic.
the class CompletionFieldStats method completionStats.
/**
* Returns total in-heap bytes used by all suggesters. This method has CPU cost <code>O(numIndexedFields)</code>.
*
* @param fieldNamePatterns if non-null, any completion field name matching any of these patterns will break out its in-heap bytes
* separately in the returned {@link CompletionStats}
*/
public static CompletionStats completionStats(IndexReader indexReader, String... fieldNamePatterns) {
long sizeInBytes = 0;
ObjectLongHashMap<String> completionFields = null;
if (fieldNamePatterns != null && fieldNamePatterns.length > 0) {
completionFields = new ObjectLongHashMap<>(fieldNamePatterns.length);
}
for (LeafReaderContext atomicReaderContext : indexReader.leaves()) {
LeafReader atomicReader = atomicReaderContext.reader();
try {
Fields fields = atomicReader.fields();
for (String fieldName : fields) {
Terms terms = fields.terms(fieldName);
if (terms instanceof CompletionTerms) {
// TODO: currently we load up the suggester for reporting its size
long fstSize = ((CompletionTerms) terms).suggester().ramBytesUsed();
if (fieldNamePatterns != null && fieldNamePatterns.length > 0 && Regex.simpleMatch(fieldNamePatterns, fieldName)) {
completionFields.addTo(fieldName, fstSize);
}
sizeInBytes += fstSize;
}
}
} catch (IOException ioe) {
throw new ElasticsearchException(ioe);
}
}
return new CompletionStats(sizeInBytes, completionFields == null ? null : new FieldMemoryStats(completionFields));
}
use of org.elasticsearch.common.FieldMemoryStats in project elasticsearch by elastic.
the class CompletionsStatsTests method testSerialize.
public void testSerialize() throws IOException {
FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats();
CompletionStats stats = new CompletionStats(randomNonNegativeLong(), map == null ? null : map);
BytesStreamOutput out = new BytesStreamOutput();
stats.writeTo(out);
CompletionStats read = new CompletionStats();
StreamInput input = out.bytes().streamInput();
read.readFrom(input);
assertEquals(-1, input.read());
assertEquals(stats.getSizeInBytes(), read.getSizeInBytes());
assertEquals(stats.getFields(), read.getFields());
}
use of org.elasticsearch.common.FieldMemoryStats in project elasticsearch by elastic.
the class CompletionSuggestSearchIT method testThatStatsAreWorking.
public void testThatStatsAreWorking() throws Exception {
String otherField = "testOtherField";
client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", 2)).execute().actionGet();
ensureGreen();
PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject(FIELD).field("type", "completion").field("analyzer", "simple").endObject().startObject(otherField).field("type", "completion").field("analyzer", "simple").endObject().endObject().endObject().endObject()).get();
assertThat(putMappingResponse.isAcknowledged(), is(true));
// Index two entities
client().prepareIndex(INDEX, TYPE, "1").setSource(jsonBuilder().startObject().field(FIELD, "Foo Fighters").field(otherField, "WHATEVER").endObject()).get();
client().prepareIndex(INDEX, TYPE, "2").setSource(jsonBuilder().startObject().field(FIELD, "Bar Fighters").field(otherField, "WHATEVER2").endObject()).get();
refresh();
ensureGreen();
// load the fst index into ram
client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("f"))).get();
client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(otherField).prefix("f"))).get();
// Get all stats
IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).get();
CompletionStats completionStats = indicesStatsResponse.getIndex(INDEX).getPrimaries().completion;
assertThat(completionStats, notNullValue());
long totalSizeInBytes = completionStats.getSizeInBytes();
assertThat(totalSizeInBytes, is(greaterThan(0L)));
IndicesStatsResponse singleFieldStats = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).setCompletionFields(FIELD).get();
long singleFieldSizeInBytes = singleFieldStats.getIndex(INDEX).getPrimaries().completion.getFields().get(FIELD);
IndicesStatsResponse otherFieldStats = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).setCompletionFields(otherField).get();
long otherFieldSizeInBytes = otherFieldStats.getIndex(INDEX).getPrimaries().completion.getFields().get(otherField);
assertThat(singleFieldSizeInBytes + otherFieldSizeInBytes, is(totalSizeInBytes));
// regexes
IndicesStatsResponse regexFieldStats = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).setCompletionFields("*").get();
FieldMemoryStats fields = regexFieldStats.getIndex(INDEX).getPrimaries().completion.getFields();
long regexSizeInBytes = fields.get(FIELD) + fields.get(otherField);
assertThat(regexSizeInBytes, is(totalSizeInBytes));
}
use of org.elasticsearch.common.FieldMemoryStats in project elasticsearch by elastic.
the class FieldDataStatsTests method testSerialize.
public void testSerialize() throws IOException {
FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats();
FieldDataStats stats = new FieldDataStats(randomNonNegativeLong(), randomNonNegativeLong(), map == null ? null : map);
BytesStreamOutput out = new BytesStreamOutput();
stats.writeTo(out);
FieldDataStats read = new FieldDataStats();
StreamInput input = out.bytes().streamInput();
read.readFrom(input);
assertEquals(-1, input.read());
assertEquals(stats.evictions, read.evictions);
assertEquals(stats.memorySize, read.memorySize);
assertEquals(stats.getFields(), read.getFields());
}
Aggregations