Simple Log Service allows you to execute search statements to query logs that are stored in a Logstore. The search results can be independently used or used in analytic statements for complex data analysis and processing. This topic describes the syntax, scenarios, and examples of search statements.
Basic syntax
Each query statement consists of a search statement and an analytic statement. The search statement and the analytic statement are separated with a vertical bar (|
). Format:
Search statement|Analytic statement
A search statement can be independently executed. An analytic statement must be executed together with a search statement. The log analysis feature is used to analyze data in the search results or all data in a Logstore.
We recommend that you specify up to 30 search conditions in a search statement.
You do not need to specify the FROM or WHERE clause in an analytic statement. By default, all data of the current Logstore is analyzed. Analytic statements do not support offsets and are not case-sensitive. You do not need to append a semicolon (;) to an analytic statement.
Statement | Description |
Search statement | A search statement specifies one or more search conditions. A search statement can be a keyword, a numeric value, a numeric value range, a space, or an asterisk (*). If you specify a space or an asterisk (*) as the search statement, no conditions are used for searching and all logs are returned. |
Analytic statement | An analytic statement is used to aggregate or analyze data in the search results or all data in a Logstore. For more information about the functions and syntax supported by Simple Log Service for analyzing logs, see the following topics: |
Sample query statement:
* | SELECT status, count(*) AS PV GROUP BY status
For more information about the raw logs in the search examples of this topic, see Debugging.
Writing process of search statements
To write a query statement, perform the following steps:
Step 1: Select a search type
Step 2: Select a field data type
Step 3: Select a match mode
Examples of search statements
If you execute a search statement on different logs based on different index configurations, the statement returns different results. The examples provided in this section are based on the following sample log and index configurations.
Sample log
An NGINX access log is used as the sample log.
Index configurations
Before you execute a search statement, make sure that indexes are configured. For more information, see Create indexes. To check index configurations, perform the following steps:
On the query and analysis page of a Logstore, choose
.In the Search & Analysis panel, check whether field indexes are configured.
Common search examples
Expected search result | Search statement | Debugging |
Logs that record successful GET requests (status codes: 200 to 299) |
| |
Logs that record GET requests and in which the source region of the requests is not the China (Hangzhou) region |
| None |
Logs that record GET requests or POST requests |
| |
Logs that do not record GET requests |
| |
Logs that record successful GET or POST requests |
| |
Logs that record failed GET or POST requests |
| |
Logs that record successful GET requests (status codes: 200 to 299) and in which the request duration is less than 60 seconds |
| |
Logs in which the request duration is equal to 60 seconds |
| |
| ||
Logs in which the request duration is greater than or equal to 60 seconds and is less than 200 seconds |
| |
| ||
Logs in which the request_time field is empty or the field value is an invalid number |
Note
| |
Logs that contain the request_time field and in which the field value is a number |
| |
Logs that contain and |
Note In this search statement, and is a common string but not an operator. | |
Logs in which the request method field value is PUT |
Important The name of the request method field contains spaces. You must enclose the field name in double quotation marks ("") in a search statement. | None |
Logs whose topic is HTTPS or HTTP |
| None |
Logs that are collected from the 192.0.2.1 host |
The Important If a log is processed by using the data transformation feature or a Logtail plug-in, the key in the __tag__:__client_ip__ field is converted to a common key. If you want to search for the log, you must enclose the name of the __tag__:__client_ip__ field in double quotation marks ("") in the search statement. Example: | None |
Logs whose IP addresses match |
For more information, see Use the LIKE clause to implement fuzzy match. | None |
Logs in which the remote_user field is not empty |
| |
Logs in which the remote_user field is empty |
| |
Logs in which the remote_user field value is not null |
| |
Logs that do not contain the remote_user field |
| |
Logs that contain the remote_user field |
| |
Logs in which the city field value is not Shanghai |
Note If you want to query Chinese strings, you must turn on Include Chinese when you configure indexes. For more information, see Create indexes. | None |
Advanced search examples
Fuzzy searches
Expected search result
Search statement
Debugging
Logs that contain specific words. The words start with cn.
cn*
Logs in which the region field value starts with cn.
region:cn*
None
Logs in which the region field value contains cn*.
region:"cn*"
NoteIn this search statement,
cn*
is a complete string. Examples:If the content of a log is
region:cn*,en
and the delimiter is a comma (,), Simple Log Service splits the log content intoregion
,cn*
, anden
. You can use the search statement to search for the log.If the content of a log is
region:cn*hangzhou
, Simple Log Service considerscn*hangzhou
as a whole. In this case, you cannot use the search statement to search for the log.
None
Logs that contain specific words. The words start with mozi, end with la, and include one character between mozi and la.
mozi?la
Logs that contain specific words. The words start with mo, end with la, and include zero or more characters between mo and la.
mo*la
Logs that contain specific words. The words start with moz or sa.
moz* and sa*
Logs in which the region field value ends with hai.
You cannot use a search statement to search for the logs. You can use the LIKE clause in an SQL statement to search for the logs. For more information, see Use the LIKE clause to implement fuzzy match.
* | select * from log where region like '%hai'
None
Delimiter-based searches
Simple Log Service splits the content of a log into multiple words based on the delimiters that you specify. The default delimiters are
, '";=()[]{}?@&<>/:\n\t\r
. If you leave Delimiter empty, Simple Log Service considers the value of each field as a whole. In this case, you can search for a log only by using a complete string or performing a fuzzy search. For more information about how to specify delimiters, see Create indexes.For example, the http_user_agent field value is
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/192.0.2.0 Safari/537.2
.If you leave Delimiter empty, Simple Log Service considers the field value as a whole. In this case, you cannot search for logs by using the
http_user_agent:Chrome
search statement.If you set Delimiter to
, '";=()[]{}?@&<>/:\n\t\r
, Simple Log Service splits the field value intoMozilla
,5.0
,Windows
,NT
,6.1
,AppleWebKit
,537.2
,KHTML
,like
,Gecko
,Chrome
,192.0.2.0
,Safari
, and537.2
. In this case, you can search for logs by using thehttp_user_agent:Chrome
search statement.
ImportantIf a search keyword contains delimiters, you can perform a phrase search or use a LIKE clause. Examples:
Phrase search:
#"redo_index/1"
. For more information, see Phrase search.LIKE clause:
* | select * from log where key like 'redo_index/1'
.
Expected search result
Search statement
Debugging
Logs in which the http_user_agent field value contains Chrome
http_user_agent:Chrome
Logs in which the http_user_agent field value contains Linux and Chrome
http_user_agent:Linux and http_user_agent:Chrome
http_user_agent:"Linux Chrome"
Logs in which the http_user_agent field value contains Firefox or Chrome
http_user_agent:Firefox or http_user_agent:Chrome
Logs in which the request_uri field value contains /request/path-2
request_uri:/request/path-2
Logs in which the request_uri field value starts with /request and does not contain /file-0
request_uri:/request* not request_uri:/file-0
Logs in which the
redo_index/1
phrase is completely matched#"redo_index/1"
* | select * from log where key like 'redo_index/1'
NoteYou can perform a phrase search or use a LIKE clause to completely match the phrase. If you perform an exact search, words such as
redo_index
and1
are matched.None
Query examples in special scenarios
Search statements
This operator is used to enclose a syntax keyword. If a syntax keyword is enclosed in double quotation marks (
""
), the keyword is converted to an ordinary word. In a field-specific search, the words that are enclosed in double quotation marks (""
) are considered as a whole.If a field name or field value contains special characters such as spaces, Chinese characters, colons (
:
), and hyphens (-
) or syntax keywords such asand
andor
, you must enclose the field name or field value in double quotation marks (""
). For example,"and"
returns the logs that contain the word and. In this case, and is not an operator.Simple Log Service supports the following operators:
sort
,asc
,desc
,group by
,avg
,sum
,min
,max
, andlimit
. If you want to use these operators as keywords, you must enclose the operators in double quotation marks (""
).If a log is processed by using the data transformation feature or a Logtail plug-in, the key in the __tag__:__client_ip__ field is converted to a common key. If you want to search for the log, you must enclose the name of the __tag__:__client_ip__ field in double quotation marks (
""
) in the search statement. Example:"__tag__:__client_ip__":192.0.2.1
. The__tag__:__client_ip__
field is a reserved field in Simple Log Service. The field indicates the IP address of the host from which logs are collected. For more information, see Reserved fields.
Expected query result
Search statement
Logs in which the
request method
field value containsPUT
. The name of therequest method
field contains a space."request method":PUT
Logs that are collected from the
192.0.2.1
host."__tag__:__client_ip__":192.0.2.1
Analytic statements
If a proper noun such as a field name or table name contains special characters such as spaces, Chinese characters, colons (
:
), and hyphens (-
) or syntax keywords such asand
andor
, you must enclose the proper noun in double quotation marks (""
) in analytic statements.If specific characters represent a string, you must use single quotation marks (
''
) to enclose the characters in analytic statements. For example,'status'
indicates the status string, andstatus
or"status"
indicates the status log field.
Expected query result
Query statement
Logs whose IP addresses match
192.168.XX.XX
* | select * from log where key like '192.168.%.%'
For more information, see How do I use quotation marks in query statements?
References
For more information about analytic functions and syntax, see Analytic functions and syntax.
Troubleshooting
Log query examples
You can query JSON logs whose field values are JSON objects or JSON arrays. For more information about query and analysis examples, see Query and analyze JSON logs and FAQ about the query and analysis of JSON logs.