Search indexes use inverted indexes and column stores to address complex query needs when a large amount of data exists. After you create a search index in the Tablestore console, you can use the search index to query data.

Prerequisites

Step 1: Create a search index

  1. Run the create_search_index command to create a search index named search_index.
    create_search_index -n search_index
  2. The following sample code shows how to enter the index schema as prompted:

    The index schema includes the settings of the search index (IndexSetting), the list of field schemas (FieldSchemas), and presorting settings for the search index (IndexSort). For more information about index schemas, see Create search indexes.

     {
    
        "IndexSetting": {
            "RoutingFields": null
        },
        "FieldSchemas": [
            {
                "FieldName": "gid",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "uid",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col2",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col3",
                "FieldType": "TEXT",
                "Index": true,
                "Analyzer": "single_word",
                "AnalyzerParameter": {
                    "CaseSensitive": true,
                    "DelimitWord": null
                },
                "EnableSortAndAgg": false,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col1",
                "FieldType": "KEYWORD",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col3V",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": true,
                "SourceFieldNames": [
                    "col3"
                ]
            }
        ]
    }

Step 2: Query data

  1. Run the search command to use the search_index search index to query data and return all indexed columns of each row that meets the query conditions.
    search -n search_index --return_all_indexed
  2. The following sample code shows how to enter the query conditions as prompted by the system:

    Search indexes support query methods such as match all query (MatchAllQuery), match query (MatchQuery), match phrase query (MatchPhraseQuery), term query (TermQuery), terms query (TermsQuery), and prefix query (PrefixQuery). In this example, term query is used. For more information about term query, see Term query.

    {
        "Offset": -1,
        "Limit": 10,
        "Collapse": null,
        "Sort": null,
        "GetTotalCount": true,
        "Token": null,
        "Query": {
            "Name": "TermQuery",
            "Query": {
                "FieldName": "uid",
                "Term": 10001
            }
        },
        "Aggregations": [{
            "Name": "avg",
            "Aggregation": {
                "AggName": "agg1",
                "Field": "pid"
            }
        }]
    }