Created
November 8, 2021 15:43
-
-
Save Hkazanci93/ebea4d28dd7cc4b16d42a65fcbd5adb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LogSearch { | |
@Builder.Default | |
private List<LogubFieldSearch> texts = emptyList(); | |
@Builder.Default | |
private List<LogubFieldSearch> systemProperties = emptyList(); | |
@Builder.Default | |
private List<LogubFieldSearch> businessProperties = emptyList(); | |
@Builder.Default | |
private List<LogubFieldSearch> basicProperties = emptyList(); | |
@Builder.Default | |
private List<LogubFieldSearch> levels = Collections.emptyList(); | |
@Builder.Default | |
private int limit = 25; | |
@Builder.Default | |
private int offset = 0; | |
@Builder.Default | |
private Optional<LogubSort> sort = Optional.empty(); | |
@Builder.Default | |
private Instant beginAt = Instant.now().minus(15, ChronoUnit.MINUTES); | |
@Builder.Default | |
private Instant endAt = Instant.now(); | |
@SneakyThrows | |
public QueryBuilder toQuery() { | |
var query = new QueryBuilder(); | |
var businessPrefix = "businessProperties."; | |
var systemPropertiesPrefix = "systemProperties."; | |
for (LogubFieldSearch properties : businessProperties) { | |
query.append(QueryBuilders.tag(businessPrefix + properties.getName(), properties.getValues(), | |
properties.isNegation())); | |
} | |
for (LogubFieldSearch properties : systemProperties) { | |
query.append(QueryBuilders | |
.tag(systemPropertiesPrefix + properties.getName(), properties.getValues(), | |
properties.isNegation())); | |
} | |
for (LogubFieldSearch properties : basicProperties) { | |
query.append(QueryBuilders | |
.tag(properties.getName(), properties.getValues(), | |
properties.isNegation())); | |
} | |
if (!levels.isEmpty()) { | |
for (LogubFieldSearch level : levels) { | |
var onError = !level.getValues().stream().allMatch(v -> Arrays.stream(LogLevel.values()) | |
.anyMatch(enumLevel -> enumLevel.name().equalsIgnoreCase(v))); | |
if(onError){ | |
log.error("bad payload for levels {}", level); | |
throw new IllegalArgumentException("bad payload for level"); | |
} | |
query.append(QueryBuilders.tag("level",level.getValues(), level.isNegation())); | |
} | |
} | |
for (LogubFieldSearch text : texts) { | |
if(!text.getType().equals(LogubFieldType.FullText)){ | |
log.warn("type {} not handle for text search", text.getType()); | |
} | |
for (String value : text.getValues()) { | |
query.append(QueryBuilders.text("message", value, text.isNegation())); | |
} | |
} | |
return query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment