All Products
Search
Document Center

Simple Log Service:Create an index

Last Updated:Mar 05, 2025

Call the CreateIndex API to create an index for a specified logstore.

Prerequisites

Parameter description

def create_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. For more information, see Manage a project.

logstore_name

String

Yes

The name of the logstore. The logstore in Simple Log Service is used to collect, store, and query logs. For more information, see Manage a logstore.

index_detail

IndexConfig

Yes

Index configuration information.

Description of the IndexConfig parameter:

Name

Type

Required

Description

line_config

IndexLineConfig

Yes

IndexLineConfig class, used to configure full-text index.

key_config_list

Dict[str, IndexLineConfig]

Yes

IndexKeyConfig class, used to configure field index and is of type dictionary. The keys of the dictionary are field names (strings), and the values are the corresponding index configurations (IndexLineConfig).

log_reduce

bool

No

Specifies whether to enable the LogReduce feature. After being enabled, either the whitelist or blacklist takes effect, but not both.

  • True: Enable LogReduce.

  • False (default): Do not enable LogReduce.

Description of the IndexLineConfig parameter:

Name

Type

Required

Description

token_list

List

Yes

The list of delimiters. Words are divided according to the given list of delimiters. For example, when the specified list of delimiters contains , and : , the log content hello:aliyun,sls will be split into three words: hello, aliyun, and sls. You can search for keywords when querying.

case_sensitive

bool

No

Specifies whether to enable case sensitivity.

  • True: Case sensitive.

  • False (default): Not case sensitive.

chinese

bool

No

Specifies whether to enable the Chinese word segmentation feature. After being enabled, the read and write capabilities of the shard will decrease to some extent.

  • True: Enable

  • False (default): Do not enable

Description of the IndexKeyConfig parameter:

Name

Type

Required

Description

index_type

String

No

The type of the index. The default value is text. Optional values are text, long, double, and json.

  • text: Text field. You can configure the token_list, case_sensitive, chinese, doc_value, and alias parameters.

  • long: Integer field. You can configure the doc_value and alias parameters.

  • double: Floating-point number field. You can configure the doc_value and alias parameters.

  • json: JSON text field. You can only configure the json_key_config parameter.

token_list

List

Yes

The list of delimiters. Words are divided according to the given list of delimiters. For example, when the specified list of delimiters contains , and : , the log content hello:aliyun,sls will be split into three words: hello, aliyun, and sls. You can search for keywords when querying.

case_sensitive

bool

No

Specifies whether to enable case sensitivity.

  • True: Case sensitive.

  • False (default): Not case sensitive.

doc_value

bool

No

Specifies whether to enable the statistical analysis feature. After it is enabled, the field can be used in analytic statements.

alias

String

No

The alias of the index key.

json_key_config

IndexJsonKeyConfig

No

IndexJsonKeyConfig class. When index_type is json, you need to configure this parameter.

chinese

bool

No

Specifies whether to enable the Chinese word segmentation feature. After being enabled, the read and write capabilities of the shard will decrease to some extent.

  • True: Enable

  • False (default): Do not enable

Description of the IndexJsonKeyConfig parameter:

Name

Type

Required

Description

index_all

bool

No

If all string values in JSON keys need to be indexed, set it to True.

max_depth

int

No

When index_all is True, only JSON values with depth <= max_depth will be indexed. The default value is -1, which indicates no depth limit.

alias

String

No

The alias of the index key.

json_keys

Dict[str, JsonKeyConfigValue]

No

The nested subfield index configuration in JSON fields. You can add subfields by calling the add_key method of the IndexJsonKeyConfig instance. For example, the log field is:

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

You can configure the index_type of the field jsonKey as json, and call the add_key("level_1_key_1", "long") and add_key("level_1_key_2.level_2_key_1", "text") on the json_key_config parameter to add field indexes for nested subfields.

Response parameters

For information about the response parameters, see CreateIndex.

Sample code

from aliyun.log import LogClient, IndexConfig, IndexKeyConfig, IndexLineConfig, IndexJsonKeyConfig
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', '')
# Specify a Simple Log Service endpoint.
endpoint = "ap-southeast-1.log.aliyuncs.com"

# Create an instance of 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", ";"]

    # Full-text index configuration.
    line_config = IndexLineConfig(token_list, case_sensitive=False, include_keys=None, exclude_keys=None, chinese=None)
    # Field index configuration.
    index_config = IndexKeyConfig(token_list, case_sensitive=False, index_type='text', doc_value=False, alias=None,
                                  json_key_config=None, chinese=None)
    # Construct index_detail configuration information.
    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)

    # Create an index for the specified logstore.
    res = client.create_index(project_name, logstore_name, index_detail)
    res.log_print()


if __name__ == '__main__':
    main()

Sample response

CreateIndexResponse:
headers: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Sat, 12 Oct 2024 06:27:20 GMT', 'x-log-time': '1728714440', 'x-log-requestid': '670A16C88F14B9A316BC3ADE'}

Process finished with exit code 0

References