Tüm Ürünler
Search
Belge Merkezi

Tablestore:Token-based wildcard query

Son Güncelleme:Oct 02, 2026

Joker karakter sorgusu (WildcardQuery) senaryolarında *word* desenini kullanırken, bulanık belirteçleme ve eşleşen ifade sorgusunu birleştirerek yüksek performanslı bulanık sorgular çalıştırabilirsiniz.

Arka plan bilgileri

Dosya adları veya cep telefonu numaraları gibi verilerin sorgulanması, veritabanı işlemlerinde sıkça karşılaşılan bulanık sorgu gereksinimlerindendir. Tablestore'da bulanık sorgu çalıştırmak için MySQL'deki LIKE fonksiyonuna benzer şekilde arama dizini üzerinde joker karakter sorgusu kullanabilirsiniz. Ancak joker karakter sorgularının sınırlamaları vardır. Sorgu ifadesi 32 karakteri aşamaz ve veri hacmi arttıkça performans düşer.

Bu sorunları çözmek için arama dizini, gelişmiş bulanık sorgu performansı sağlayan belirteç tabanlı joker karakter sorgularını destekler. Belirteç tabanlı joker karakter sorgusu kullanırken sorgu ifadesinin uzunluğu konusunda bir sınır bulunmaz. Ancak orijinal içerik en fazla 1.024 karakterle sınırlıdır. Bu sınırı aşan içerik kırpılır ve yalnızca ilk 1.024 karakter korunur.

Senaryolar

Bulanık sorguları senaryonuza göre uygun yöntemle çalıştırın.

  • *word* desenini kullanan joker karakter sorgusu senaryolarında belirteç tabanlı joker karakter sorgusu kullanabilirsiniz. Örneğin, herhangi bir konumda 123 içeren cep telefonu numaralarını bulmak için "123" araması yapabilirsiniz.

    Bu yöntem, standart joker karakter sorgusuna kıyasla çoğu durumda performansı 10 katın üzerinde artırır.

    Bir veri tablosunda file_name sütunu bulunduğunu varsayalım. Arama dizininde bu sütunun türü Text olarak ayarlanır ve analiz edici Fuzzy Analyzer olarak yapılandırılır. file_name sütun değeri 2021 woRK@Hangzhou olan satırı bulmak için eşleşen ifade sorgusu (MatchPhraseQuery) kullanmanız gerekir. Sorgu ifadesi, orijinal metnin ardışık bir alt dizesi olmalıdır.

    • Sorgu ifadesi 2021, 20, 21, work, WORK, @, Hang, zhou, Hangzhou veya @Hangzhou ise sorgu, file_name sütun değeri 2021 woRK@Hangzhou olan satır ile eşleşir.

    • Sorgu ifadesi 21work, 2021Hangzhou, 2120 veya #Hangzhou ise sorgu, file_name sütun değeri 2021 woRK@Hangzhou olan satır ile eşleşmez.

  • Diğer karmaşık sorgu senaryoları için joker karakter sorgusu kullanarak bulanık sorgular çalıştırabilirsiniz. Daha fazla bilgi için bkz. Joker karakter sorgusu.

Kullanım

Belirteç tabanlı yöntemi kullanarak bulanık sorgu çalıştırmak için şu adımları izleyin:

  1. Arama dizini oluştururken sütun türünü Text, analiz ediciyi ise Fuzzy Analyzer olarak ayarlayın. Diğer parametreler için varsayılan yapılandırmaları kullanabilirsiniz. Daha fazla bilgi için bkz. Arama dizini oluşturma.

    Not

    Zaten mevcut bir arama diziniz varsa, dinamik şema değiştirme özelliğini kullanarak belirtilen sütun için sanal bir sütun ekleyebilirsiniz. Sanal sütun türünü Text, analiz ediciyi ise Fuzzy Analyzer olarak ayarlayın. Daha fazla bilgi için bkz. Şemayı dinamik olarak değiştirme ve Sanal sütunlar.

  2. Verileri arama dizini kullanarak sorgularken MatchPhraseQuery kullanın. Daha fazla bilgi için bkz. Eşleşen ifade sorgusu.

Ek: Kullanım örneği

Aşağıdaki örnek, belirteç tabanlı joker karakter sorgusu kullanarak bulanık sorgunun nasıl çalıştırılacağını göstermektedir.

import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.model.ColumnValue;
import com.alicloud.openservices.tablestore.model.PrimaryKey;
import com.alicloud.openservices.tablestore.model.PrimaryKeyBuilder;
import com.alicloud.openservices.tablestore.model.PrimaryKeyValue;
import com.alicloud.openservices.tablestore.model.PutRowRequest;
import com.alicloud.openservices.tablestore.model.RowPutChange;
import com.alicloud.openservices.tablestore.model.search.CreateSearchIndexRequest;
import com.alicloud.openservices.tablestore.model.search.CreateSearchIndexResponse;
import com.alicloud.openservices.tablestore.model.search.FieldSchema;
import com.alicloud.openservices.tablestore.model.search.FieldType;
import com.alicloud.openservices.tablestore.model.search.IndexSchema;
import com.alicloud.openservices.tablestore.model.search.SearchQuery;
import com.alicloud.openservices.tablestore.model.search.SearchRequest;
import com.alicloud.openservices.tablestore.model.search.SearchResponse;
import com.alicloud.openservices.tablestore.model.search.query.QueryBuilders;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.Assert.assertEquals;

public class TestFuzzy {
    private static final String tableName = "analysis_test";
    private static final String indexName = "analysis_test_index";

    public void testFuzzyMatchPhrase(SyncClient client) throws Exception {
        // Define the table schema.
        IndexSchema indexSchema = new IndexSchema();
        indexSchema.setFieldSchemas(Collections.singletonList(
                // Note: If the name field was originally of the Keyword type, queries may fail after you change the field type to Text and configure an analyzer.
                // To retain both Keyword and Text types, see the example for the virtual column feature. If you only need to use the name field to perform queries that match *abc*, use only the Text type field. The Keyword type is not required.
                new FieldSchema("name", FieldType.TEXT).setAnalyzer(FieldSchema.Analyzer.Fuzzy)
        ));
        // Create a search index.
        {
            CreateSearchIndexRequest request = new CreateSearchIndexRequest();
            request.setTableName(tableName);
            request.setIndexName(indexName);
            request.setIndexSchema(indexSchema);
            CreateSearchIndexResponse response = client.createSearchIndex(request);
        }

        // Write a row of data.
        PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                .addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromString("1"))
                .addPrimaryKeyColumn("pk2", PrimaryKeyValue.fromLong(1))
                .addPrimaryKeyColumn("pk3", PrimaryKeyValue.fromBinary(new byte[]{1, 2, 3}))
                .build();
        RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
        // Write an attribute column.
        rowPutChange.addColumn("name", ColumnValue.fromString("Tuner1024x768P.mp4"));
        PutRowRequest request = new PutRowRequest(rowPutChange);
        client.putRow(request);

        // Wait for the data to be synchronized to the search index.
        Thread.sleep(1000 * 180);

        // Demonstrate the query feature that matches *abc*.
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tun", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tuner", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tun er", 0);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tuner102", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tuner1024", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tuner1024x", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tuner1024x7", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "Tuner1024x768P.mp4", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "24x768P.mp4", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "24x76 8P.mp4", 0);
        assertMatchPhraseQuery(client, tableName, indexName, "name", "24x7 P.mp4", 0);
    }

    // Use a virtual column.
    public void testFuzzyMatchPhraseWithVirtualField(SyncClient client) throws Exception {
        // Define the table schema.
        IndexSchema indexSchema = new IndexSchema();
        indexSchema.setFieldSchemas(Arrays.asList(
                // The source field is of the Keyword type, which is convenient for exact match queries.
                new FieldSchema("name", FieldType.KEYWORD).setIndex(true).setStore(true),
                // Create a virtual column named name_virtual_text. Set the type of the virtual column to Text and the analyzer to Fuzzy. The source of this virtual column is the name field.
                new FieldSchema("name_virtual_text", FieldType.TEXT).setIndex(true).setAnalyzer(FieldSchema.Analyzer.Fuzzy).setVirtualField(true).setSourceFieldName("name")
        ));
        // Create a search index.
        {
            CreateSearchIndexRequest request = new CreateSearchIndexRequest();
            request.setTableName(tableName);
            request.setIndexName(indexName);
            request.setIndexSchema(indexSchema);
            CreateSearchIndexResponse response = client.createSearchIndex(request);
        }

        // Write a row of data.
        PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                .addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromString("1"))
                .addPrimaryKeyColumn("pk2", PrimaryKeyValue.fromLong(1))
                .addPrimaryKeyColumn("pk3", PrimaryKeyValue.fromBinary(new byte[]{1, 2, 3}))
                .build();
        RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
        // Write an attribute column.
        rowPutChange.addColumn("name", ColumnValue.fromString("Tuner1024x768P.mp4"));
        PutRowRequest request = new PutRowRequest(rowPutChange);
        client.putRow(request);

        // Wait for the data to be synchronized to the search index.
        Thread.sleep(1000 * 180);

        // Demonstrate the query scenario that matches *abc*.
        // Note that the query field is the virtual column name_virtual_text, not name.
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tun", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tuner", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tun er", 0);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tuner102", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tuner1024", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tuner1024x", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tuner1024x7", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "Tuner1024x768P.mp4", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "24x768P.mp4", 1);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "24x76 8P.mp4", 0);
        assertMatchPhraseQuery(client, tableName, indexName, "name_virtual_text", "24x7 P.mp4", 0);
    }

    // Show how to implement MatchPhraseQuery.
    public static void assertMatchPhraseQuery(SyncClient client, String tableName, String indexName, String fieldName, String searchContent, long exceptCount) {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setTableName(tableName);
        searchRequest.setIndexName(indexName);
        SearchQuery searchQuery = new SearchQuery();
        // Use MatchPhraseQuery to query the tokenized field.
        searchQuery.setQuery(QueryBuilders.matchPhrase(fieldName, searchContent).build());
        searchQuery.setLimit(0);
        // To demonstrate the feature, this example returns the total number of matched rows. If you do not need the total count, set this to false for better performance.
        searchQuery.setGetTotalCount(true);
        searchRequest.setSearchQuery(searchQuery);
        SearchResponse response = client.search(searchRequest);
        assertEquals(String.format("field:[%s], searchContent:[%s]", fieldName, searchContent), exceptCount, response.getTotalCount());
    }
}

SSS

İlgili Konular