Call the CreateIndex API to create an index for a specified logstore.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
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.
|
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 |
case_sensitive | bool | No | Specifies whether to enable case sensitivity.
|
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.
|
Description of the IndexKeyConfig parameter:
Name | Type | Required | Description |
index_type | String | No | The type of the index. The default value is
|
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 |
case_sensitive | bool | No | Specifies whether to enable case sensitivity.
|
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 |
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.
|
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 |
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:
You can configure the |
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
The following APIs are available to manage indexes:
For more sample code, see Alibaba Cloud Simple Log Service SDK for Python on GitHub.