Search in sources :

Example 1 with FieldMemoryStats

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));
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) Terms(org.apache.lucene.index.Terms) CompletionTerms(org.apache.lucene.search.suggest.document.CompletionTerms) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) CompletionTerms(org.apache.lucene.search.suggest.document.CompletionTerms) Fields(org.apache.lucene.index.Fields) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FieldMemoryStats(org.elasticsearch.common.FieldMemoryStats)

Example 2 with FieldMemoryStats

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());
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) FieldMemoryStats(org.elasticsearch.common.FieldMemoryStats) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) CompletionStats(org.elasticsearch.search.suggest.completion.CompletionStats)

Example 3 with FieldMemoryStats

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));
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) FieldMemoryStats(org.elasticsearch.common.FieldMemoryStats) Matchers.containsString(org.hamcrest.Matchers.containsString) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) CompletionStats(org.elasticsearch.search.suggest.completion.CompletionStats)

Example 4 with FieldMemoryStats

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());
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) FieldMemoryStats(org.elasticsearch.common.FieldMemoryStats) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

FieldMemoryStats (org.elasticsearch.common.FieldMemoryStats)4 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)2 StreamInput (org.elasticsearch.common.io.stream.StreamInput)2 CompletionStats (org.elasticsearch.search.suggest.completion.CompletionStats)2 IOException (java.io.IOException)1 Fields (org.apache.lucene.index.Fields)1 LeafReader (org.apache.lucene.index.LeafReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Terms (org.apache.lucene.index.Terms)1 CompletionTerms (org.apache.lucene.search.suggest.document.CompletionTerms)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)1 IndicesStatsResponse (org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1