|
| 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.client.Request; |
| 11 | +import org.elasticsearch.client.Response; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.common.time.DateFormatter; |
| 14 | +import org.elasticsearch.common.time.FormatNames; |
| 15 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 16 | +import org.elasticsearch.test.cluster.local.distribution.DistributionType; |
| 17 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 18 | +import org.elasticsearch.test.rest.ObjectPath; |
| 19 | +import org.hamcrest.Matchers; |
| 20 | +import org.junit.ClassRule; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.time.Instant; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.hamcrest.Matchers.equalTo; |
| 30 | +import static org.hamcrest.Matchers.is; |
| 31 | + |
| 32 | +public class PatternedTextBasicRestIT extends ESRestTestCase { |
| 33 | + |
| 34 | + @ClassRule |
| 35 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 36 | + .distribution(DistributionType.DEFAULT) |
| 37 | + .setting("xpack.license.self_generated.type", "trial") |
| 38 | + .setting("xpack.security.enabled", "false") |
| 39 | + .build(); |
| 40 | + |
| 41 | + @Override |
| 42 | + protected String getTestRestCluster() { |
| 43 | + return cluster.getHttpAddresses(); |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + public void testBulkInsertThenMatchAllSource() throws IOException { |
| 48 | + |
| 49 | + Settings.Builder settings = Settings.builder(); |
| 50 | + if (randomBoolean()) { |
| 51 | + settings.put("index.mode", "logsdb"); |
| 52 | + } |
| 53 | + if (randomBoolean()) { |
| 54 | + settings.put("index.mapping.source.mode", "synthetic"); |
| 55 | + } |
| 56 | + |
| 57 | + String mapping = """ |
| 58 | + { |
| 59 | + "properties": { |
| 60 | + "@timestamp": { |
| 61 | + "type": "date" |
| 62 | + }, |
| 63 | + "message": { |
| 64 | + "type": "patterned_text" |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + """; |
| 69 | + |
| 70 | + String indexName = "test-index"; |
| 71 | + createIndex(indexName, settings.build(), mapping); |
| 72 | + |
| 73 | + int numDocs = randomIntBetween(1, 100); |
| 74 | + List<String> messages = randomMessages(numDocs); |
| 75 | + indexDocs(indexName, messages); |
| 76 | + |
| 77 | + var actualMapping = getIndexMappingAsMap(indexName); |
| 78 | + assertThat("patterned_text", equalTo(ObjectPath.evaluate(actualMapping, "properties.message.type"))); |
| 79 | + |
| 80 | + Request searchRequest = new Request("GET", "/" + indexName + "/_search"); |
| 81 | + searchRequest.setJsonEntity(""" |
| 82 | + { |
| 83 | + "query" : { "match_all" : {} }, |
| 84 | + "size": 100 |
| 85 | + } |
| 86 | + """); |
| 87 | + Response getResponse = client().performRequest(searchRequest); |
| 88 | + assertThat(getResponse.getStatusLine().getStatusCode(), is(200)); |
| 89 | + ObjectPath objectPath = ObjectPath.createFromResponse(getResponse); |
| 90 | + |
| 91 | + assertThat(objectPath.evaluate("hits.total.value"), equalTo(messages.size())); |
| 92 | + var hits = (ArrayList<Map<String, Object>>) objectPath.evaluate("hits.hits"); |
| 93 | + var values = new HashSet<>(hits.stream().map(o -> { |
| 94 | + var source = (Map<String, Object>) o.get("_source"); |
| 95 | + return (String) source.get("message"); |
| 96 | + }).toList()); |
| 97 | + |
| 98 | + assertEquals(new HashSet<>(messages), values); |
| 99 | + } |
| 100 | + |
| 101 | + private void indexDocs(String indexName, List<String> messages) throws IOException { |
| 102 | + var now = Instant.now(); |
| 103 | + StringBuilder sb = new StringBuilder(); |
| 104 | + for (int i = 0; i < messages.size(); i++) { |
| 105 | + sb.append("{ \"create\": {} }").append('\n'); |
| 106 | + if (messages.get(i) == null) { |
| 107 | + sb.append(""" |
| 108 | + {"@timestamp": "$now"} |
| 109 | + """.replace("$now", formatInstant(now))); |
| 110 | + } else { |
| 111 | + sb.append(""" |
| 112 | + {"@timestamp": "$now", "message": "$msg"} |
| 113 | + """.replace("$now", formatInstant(now)).replace("$msg", messages.get(i))); |
| 114 | + } |
| 115 | + sb.append('\n'); |
| 116 | + now = now.plusSeconds(1); |
| 117 | + } |
| 118 | + |
| 119 | + var bulkRequest = new Request("POST", "/" + indexName + "/_bulk"); |
| 120 | + bulkRequest.setJsonEntity(sb.toString()); |
| 121 | + bulkRequest.addParameter("refresh", "true"); |
| 122 | + var bulkResponse = client().performRequest(bulkRequest); |
| 123 | + var bulkResponseBody = responseAsMap(bulkResponse); |
| 124 | + assertThat(bulkResponseBody, Matchers.hasEntry("errors", false)); |
| 125 | + } |
| 126 | + |
| 127 | + static String formatInstant(Instant instant) { |
| 128 | + return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant); |
| 129 | + } |
| 130 | + |
| 131 | + public static List<String> randomMessages(int numDocs) { |
| 132 | + List<String> messages = new ArrayList<>(); |
| 133 | + for (int i = 0; i < numDocs; i++) { |
| 134 | + String message = randomFrom(random(), () -> null, () -> randomMessage(10), () -> randomMessage(8 * 1024)); |
| 135 | + messages.add(message); |
| 136 | + } |
| 137 | + return messages; |
| 138 | + } |
| 139 | + |
| 140 | + static String randomMessage(int minLength) { |
| 141 | + StringBuilder sb = new StringBuilder(); |
| 142 | + while (sb.length() < minLength) { |
| 143 | + var token = randomBoolean() ? randomAlphaOfLength(randomIntBetween(1, 10)) : randomInt(); |
| 144 | + sb.append(token).append(" "); |
| 145 | + } |
| 146 | + return sb.toString(); |
| 147 | + } |
| 148 | +} |
0 commit comments