Create a saved search, 创建快速查询

Updated at:

Call CreateSavedSearch to create a saved search.

Prerequisites

Parameters

def create_savedsearch(self, project, detail):

Request parameters

Parameter

Type

Required

Description

project

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.

detail

String

Yes

A JSON string that contains the saved search configuration. Example:

detail = {
    "filterjson": "{\"filterJson\":{\"children\":[]},\"filterQuery\":\"\"}",
    "logstore": "logstore-1",
    "savedsearchName": "savedsearch-1",
    "searchQuery": "* | SELECT status, count(*) AS PV GROUP BY status",
    "topic": ""
}

The detail parameter contains the following fields:

Parameter

Type

Required

Description

logstore

String

Yes

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

savedsearchName

String

Yes

The name of the saved search. The name must be unique within a Simple Log Service project and 3 to 63 characters long. For more information, see Saved search.

searchQuery

String

Yes

The query statement or query and analysis statement. For more information, see Query and analysis overview.

topic

String

No

The topic of logs. The default value is an empty string. For more information, see Log topics.

Response parameters

For more information, see CreateSavedSearch.

Sample code

from aliyun.log import LogClient
import os

# This example shows how to obtain an AccessKey ID and an AccessKey Secret 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 Log Service.
endpoint = "cn-hangzhou.log.aliyuncs.com"

client = LogClient(endpoint, access_key_id, access_key_secret)

project = "proj-test-1"
detail = {
    "filterjson": "{\"filterJson\":{\"children\":[]},\"filterQuery\":\"\"}",
    "logstore": "logstore-1",
    "savedsearchName": "savedsearch-1",
    "searchQuery": "* | SELECT status, count(*) AS PV GROUP BY status",
    "topic": ""
}


def main():
    # Create a saved search.
    res = client.create_savedsearch(project, detail)
    res.log_print()


if __name__ == '__main__':
    main()
    

Sample response

header:  {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Tue, 15 Oct 2024 07:30:19 GMT', 'x-log-time': '1728977419', 'x-log-requestid': '670E1A0B12508244EEF3****'}

Process finished with exit code 0

References