Skip to content

Commit 6df0461

Browse files
committed
Added range query support
1 parent c283f04 commit 6df0461

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

api/src/main/java/io/kafbat/ui/service/index/PrefixQueryParser.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.apache.lucene.search.BoostAttribute.DEFAULT_BOOST;
44

5+
import io.kafbat.ui.service.index.TopicsIndex.FieldType;
6+
import java.util.List;
57
import java.util.Optional;
68
import org.apache.lucene.analysis.Analyzer;
79
import org.apache.lucene.document.IntPoint;
@@ -12,19 +14,50 @@
1214
import org.apache.lucene.search.PrefixQuery;
1315
import org.apache.lucene.search.Query;
1416
import org.apache.lucene.search.TermQuery;
17+
import org.apache.lucene.search.TermRangeQuery;
1518

1619
public class PrefixQueryParser extends QueryParser {
17-
20+
1821
public PrefixQueryParser(String field, Analyzer analyzer) {
1922
super(field, analyzer);
2023
}
2124

25+
@Override
26+
protected Query newRangeQuery(String field, String part1, String part2, boolean startInclusive,
27+
boolean endInclusive) {
28+
FieldType fieldType = Optional.ofNullable(field)
29+
.map(TopicsIndex.FIELD_TYPES::get)
30+
.orElse(FieldType.STRING);
31+
32+
return switch (fieldType) {
33+
case STRING, BOOLEAN -> super.newRangeQuery(field, part1, part2, startInclusive, endInclusive);
34+
case INT -> IntPoint.newRangeQuery(field, parseInt(part1, true), parseInt(part2, false));
35+
case LONG -> LongPoint.newRangeQuery(field, parseLong(part1, true), parseLong(part2, false));
36+
};
37+
}
38+
39+
private Integer parseInt(String value, boolean min) {
40+
if ("*".equals(value) || value == null) {
41+
return min ? Integer.MIN_VALUE : Integer.MAX_VALUE;
42+
} else {
43+
return Integer.parseInt(value);
44+
}
45+
}
46+
47+
private Long parseLong(String value, boolean min) {
48+
if ("*".equals(value) || value == null) {
49+
return min ? Long.MIN_VALUE : Long.MAX_VALUE;
50+
} else {
51+
return Long.parseLong(value);
52+
}
53+
}
54+
2255
@Override
2356
protected Query newTermQuery(Term term, float boost) {
2457

25-
TopicsIndex.FieldType fieldType = Optional.ofNullable(term.field())
58+
FieldType fieldType = Optional.ofNullable(term.field())
2659
.map(TopicsIndex.FIELD_TYPES::get)
27-
.orElse(TopicsIndex.FieldType.STRING);
60+
.orElse(FieldType.STRING);
2861

2962
Query query = switch (fieldType) {
3063
case STRING -> new PrefixQuery(term);

api/src/test/java/io/kafbat/ui/service/index/LuceneTopicsIndexTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void testFindTopicsByName() throws Exception {
8181
HashMap<String, Integer> indexExamples = new HashMap<>(examples);
8282
indexExamples.put("config_retention:compact", 1);
8383
indexExamples.put("partitions:10", 1);
84+
indexExamples.put("partitions:{1 TO *]", 1);
85+
indexExamples.put("partitions:{* TO 9]", topics.size() - 1);
8486

8587
try (LuceneTopicsIndex index = new LuceneTopicsIndex(topics,
8688
new ClustersProperties.FtsProperties(false, 1, 4))) {

0 commit comments

Comments
 (0)