|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.logsdb.patternedtext; |
| 9 | + |
| 10 | +import org.elasticsearch.ResourceNotFoundException; |
| 11 | +import org.elasticsearch.action.ActionType; |
| 12 | +import org.elasticsearch.action.DocWriteRequest; |
| 13 | +import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; |
| 14 | +import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; |
| 15 | +import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; |
| 16 | +import org.elasticsearch.action.bulk.BulkRequest; |
| 17 | +import org.elasticsearch.action.bulk.BulkResponse; |
| 18 | +import org.elasticsearch.action.index.IndexRequest; |
| 19 | +import org.elasticsearch.action.support.IndicesOptions; |
| 20 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 21 | +import org.elasticsearch.common.settings.Settings; |
| 22 | +import org.elasticsearch.common.time.DateFormatter; |
| 23 | +import org.elasticsearch.core.Tuple; |
| 24 | +import org.elasticsearch.index.IndexMode; |
| 25 | +import org.elasticsearch.index.IndexSettings; |
| 26 | +import org.elasticsearch.index.mapper.DateFieldMapper; |
| 27 | +import org.elasticsearch.test.ESIntegTestCase; |
| 28 | +import org.elasticsearch.test.ESTestCase; |
| 29 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 30 | +import org.elasticsearch.xcontent.XContentFactory; |
| 31 | +import org.elasticsearch.xcontent.XContentType; |
| 32 | +import org.elasticsearch.xcontent.json.JsonXContent; |
| 33 | +import org.elasticsearch.xpack.core.security.action.apikey.CreateApiKeyAction; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.time.Instant; |
| 37 | +import java.time.ZoneOffset; |
| 38 | +import java.time.ZonedDateTime; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.UUID; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | + |
| 43 | +public class PatternedTextRandomTests extends ESIntegTestCase { |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + public void test() throws IOException { |
| 48 | + var settings = Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.LOGSDB.getName()); |
| 49 | + String index = "test_index"; |
| 50 | + var mappings = XContentFactory.jsonBuilder() |
| 51 | + .startObject() |
| 52 | + .startObject("properties") |
| 53 | + .startObject("@timestamp") |
| 54 | + .field("type", "date") |
| 55 | + .endObject() |
| 56 | + .startObject("field_patterned_text") |
| 57 | + .field("type", "patterned_text") |
| 58 | + .endObject() |
| 59 | + .startObject("field_match_only_text") |
| 60 | + .field("type", "match_only_text") |
| 61 | + .endObject() |
| 62 | + .endObject() |
| 63 | + .endObject(); |
| 64 | + |
| 65 | + var createRequest = new CreateIndexRequest(index) |
| 66 | + .settings(settings) |
| 67 | + .mapping(mappings); |
| 68 | + |
| 69 | + var createResponse = safeGet(admin().indices().create(createRequest)); |
| 70 | + assertTrue(createResponse.isAcknowledged()); |
| 71 | + |
| 72 | + BulkRequest bulkRequest = new BulkRequest(); |
| 73 | + int numDocs = randomIntBetween(1, 300); |
| 74 | + long timestamp = System.currentTimeMillis(); |
| 75 | + for (int i = 0; i < numDocs; i++) { |
| 76 | + timestamp += TimeUnit.SECONDS.toMillis(1); |
| 77 | + String logMessage = randomMessage(); |
| 78 | + var indexRequest = new IndexRequest(index).opType(DocWriteRequest.OpType.CREATE) |
| 79 | + .source( |
| 80 | + JsonXContent.contentBuilder() |
| 81 | + .startObject() |
| 82 | + .field("@timestamp", timestamp) |
| 83 | + .field("field_patterned_text", logMessage) |
| 84 | + .field("field_match_only_text", logMessage); |
| 85 | + .endObject() |
| 86 | + ); |
| 87 | + bulkRequest.add(indexRequest); |
| 88 | + } |
| 89 | + BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet(); |
| 90 | + |
| 91 | + client().index( |
| 92 | + |
| 93 | + ) |
| 94 | + ); |
| 95 | + safeGet( |
| 96 | + indicesAdmin().refresh( |
| 97 | + new RefreshRequest(".ds-" + dataStreamName + "*").indicesOptions(IndicesOptions.lenientExpandOpenHidden()) |
| 98 | + ) |
| 99 | + ); |
| 100 | + |
| 101 | + int numDocs = randomIntBetween(100, 1000); |
| 102 | + |
| 103 | + |
| 104 | + new BulkRequest() |
| 105 | + . |
| 106 | + |
| 107 | + for (int i = 0; i < numDocs; i++) { |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + public static String randomMessage() { |
| 116 | + if (rarely()) { |
| 117 | + return randomRealisticUnicodeOfCodepointLength(randomIntBetween(1, 100)); |
| 118 | + } |
| 119 | + |
| 120 | + StringBuilder message = new StringBuilder(); |
| 121 | + int numTokens = randomIntBetween(1, 30); |
| 122 | + |
| 123 | + if (randomBoolean()) { |
| 124 | + message.append("[").append(randomTimestamp()).append("]"); |
| 125 | + } |
| 126 | + for (int i = 0; i < numTokens; i++) { |
| 127 | + message.append(randomSeparator()); |
| 128 | + |
| 129 | + if (randomBoolean()) { |
| 130 | + message.append(randomSentence()); |
| 131 | + } else { |
| 132 | + var token = randomFrom( |
| 133 | + random(), |
| 134 | + () -> randomRealisticUnicodeOfCodepointLength(randomIntBetween(1, 20)), |
| 135 | + () -> UUID.randomUUID().toString(), |
| 136 | + () -> randomIp(randomBoolean()), |
| 137 | + PatternedTextRandomTests::randomTimestamp, |
| 138 | + ESTestCase::randomInt, |
| 139 | + ESTestCase::randomDouble |
| 140 | + ); |
| 141 | + |
| 142 | + if (randomBoolean()) { |
| 143 | + message.append("[").append(token).append("]"); |
| 144 | + } else { |
| 145 | + message.append(token); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return message.toString(); |
| 150 | + } |
| 151 | + |
| 152 | + private static StringBuilder randomSentence() { |
| 153 | + int words = randomIntBetween(1, 10); |
| 154 | + StringBuilder text = new StringBuilder(); |
| 155 | + for (int i = 0; i < words; i++) { |
| 156 | + if (i > 0) { |
| 157 | + text.append(" "); |
| 158 | + } |
| 159 | + text.append(randomAlphaOfLength(randomIntBetween(1, 10))); |
| 160 | + } |
| 161 | + return text; |
| 162 | + } |
| 163 | + |
| 164 | + private static String randomSeparator() { |
| 165 | + if (randomBoolean()) { |
| 166 | + // Return spaces frequently since current token splitting is on spaces. |
| 167 | + return " ".repeat(randomIntBetween(1, 10)); |
| 168 | + } else { |
| 169 | + return randomFrom("\t\n;:.',".split("")); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static String randomTimestamp() { |
| 174 | + long millis = randomMillisUpToYear9999(); |
| 175 | + ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), randomZone()); |
| 176 | + DateFormatter formatter = DateFormatter.forPattern(randomDateFormatterPattern()).withLocale(randomLocale(random())); |
| 177 | + return formatter.format(zonedDateTime); |
| 178 | + } |
| 179 | +} |
0 commit comments