Skip to content

Commit 5f73e9c

Browse files
committed
Fixes
1 parent df6a364 commit 5f73e9c

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

api/src/main/java/io/kafbat/ui/config/ClustersProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public static class FtsProperties {
232232
@AllArgsConstructor
233233
public static class ClusterFtsProperties {
234234
boolean enabled = false;
235-
FtsProperties topics = new FtsProperties(true, 3, 5);
235+
FtsProperties topics = new FtsProperties(false, 3, 5);
236236
FtsProperties schemas = new FtsProperties(true, 1, 4);
237237
FtsProperties consumers = new FtsProperties(true, 1, 4);
238238
FtsProperties connect = new FtsProperties(true, 1, 4);

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,11 @@ public List<InternalTopic> find(String search, Boolean showInternal,
130130
}
131131
nameQuery = builder.build();
132132
} else {
133-
QueryParser queryParser = new QueryParser(FIELD_NAME, this.analyzer);
133+
QueryParser queryParser = new PrefixQueryParser(FIELD_NAME, this.analyzer);
134134
queryParser.setDefaultOperator(QueryParser.Operator.AND);
135135

136136
try {
137137
nameQuery = queryParser.parse(search);
138-
139-
if (!search.contains(" ") && !search.contains("*")) {
140-
String wildcardSearch = search + "*";
141-
Query wildCardNameQuery = queryParser.parse(wildcardSearch);
142-
BooleanQuery.Builder withWildcard = new BooleanQuery.Builder();
143-
withWildcard.add(nameQuery, BooleanClause.Occur.SHOULD);
144-
withWildcard.add(wildCardNameQuery, BooleanClause.Occur.SHOULD);
145-
nameQuery = withWildcard.build();
146-
}
147-
148-
149138
} catch (Exception e) {
150139
throw new RuntimeException(e);
151140
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.kafbat.ui.service.index;
2+
3+
import static org.apache.lucene.search.BoostAttribute.DEFAULT_BOOST;
4+
5+
import java.util.Optional;
6+
import org.apache.lucene.analysis.Analyzer;
7+
import org.apache.lucene.document.IntPoint;
8+
import org.apache.lucene.document.LongPoint;
9+
import org.apache.lucene.index.Term;
10+
import org.apache.lucene.queryparser.classic.QueryParser;
11+
import org.apache.lucene.search.BoostQuery;
12+
import org.apache.lucene.search.PrefixQuery;
13+
import org.apache.lucene.search.Query;
14+
import org.apache.lucene.search.TermQuery;
15+
16+
public class PrefixQueryParser extends QueryParser {
17+
18+
public PrefixQueryParser(String field, Analyzer analyzer) {
19+
super(field, analyzer);
20+
}
21+
22+
@Override
23+
protected Query newTermQuery(Term term, float boost) {
24+
25+
TopicsIndex.FieldType fieldType = Optional.ofNullable(term.field())
26+
.map(TopicsIndex.FIELD_TYPES::get)
27+
.orElse(TopicsIndex.FieldType.STRING);
28+
29+
Query query = switch (fieldType) {
30+
case STRING -> new PrefixQuery(term);
31+
case INT -> IntPoint.newExactQuery(term.field(), Integer.parseInt(term.text()));
32+
case LONG -> LongPoint.newExactQuery(term.field(), Long.parseLong(term.text()));
33+
case BOOLEAN -> new TermQuery(term);
34+
};
35+
36+
if (boost == DEFAULT_BOOST) {
37+
return query;
38+
}
39+
return new BoostQuery(query, boost);
40+
}
41+
42+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.kafbat.ui.model.InternalTopic;
44
import java.util.List;
5+
import java.util.Map;
56

67
public interface TopicsIndex extends AutoCloseable {
78
String FIELD_NAME = "name";
@@ -11,6 +12,22 @@ public interface TopicsIndex extends AutoCloseable {
1112
String FIELD_SIZE = "size";
1213
String FIELD_CONFIG_PREFIX = "config";
1314

15+
enum FieldType {
16+
STRING,
17+
INT,
18+
LONG,
19+
BOOLEAN
20+
}
21+
22+
Map<String, FieldType> FIELD_TYPES = Map.of(
23+
FIELD_NAME, FieldType.STRING,
24+
FIELD_INTERNAL, FieldType.BOOLEAN,
25+
FIELD_PARTITIONS, FieldType.INT,
26+
FIELD_REPLICATION, FieldType.INT,
27+
FIELD_SIZE, FieldType.LONG,
28+
FIELD_CONFIG_PREFIX, FieldType.STRING
29+
);
30+
1431
default List<InternalTopic> find(String search, Boolean showInternal, Integer count) {
1532
return this.find(search, showInternal, FIELD_NAME, count);
1633
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package io.kafbat.ui.service.index;
22

33
import io.kafbat.ui.config.ClustersProperties;
4+
import io.kafbat.ui.model.InternalPartition;
45
import io.kafbat.ui.model.InternalTopic;
56
import io.kafbat.ui.model.InternalTopicConfig;
67
import java.util.ArrayList;
78
import java.util.HashMap;
89
import java.util.List;
910
import java.util.Map;
11+
import java.util.function.Function;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.IntStream;
1014
import java.util.stream.Stream;
1115
import org.assertj.core.api.SoftAssertions;
1216
import org.junit.jupiter.api.Test;
1317

14-
class TopicsIndexTest {
18+
class LuceneTopicsIndexTest {
1519
@Test
1620
void testFindTopicsByName() throws Exception {
1721
List<InternalTopic> topics = new ArrayList<>(
@@ -36,6 +40,14 @@ void testFindTopicsByName() throws Exception {
3640
List.of(
3741
InternalTopic.builder().name("configurable").partitions(Map.of()).topicConfigs(
3842
List.of(InternalTopicConfig.builder().name("retention").value("compact").build())
43+
).build(),
44+
InternalTopic.builder().name("multiple_parts").partitionCount(10).partitions(
45+
IntStream.range(0, 10).mapToObj(i ->
46+
InternalPartition.builder().partition(i).build()
47+
).collect(Collectors.toMap(
48+
InternalPartition::getPartition,
49+
Function.identity()
50+
))
3951
).build()
4052
)
4153
);
@@ -68,6 +80,7 @@ void testFindTopicsByName() throws Exception {
6880

6981
HashMap<String, Integer> indexExamples = new HashMap<>(examples);
7082
indexExamples.put("config_retention:compact", 1);
83+
indexExamples.put("partitions:10", 1);
7184

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

0 commit comments

Comments
 (0)