|
| 1 | +/* |
| 2 | + * SonarQube |
| 3 | + * Copyright (C) 2009-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.server.es; |
| 21 | + |
| 22 | +import co.elastic.clients.elasticsearch._types.FieldValue; |
| 23 | +import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery; |
| 24 | +import co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode; |
| 25 | +import co.elastic.clients.elasticsearch._types.query_dsl.Operator; |
| 26 | +import co.elastic.clients.elasticsearch._types.query_dsl.Query; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.List; |
| 29 | +import java.util.function.Consumer; |
| 30 | +import java.util.stream.Stream; |
| 31 | +import javax.annotation.Nullable; |
| 32 | + |
| 33 | +/** |
| 34 | + * Helper class for building ES 8 Query objects. |
| 35 | + * Provides methods similar to ES 7's QueryBuilders. |
| 36 | + */ |
| 37 | +public final class ES8QueryHelper { |
| 38 | + |
| 39 | + private ES8QueryHelper() { |
| 40 | + // Utility class |
| 41 | + } |
| 42 | + |
| 43 | + // ========== MATCH ALL ========== |
| 44 | + |
| 45 | + public static Query matchAllQuery() { |
| 46 | + return Query.of(q -> q.matchAll(m -> m)); |
| 47 | + } |
| 48 | + |
| 49 | + // ========== TERM QUERIES ========== |
| 50 | + |
| 51 | + public static Query termQuery(String field, String value) { |
| 52 | + return Query.of(q -> q.term(t -> t.field(field).value(value))); |
| 53 | + } |
| 54 | + |
| 55 | + public static Query termQuery(String field, long value) { |
| 56 | + return Query.of(q -> q.term(t -> t.field(field).value(value))); |
| 57 | + } |
| 58 | + |
| 59 | + public static Query termQuery(String field, boolean value) { |
| 60 | + return Query.of(q -> q.term(t -> t.field(field).value(value))); |
| 61 | + } |
| 62 | + |
| 63 | + public static Query termsQuery(String field, String... values) { |
| 64 | + List<FieldValue> fieldValues = Stream.of(values) |
| 65 | + .map(FieldValue::of) |
| 66 | + .toList(); |
| 67 | + return Query.of(q -> q.terms(t -> t.field(field).terms(tv -> tv.value(fieldValues)))); |
| 68 | + } |
| 69 | + |
| 70 | + public static Query termsQuery(String field, @Nullable Collection<String> values) { |
| 71 | + if (values == null || values.isEmpty()) { |
| 72 | + return matchAllQuery(); |
| 73 | + } |
| 74 | + List<FieldValue> fieldValues = values.stream() |
| 75 | + .map(FieldValue::of) |
| 76 | + .toList(); |
| 77 | + return Query.of(q -> q.terms(t -> t.field(field).terms(tv -> tv.value(fieldValues)))); |
| 78 | + } |
| 79 | + |
| 80 | + // ========== BOOL QUERIES ========== |
| 81 | + |
| 82 | + public static Query boolQuery() { |
| 83 | + return Query.of(q -> q.bool(b -> b)); |
| 84 | + } |
| 85 | + |
| 86 | + public static Query boolQuery(Consumer<BoolQuery.Builder> fn) { |
| 87 | + return Query.of(q -> q.bool(b -> { |
| 88 | + fn.accept(b); |
| 89 | + return b; |
| 90 | + })); |
| 91 | + } |
| 92 | + |
| 93 | + // ========== MATCH QUERIES ========== |
| 94 | + |
| 95 | + public static Query matchQuery(String field, String text) { |
| 96 | + return Query.of(q -> q.match(m -> m.field(field).query(text))); |
| 97 | + } |
| 98 | + |
| 99 | + public static Query matchQuery(String field, String text, Operator operator) { |
| 100 | + return Query.of(q -> q.match(m -> m.field(field).query(text).operator(operator))); |
| 101 | + } |
| 102 | + |
| 103 | + // ========== RANGE QUERIES ========== |
| 104 | + |
| 105 | + /** |
| 106 | + * ES 8: Creates a range query for a specific field. |
| 107 | + * Note: In ES8, we use the NumberRangeQuery variant for numeric comparisons. |
| 108 | + */ |
| 109 | + public static Query rangeQuery(String field) { |
| 110 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field)))); |
| 111 | + } |
| 112 | + |
| 113 | + public static Query rangeQueryGte(String field, long value) { |
| 114 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).gte((double) value)))); |
| 115 | + } |
| 116 | + |
| 117 | + public static Query rangeQueryLte(String field, long value) { |
| 118 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).lte((double) value)))); |
| 119 | + } |
| 120 | + |
| 121 | + public static Query rangeQueryGt(String field, long value) { |
| 122 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).gt((double) value)))); |
| 123 | + } |
| 124 | + |
| 125 | + public static Query rangeQueryLt(String field, long value) { |
| 126 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).lt((double) value)))); |
| 127 | + } |
| 128 | + |
| 129 | + public static Query rangeQueryBetween(String field, long from, long to) { |
| 130 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).gte((double) from).lte((double) to)))); |
| 131 | + } |
| 132 | + |
| 133 | + public static Query rangeQueryGte(String field, double value) { |
| 134 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).gte(value)))); |
| 135 | + } |
| 136 | + |
| 137 | + public static Query rangeQueryLte(String field, double value) { |
| 138 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).lte(value)))); |
| 139 | + } |
| 140 | + |
| 141 | + public static Query rangeQueryGt(String field, double value) { |
| 142 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).gt(value)))); |
| 143 | + } |
| 144 | + |
| 145 | + public static Query rangeQueryLt(String field, double value) { |
| 146 | + return Query.of(q -> q.range(r -> r.number(n -> n.field(field).lt(value)))); |
| 147 | + } |
| 148 | + |
| 149 | + // ========== EXISTS QUERIES ========== |
| 150 | + |
| 151 | + public static Query existsQuery(String field) { |
| 152 | + return Query.of(q -> q.exists(e -> e.field(field))); |
| 153 | + } |
| 154 | + |
| 155 | + // ========== PREFIX QUERIES ========== |
| 156 | + |
| 157 | + public static Query prefixQuery(String field, String prefix) { |
| 158 | + return Query.of(q -> q.prefix(p -> p.field(field).value(prefix))); |
| 159 | + } |
| 160 | + |
| 161 | + // ========== WILDCARD QUERIES ========== |
| 162 | + |
| 163 | + public static Query wildcardQuery(String field, String value) { |
| 164 | + return Query.of(q -> q.wildcard(w -> w.field(field).value(value))); |
| 165 | + } |
| 166 | + |
| 167 | + // ========== NESTED QUERIES ========== |
| 168 | + |
| 169 | + public static Query nestedQuery(String path, Query query) { |
| 170 | + return Query.of(q -> q.nested(n -> n.path(path).query(query))); |
| 171 | + } |
| 172 | + |
| 173 | + public static Query nestedQuery(String path, Query query, ChildScoreMode scoreMode) { |
| 174 | + return Query.of(q -> q.nested(n -> n.path(path).query(query).scoreMode(scoreMode))); |
| 175 | + } |
| 176 | + |
| 177 | + // ========== HAS CHILD/PARENT QUERIES ========== |
| 178 | + |
| 179 | + public static Query hasChildQuery(String type, Query query) { |
| 180 | + return Query.of(q -> q.hasChild(h -> h.type(type).query(query))); |
| 181 | + } |
| 182 | + |
| 183 | + public static Query hasChildQuery(String type, Query query, ChildScoreMode scoreMode) { |
| 184 | + return Query.of(q -> q.hasChild(h -> h.type(type).query(query).scoreMode(scoreMode))); |
| 185 | + } |
| 186 | + |
| 187 | + public static Query hasParentQuery(String parentType, Query query) { |
| 188 | + return Query.of(q -> q.hasParent(h -> h.parentType(parentType).query(query))); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Wraps a query with a boost |
| 193 | + */ |
| 194 | + public static Query withBoost(Query query, float boost) { |
| 195 | + return Query.of(q -> q.bool(b -> b.must(query).boost(boost))); |
| 196 | + } |
| 197 | +} |
0 commit comments