Tüm Ürünler
Search
Belge Merkezi

Tablestore:Search index quick start

Son Güncelleme:Oct 02, 2026

Arama dizinleri, verileri birincil anahtar sütunları dışındaki sütunlara ve birden çok koşula göre sorgulamanıza olanak tanır. Bu konuda, Tablestore SDK for Java kullanarak bir veri tablosu ve arama dizini oluşturma, veri yazma ve terim sorgusu çalıştırma işlemleri ürün verileri üzerinden örneklenmiştir.

Ön hazırlıklar

  • Tablestore hizmetini etkinleştirin ve bir örnek oluşturun. Daha fazla bilgi için bkz. Hizmeti etkinleştirme ve örnek oluşturma.

  • Tablestore SDK for Java paketini yükleyin, istemciyi başlatın ve bir SyncClient örneği edinin. Daha fazla bilgi için bkz. Java SDK.

Yordam

Bu örnekte, example_table adlı bir veri tablosu ve example_index adlı bir arama dizini oluşturulur. Arama dizini hazır duruma geldikten sonra üç ürün satırı yazılır ve category sütun değeri books olan satırlar sorgulanır.

Aşağıdaki adımlarda yer alan örnek yöntemleri, istemcinin başlatıldığı Java uygulamasına ekleyin. Ardından yöntemleri aşağıdaki sırayla çağırın:

private static void runQuickStart(SyncClient client)
        throws InterruptedException {
    String tableName = "example_table";
    String indexName = "example_index";

    createTable(client, tableName);
    createSearchIndex(client, tableName, indexName);
    waitUntilSearchIndexIsReady(client, tableName, indexName);
    putSampleRows(client, tableName);

    SearchResponse response =
            waitUntilBooksAreQueryable(client, tableName, indexName);
    System.out.println("Total count: " + response.getTotalCount());
    System.out.println("Rows: " + response.getRows());
}

1. Adım: Veri tablosu oluşturma

Bir veri tablosu için arama dizini oluşturmak üzere maksimum sürüm sayısını 1 olarak ayarlamanız gerekir. Ayrıca veri tablosunun yaşam süresini (TTL) -1 (verilerin hiçbir zaman sona ermediğini belirtir) olarak ayarlamalı veya veri güncellemelerini devre dışı bırakmalısınız. Bu örnekte maksimum sürüm sayısı 1, TTL değeri ise -1 olarak ayarlanır.

private static void createTable(
        SyncClient client, String tableName) {
    TableMeta tableMeta = new TableMeta(tableName);
    tableMeta.addPrimaryKeyColumn(
            new PrimaryKeySchema(
                    "product_id", PrimaryKeyType.STRING));

    TableOptions tableOptions = new TableOptions();
    tableOptions.setTimeToLive(-1);
    tableOptions.setMaxVersions(1);

    client.createTable(
            new CreateTableRequest(tableMeta, tableOptions));
}

2. Adım: Arama dizini oluşturma

category ve price nitelik sütunları için dizin alanları oluşturun. Bir dizin alanının adı ve veri türü, veri tablosundaki ilgili nitelik sütunuyla aynı olmalıdır.

private static void createSearchIndex(
        SyncClient client, String tableName, String indexName) {
    IndexSchema indexSchema = new IndexSchema();
    indexSchema.setFieldSchemas(Arrays.asList(
            new FieldSchema("category", FieldType.KEYWORD),
            new FieldSchema("price", FieldType.LONG)));

    CreateSearchIndexRequest request =
            new CreateSearchIndexRequest();
    request.setTableName(tableName);
    request.setIndexName(indexName);
    request.setIndexSchema(indexSchema);
    client.createSearchIndex(request);
}

Arama dizinleri eşzamansız olarak oluşturulur. Verileri sorgulamadan önce dizin durumunun RUNNING ve senkronizasyon aşamasının INCR değerine geçmesini bekleyin.

private static final int RETRY_ATTEMPTS = 90;
private static final long RETRY_INTERVAL_MILLIS = 1_000L;

private static void waitUntilSearchIndexIsReady(
        SyncClient client, String tableName, String indexName)
        throws InterruptedException {
    for (int attempt = 1; attempt <= RETRY_ATTEMPTS; attempt++) {
        DescribeSearchIndexRequest request =
                new DescribeSearchIndexRequest();
        request.setTableName(tableName);
        request.setIndexName(indexName);
        request.setIncludeSyncStat(true);

        DescribeSearchIndexResponse response =
                client.describeSearchIndex(request);
        if (response.getIndexStatus() != null
                && response.getIndexStatus().indexStatusEnum
                == DescribeSearchIndexResponse.IndexStatusEnum.RUNNING
                && response.getSyncStat() != null
                && response.getSyncStat().getSyncPhase()
                == SyncStat.SyncPhase.INCR) {
            return;
        }
        Thread.sleep(RETRY_INTERVAL_MILLIS);
    }
    throw new IllegalStateException(
            "Search index did not become ready before timeout.");
}

3. Adım: Örnek veri yazma

Üç ürün satırı yazın. product_id sütunu birincil anahtar sütunudur. category ve price sütunları ise nitelik sütunlarıdır.

private static void putSampleRows(
        SyncClient client, String tableName) {
    putProduct(client, tableName, "product-001", "books", 89L);
    putProduct(client, tableName, "product-002", "books", 129L);
    putProduct(client, tableName, "product-003", "devices", 599L);
}

private static void putProduct(
        SyncClient client,
        String tableName,
        String productId,
        String category,
        long price) {
    PrimaryKey primaryKey =
            PrimaryKeyBuilder.createPrimaryKeyBuilder()
                    .addPrimaryKeyColumn(
                            "product_id",
                            PrimaryKeyValue.fromString(productId))
                    .build();

    RowPutChange rowPutChange =
            new RowPutChange(tableName, primaryKey);
    rowPutChange.addColumn(
            "category", ColumnValue.fromString(category));
    rowPutChange.addColumn(
            "price", ColumnValue.fromLong(price));
    client.putRow(new PutRowRequest(rowPutChange));
}

4. Adım: Veri sorgulama

category sütun değeri books olan satırları eşleştirmek için bir terim sorgusu çalıştırın. Verilerin arama dizinine senkronize edilmesi kısa bir süre alır. Sonuçlar hazır değilse örnek kod sorguyu yeniden dener.

private static SearchResponse waitUntilBooksAreQueryable(
        SyncClient client, String tableName, String indexName)
        throws InterruptedException {
    for (int attempt = 1; attempt <= RETRY_ATTEMPTS; attempt++) {
        SearchResponse response =
                queryBooks(client, tableName, indexName);
        if (response.isAllSuccess()
                && response.getTotalCount() == 2L
                && response.getRows().size() == 2) {
            return response;
        }
        Thread.sleep(RETRY_INTERVAL_MILLIS);
    }
    throw new IllegalStateException(
            "Sample rows were not queryable before timeout.");
}

private static SearchResponse queryBooks(
        SyncClient client, String tableName, String indexName) {
    TermQuery termQuery = new TermQuery();
    termQuery.setFieldName("category");
    termQuery.setTerm(ColumnValue.fromString("books"));

    SearchQuery searchQuery = new SearchQuery();
    searchQuery.setQuery(termQuery);
    searchQuery.setLimit(10);
    searchQuery.setTrackTotalCount(
            SearchQuery.TRACK_TOTAL_COUNT);

    SearchRequest.ColumnsToGet columnsToGet =
            new SearchRequest.ColumnsToGet();
    columnsToGet.setReturnAll(true);

    SearchRequest request =
            new SearchRequest(tableName, indexName, searchQuery);
    request.setColumnsToGet(columnsToGet);
    return client.search(request);
}

Eşleşen toplam satır sayısı ile döndürülen satır sayısı her ikisi de 2 ise sorgu başarılı olmuş demektir.

Kaynakları temizleme

Örnek kaynaklara artık ihtiyacınız yoksa veri tablosunu silmeden önce arama dizinini silin.

DeleteSearchIndexRequest deleteIndexRequest =
        new DeleteSearchIndexRequest();
deleteIndexRequest.setTableName("example_table");
deleteIndexRequest.setIndexName("example_index");
client.deleteSearchIndex(deleteIndexRequest);

client.deleteTable(new DeleteTableRequest("example_table"));
client.shutdown();

Sonraki adımlar

  • Arama dizinlerinin çalışma şekli, kullanım senaryoları ve sınırlamalar hakkında bilgi edinmek için bkz. Arama dizini.

  • Dizin sıralama, TTL, sanal sütunlar ve diğer dizin özellikleri hakkında bilgi edinmek için bkz. Arama dizini oluşturma.

  • Diğer sorgu türleri ve sorgu sonuçlarını sıralama, toplama veya yinelenenleri kaldırma hakkında bilgi edinmek için bkz. Veri sorgulama.