Skip to content

Commit

Permalink
refactor ES query api
Browse files Browse the repository at this point in the history
Signed-off-by: neo <[email protected]>
  • Loading branch information
neowu committed Sep 26, 2024
1 parent 3176d2a commit d97ff1a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Change log

### 9.1.3 (9/26/2024 - )

### 9.1.2 (8/9/2024 - 9/26/2024)

* search: loading from json into search request
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply(plugin = "project")

subprojects {
group = "core.framework"
version = "9.1.2"
version = "9.1.3"
}

val elasticVersion = "8.15.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.query_dsl.DateRangeQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.NumberRangeQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.TermsQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;

import java.time.LocalDate;
import java.time.ZonedDateTime;
Expand All @@ -20,43 +16,47 @@
*/
public class Queries {
public static Query ids(List<String> ids) {
return new Query(new IdsQuery.Builder().values(ids).build());
return QueryBuilders.ids().values(ids).build()._toQuery();
}

public static Query match(String field, String value) {
return new Query(new MatchQuery.Builder().field(field).query(FieldValue.of(value)).build());
return QueryBuilders.match().field(field).query(value).build()._toQuery();
}

public static Query matchPhase(String field, String query) {
return QueryBuilders.matchPhrase().field(field).query(query).build()._toQuery();
}

public static Query range(String field, ZonedDateTime from, ZonedDateTime to) {
var range = new DateRangeQuery.Builder().field(field);
if (from != null) range.gte(from.format(DateTimeFormatter.ISO_INSTANT));
if (to != null) range.lte(to.format(DateTimeFormatter.ISO_INSTANT));
return new Query(new RangeQuery(range.build()));
return QueryBuilders.range().date(range.build()).build()._toQuery();
}

public static Query range(String field, LocalDate from, LocalDate to) {
var range = new DateRangeQuery.Builder().field(field);
if (from != null) range.gte(from.format(DateTimeFormatter.ISO_LOCAL_DATE));
if (to != null) range.lte(to.format(DateTimeFormatter.ISO_LOCAL_DATE));
return new Query(new RangeQuery(range.build()));
return QueryBuilders.range().date(range.build()).build()._toQuery();
}

public static Query range(String field, Number from, Number to) {
var range = new NumberRangeQuery.Builder().field(field);
if (from != null) range.gte(from.doubleValue());
if (to != null) range.lte(to.doubleValue());
return new Query(new RangeQuery(range.build()));
return QueryBuilders.range().number(range.build()).build()._toQuery();
}

public static Query terms(String field, List<String> values) {
return new Query(new TermsQuery.Builder().field(field).terms(t -> t.value(values.stream().map(FieldValue::of).toList())).build());
return QueryBuilders.terms().field(field).terms(t -> t.value(values.stream().map(FieldValue::of).toList())).build()._toQuery();
}

public static Query term(String field, String value) {
return new Query(new TermQuery.Builder().field(field).value(FieldValue.of(value)).build());
return QueryBuilders.term().field(field).value(FieldValue.of(value)).build()._toQuery();
}

public static Query term(String field, boolean value) {
return new Query(new TermQuery.Builder().field(field).value(FieldValue.of(value)).build());
return QueryBuilders.term().field(field).value(FieldValue.of(value)).build()._toQuery();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ void ids() {
assertThat(query.isIds()).isTrue();
assertThat(query.ids().values()).isEqualTo(List.of("id1", "id2"));
}

@Test
void matchPhrase() {
Query query = Queries.matchPhase("field", "query");
assertThat(query.isMatchPhrase()).isTrue();
assertThat(query.matchPhrase().field()).isEqualTo("field");
assertThat(query.matchPhrase().query()).isEqualTo("query");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core.diagram.service;

import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import core.framework.inject.Inject;
Expand All @@ -28,7 +27,7 @@ public class DiagramService {
public String arch(int hours, List<String> includeApps, List<String> excludeApps) {
var request = new SearchRequest();
request.index = "action-*";
request.query = new Query.Builder().bool(b -> filterActions(hours, includeApps, excludeApps)).build();
request.query = query(hours, includeApps, excludeApps);
request.limit = 0;
request.aggregations.put("app", Aggregation.of(a -> a.terms(t -> t.field("app").size(100))
.aggregations("action", sub1 -> sub1.terms(t -> t.field("action").size(500))
Expand All @@ -40,7 +39,7 @@ public String arch(int hours, List<String> includeApps, List<String> excludeApps
return diagram.dot();
}

private BoolQuery.Builder filterActions(int hours, List<String> includeApps, List<String> excludeApps) {
private Query query(int hours, List<String> includeApps, List<String> excludeApps) {
var query = QueryBuilders.bool().must(range("@timestamp", ZonedDateTime.now().minusHours(hours), null));
if (!includeApps.isEmpty()) {
query.must(b -> b.bool(q -> q.should(terms("app", includeApps))
Expand All @@ -49,7 +48,7 @@ private BoolQuery.Builder filterActions(int hours, List<String> includeApps, Lis
query.mustNot(terms("app", excludeApps))
.mustNot(terms("client", excludeApps));
}
return query;
return query.build()._toQuery();
}

public String action(String actionId) {
Expand Down

0 comments on commit d97ff1a

Please sign in to comment.