Search in sources :

Example 1 with CompletionStats

use of org.opensearch.search.suggest.completion.CompletionStats in project OpenSearch by opensearch-project.

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)).get();
    ensureGreen();
    AcknowledgedResponse putMappingResponse = client().admin().indices().preparePutMapping(INDEX).setSource(jsonBuilder().startObject().startObject(MapperService.SINGLE_MAPPING_NAME).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).setId("1").setSource(jsonBuilder().startObject().field(FIELD, "Foo Fighters").field(otherField, "WHATEVER").endObject()).get();
    client().prepareIndex(INDEX).setId("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.opensearch.action.admin.indices.stats.IndicesStatsResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) FieldMemoryStats(org.opensearch.common.FieldMemoryStats) Matchers.containsString(org.hamcrest.Matchers.containsString) CompletionStats(org.opensearch.search.suggest.completion.CompletionStats)

Example 2 with CompletionStats

use of org.opensearch.search.suggest.completion.CompletionStats in project OpenSearch by opensearch-project.

the class CompletionStatsCache method filterCompletionStatsByFieldName.

private static CompletionStats filterCompletionStatsByFieldName(String[] fieldNamePatterns, CompletionStats fullCompletionStats) {
    final FieldMemoryStats fieldMemoryStats;
    if (CollectionUtils.isEmpty(fieldNamePatterns) == false) {
        final ObjectLongHashMap<String> completionFields = new ObjectLongHashMap<>(fieldNamePatterns.length);
        for (ObjectLongCursor<String> fieldCursor : fullCompletionStats.getFields()) {
            if (Regex.simpleMatch(fieldNamePatterns, fieldCursor.key)) {
                completionFields.addTo(fieldCursor.key, fieldCursor.value);
            }
        }
        fieldMemoryStats = new FieldMemoryStats(completionFields);
    } else {
        fieldMemoryStats = null;
    }
    return new CompletionStats(fullCompletionStats.getSizeInBytes(), fieldMemoryStats);
}
Also used : ObjectLongHashMap(com.carrotsearch.hppc.ObjectLongHashMap) FieldMemoryStats(org.opensearch.common.FieldMemoryStats) CompletionStats(org.opensearch.search.suggest.completion.CompletionStats)

Example 3 with CompletionStats

use of org.opensearch.search.suggest.completion.CompletionStats in project OpenSearch by opensearch-project.

the class CompletionStatsCache method get.

CompletionStats get(String... fieldNamePatterns) {
    final PlainActionFuture<CompletionStats> newFuture = new PlainActionFuture<>();
    // final PlainActionFuture<CompletionStats> oldFuture = completionStatsFutureRef.compareAndExchange(null, newFuture);
    // except JDK8 doesn't have compareAndExchange so we emulate it:
    final PlainActionFuture<CompletionStats> oldFuture;
    synchronized (completionStatsFutureMutex) {
        if (completionStatsFuture == null) {
            completionStatsFuture = newFuture;
            oldFuture = null;
        } else {
            oldFuture = completionStatsFuture;
        }
    }
    if (oldFuture != null) {
        // we lost the race, someone else is already computing stats, so we wait for that to finish
        return filterCompletionStatsByFieldName(fieldNamePatterns, oldFuture.actionGet());
    }
    // we won the race, nobody else is already computing stats, so it's up to us
    ActionListener.completeWith(newFuture, () -> {
        long sizeInBytes = 0;
        final ObjectLongHashMap<String> completionFields = new ObjectLongHashMap<>();
        try (Engine.Searcher currentSearcher = searcherSupplier.get()) {
            for (LeafReaderContext atomicReaderContext : currentSearcher.getIndexReader().leaves()) {
                LeafReader atomicReader = atomicReaderContext.reader();
                for (FieldInfo info : atomicReader.getFieldInfos()) {
                    Terms terms = atomicReader.terms(info.name);
                    if (terms instanceof CompletionTerms) {
                        // TODO: currently we load up the suggester for reporting its size
                        final long fstSize = ((CompletionTerms) terms).suggester().ramBytesUsed();
                        completionFields.addTo(info.name, fstSize);
                        sizeInBytes += fstSize;
                    }
                }
            }
        }
        return new CompletionStats(sizeInBytes, new FieldMemoryStats(completionFields));
    });
    boolean success = false;
    final CompletionStats completionStats;
    try {
        completionStats = newFuture.actionGet();
        success = true;
    } finally {
        if (success == false) {
            // completionStatsFutureRef.compareAndSet(newFuture, null); except we're not using AtomicReference in JDK8
            synchronized (completionStatsFutureMutex) {
                if (completionStatsFuture == newFuture) {
                    completionStatsFuture = null;
                }
            }
        }
    }
    return filterCompletionStatsByFieldName(fieldNamePatterns, completionStats);
}
Also used : ObjectLongHashMap(com.carrotsearch.hppc.ObjectLongHashMap) LeafReader(org.apache.lucene.index.LeafReader) Terms(org.apache.lucene.index.Terms) CompletionTerms(org.apache.lucene.search.suggest.document.CompletionTerms) CompletionTerms(org.apache.lucene.search.suggest.document.CompletionTerms) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FieldMemoryStats(org.opensearch.common.FieldMemoryStats) FieldInfo(org.apache.lucene.index.FieldInfo) CompletionStats(org.opensearch.search.suggest.completion.CompletionStats)

Example 4 with CompletionStats

use of org.opensearch.search.suggest.completion.CompletionStats in project OpenSearch by opensearch-project.

the class CommonStats method add.

public void add(CommonStats stats) {
    if (docs == null) {
        if (stats.getDocs() != null) {
            docs = new DocsStats();
            docs.add(stats.getDocs());
        }
    } else {
        docs.add(stats.getDocs());
    }
    if (store == null) {
        if (stats.getStore() != null) {
            store = new StoreStats();
            store.add(stats.getStore());
        }
    } else {
        store.add(stats.getStore());
    }
    if (indexing == null) {
        if (stats.getIndexing() != null) {
            indexing = new IndexingStats();
            indexing.add(stats.getIndexing());
        }
    } else {
        indexing.add(stats.getIndexing());
    }
    if (get == null) {
        if (stats.getGet() != null) {
            get = new GetStats();
            get.add(stats.getGet());
        }
    } else {
        get.add(stats.getGet());
    }
    if (search == null) {
        if (stats.getSearch() != null) {
            search = new SearchStats();
            search.add(stats.getSearch());
        }
    } else {
        search.add(stats.getSearch());
    }
    if (merge == null) {
        if (stats.getMerge() != null) {
            merge = new MergeStats();
            merge.add(stats.getMerge());
        }
    } else {
        merge.add(stats.getMerge());
    }
    if (refresh == null) {
        if (stats.getRefresh() != null) {
            refresh = new RefreshStats();
            refresh.add(stats.getRefresh());
        }
    } else {
        refresh.add(stats.getRefresh());
    }
    if (flush == null) {
        if (stats.getFlush() != null) {
            flush = new FlushStats();
            flush.add(stats.getFlush());
        }
    } else {
        flush.add(stats.getFlush());
    }
    if (warmer == null) {
        if (stats.getWarmer() != null) {
            warmer = new WarmerStats();
            warmer.add(stats.getWarmer());
        }
    } else {
        warmer.add(stats.getWarmer());
    }
    if (queryCache == null) {
        if (stats.getQueryCache() != null) {
            queryCache = new QueryCacheStats();
            queryCache.add(stats.getQueryCache());
        }
    } else {
        queryCache.add(stats.getQueryCache());
    }
    if (fieldData == null) {
        if (stats.getFieldData() != null) {
            fieldData = new FieldDataStats();
            fieldData.add(stats.getFieldData());
        }
    } else {
        fieldData.add(stats.getFieldData());
    }
    if (completion == null) {
        if (stats.getCompletion() != null) {
            completion = new CompletionStats();
            completion.add(stats.getCompletion());
        }
    } else {
        completion.add(stats.getCompletion());
    }
    if (segments == null) {
        if (stats.getSegments() != null) {
            segments = new SegmentsStats();
            segments.add(stats.getSegments());
        }
    } else {
        segments.add(stats.getSegments());
    }
    if (translog == null) {
        if (stats.getTranslog() != null) {
            translog = new TranslogStats();
            translog.add(stats.getTranslog());
        }
    } else {
        translog.add(stats.getTranslog());
    }
    if (requestCache == null) {
        if (stats.getRequestCache() != null) {
            requestCache = new RequestCacheStats();
            requestCache.add(stats.getRequestCache());
        }
    } else {
        requestCache.add(stats.getRequestCache());
    }
    if (recoveryStats == null) {
        if (stats.getRecoveryStats() != null) {
            recoveryStats = new RecoveryStats();
            recoveryStats.add(stats.getRecoveryStats());
        }
    } else {
        recoveryStats.add(stats.getRecoveryStats());
    }
}
Also used : StoreStats(org.opensearch.index.store.StoreStats) RefreshStats(org.opensearch.index.refresh.RefreshStats) IndexingStats(org.opensearch.index.shard.IndexingStats) WarmerStats(org.opensearch.index.warmer.WarmerStats) TranslogStats(org.opensearch.index.translog.TranslogStats) GetStats(org.opensearch.index.get.GetStats) SearchStats(org.opensearch.index.search.stats.SearchStats) RecoveryStats(org.opensearch.index.recovery.RecoveryStats) SegmentsStats(org.opensearch.index.engine.SegmentsStats) FlushStats(org.opensearch.index.flush.FlushStats) QueryCacheStats(org.opensearch.index.cache.query.QueryCacheStats) MergeStats(org.opensearch.index.merge.MergeStats) DocsStats(org.opensearch.index.shard.DocsStats) RequestCacheStats(org.opensearch.index.cache.request.RequestCacheStats) FieldDataStats(org.opensearch.index.fielddata.FieldDataStats) CompletionStats(org.opensearch.search.suggest.completion.CompletionStats)

Example 5 with CompletionStats

use of org.opensearch.search.suggest.completion.CompletionStats in project OpenSearch by opensearch-project.

the class CompletionsStatsTests method testSerialize.

public void testSerialize() throws IOException {
    FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats();
    CompletionStats stats = new CompletionStats(randomNonNegativeLong(), map);
    BytesStreamOutput out = new BytesStreamOutput();
    stats.writeTo(out);
    StreamInput input = out.bytes().streamInput();
    CompletionStats read = new CompletionStats(input);
    assertEquals(-1, input.read());
    assertEquals(stats.getSizeInBytes(), read.getSizeInBytes());
    assertEquals(stats.getFields(), read.getFields());
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) FieldMemoryStats(org.opensearch.common.FieldMemoryStats) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) CompletionStats(org.opensearch.search.suggest.completion.CompletionStats)

Aggregations

CompletionStats (org.opensearch.search.suggest.completion.CompletionStats)9 FieldMemoryStats (org.opensearch.common.FieldMemoryStats)4 QueryCacheStats (org.opensearch.index.cache.query.QueryCacheStats)4 SegmentsStats (org.opensearch.index.engine.SegmentsStats)4 FieldDataStats (org.opensearch.index.fielddata.FieldDataStats)4 FlushStats (org.opensearch.index.flush.FlushStats)4 GetStats (org.opensearch.index.get.GetStats)4 MergeStats (org.opensearch.index.merge.MergeStats)4 RefreshStats (org.opensearch.index.refresh.RefreshStats)4 SearchStats (org.opensearch.index.search.stats.SearchStats)4 DocsStats (org.opensearch.index.shard.DocsStats)3 ObjectLongHashMap (com.carrotsearch.hppc.ObjectLongHashMap)2 CommonStats (org.opensearch.action.admin.indices.stats.CommonStats)2 IndicesStatsResponse (org.opensearch.action.admin.indices.stats.IndicesStatsResponse)2 ShardStats (org.opensearch.action.admin.indices.stats.ShardStats)2 ShardRouting (org.opensearch.cluster.routing.ShardRouting)2 UnassignedInfo (org.opensearch.cluster.routing.UnassignedInfo)2 Table (org.opensearch.common.Table)2 RequestCacheStats (org.opensearch.index.cache.request.RequestCacheStats)2 IndexingStats (org.opensearch.index.shard.IndexingStats)2