You can manage indexes in the Simple Log Service console with ease. For more information, see Create indexes.
The following sample code provides an example on how to create an index named testindex. In this example, the full-text indexing is enabled, and field indexing is enabled for the request_method and status fields. This example is based on the raw log.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Index;
import com.aliyun.openservices.log.exception.LogException;
public class CreateIndex {
public static void main(String[] args) throws LogException {
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
String projectName = "ali-test-project";
String logstoreName = "ali-test-logstore";
String host = "https://cn-hangzhou.log.aliyuncs.com";
Client client = new Client(host, accessId, accessKey);
try {
String logstoreindex = "{\"line\": {\"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": false, \"chn\": false}, \"keys\": {\"request_method\": {\"type\": \"text\", \"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": false, \"alias\": \"\", \"doc_value\": true, \"chn\": false}, \"status\": {\"type\": \"long\", \"alias\": \"\", \"doc_value\": true}}, \"log_reduce\": false, \"max_text_len\": 2048}";
Index index = new Index();
System.out.println("ready to create index");
index.FromJsonString(logstoreindex);
client.CreateIndex(projectName, logstoreName, index);
System.out.println(String.format("create index for %s success", logstoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}