Update index

Updated at:

Call the UpdateIndex operation to update the index of a Logstore.

Prerequisites

Parameter description

def update_index(self, project_name, logstore_name, index_detail):

Request parameters

Parameter

Type

Required

Description

project_name

String

Yes

The name of the project. The project in Simple Log Service is used to isolate the resources of different users and control access to specific resources. See Manage projects.

logstore_name

String

Yes

The name of the logstore. The logstore in Simple Log Service is used to collect, store, and query logs. See Manage Logstores.

index_detail

IndexConfig

Yes

Index configuration.

IndexConfig parameters

Name

Type

Required

Description

line_config

IndexLineConfig

Yes

Configures the full-text index.

key_config_list

Dict[str, IndexLineConfig]

Yes

Field index configuration. Keys are field names (strings), values are IndexLineConfig objects.

log_reduce

bool

No

Enables LogReduce. When enabled, either the whitelist or blacklist takes effect, but not both.

  • True: Enabled.

  • False (default): Disabled.

IndexLineConfig parameters

Name

Type

Required

Description

token_list

List

Yes

Delimiters used to split log content into tokens. For example, with delimiters , and : , the log hello:aliyun,sls splits into hello, aliyun, and sls.

case_sensitive

bool

No

Enables case-sensitive matching.

  • True: Case sensitive.

  • False (default): Case-insensitive.

chinese

bool

No

Enables Chinese word segmentation. When enabled, shard read/write throughput decreases.

  • True: Enabled.

  • False (default): Disabled.

IndexKeyConfig parameters

Name

Type

Required

Description

index_type

String

No

Index type. Default: text. Valid values: text, long, double, and json.

  • text: Text field. Configurable parameters: token_list, case_sensitive, chinese, doc_value, and alias.

  • long: Integer field. Configurable parameters: doc_value and alias.

  • double: Floating-point field. Configurable parameters: doc_value and alias.

  • json: JSON text field. Only the json_key_config parameter is configurable.

token_list

List

Yes

Delimiters used to split log content into tokens. For example, with delimiters , and : , the log hello:aliyun,sls splits into hello, aliyun, and sls.

case_sensitive

bool

No

Enables case-sensitive matching.

  • True: Case sensitive.

  • False (default): Case-insensitive.

doc_value

bool

No

Enables statistical analysis. When enabled, the field can be used in analytic statements.

alias

String

No

Alias of the index key.

json_key_config

IndexJsonKeyConfig

No

JSON subfield index configuration. Required when index_type is json.

chinese

bool

No

Enables Chinese word segmentation. When enabled, shard read/write throughput decreases.

  • True: Enabled.

  • False (default): Disabled.

IndexJsonKeyConfig parameters

Name

Type

Required

Description

index_all

bool

No

When set to True, indexes all string values in JSON keys.

max_depth

int

No

When index_all is True, only JSON values with depth <= max_depth are indexed. Default: -1 (no depth limit).

alias

String

No

Alias of the index key.

json_keys

Dict[str, JsonKeyConfigValue]

No

Subfield index configuration for JSON fields. Add subfields by calling add_key on the IndexJsonKeyConfig instance. Example log field:

jsonKey:{
    "level_1_key_1": 123,
    "level_1_key_2": {
        "level_2_key_1": "hello"
    }
}

Set index_type of field jsonKey to json, then call add_key("level_1_key_1", "long") and add_key("level_1_key_2.level_2_key_1", "text") on json_key_config to index the subfields.

Response parameters

For response parameters, see: UpdateIndex - Update Index.

Sample code

from aliyun.log import LogClient, IndexConfig, IndexKeyConfig, IndexLineConfig
import os

# In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The endpoint of Simple Log Service.
endpoint = "cn-hangzhou.log.aliyuncs.com"

# Instantiate the LogClient class.
client = LogClient(endpoint, access_key_id, access_key_secret)

project_name = "project-1"
logstore_name = "logstore-1"


def main():
    ttl = 1
    token_list = [",", "\t", "\n", ";"]

    # Configure a full-text index.
    line_config = IndexLineConfig(token_list, case_sensitive=False, include_keys=None, exclude_keys=None, chinese=None)
    # Configure a field index.
    index_config = IndexKeyConfig(token_list, case_sensitive=False, index_type='text', doc_value=False, alias=None,
                                  json_key_config=None, chinese=None)
    # Construct the index_detail configuration.
    key_config_list = {"key_1": index_config, "key_2": index_config}
    index_detail = IndexConfig(ttl, line_config, key_config_list, all_keys_config=None, log_reduce=None)

    # Update the index configuration of the specified Logstore.
    res = client.update_index(project_name, logstore_name, index_detail)
    res.log_print()


if __name__ == '__main__':
    main()

Sample response

UpdateIndexResponse:
headers: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Sat, 12 Oct 2024 06:31:53 GMT', 'x-log-time': '1728714713', 'x-log-requestid': '670A17D9A94ADA57D6BB****'}

Process finished with exit code 0

References