Skip to content

Commit 10d30f3

Browse files
committed
Switched topic index to lucene prefixed by default
1 parent 6abf7b0 commit 10d30f3

File tree

9 files changed

+29
-75
lines changed

9 files changed

+29
-75
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ public static class CacheProperties {
221221
@Data
222222
@NoArgsConstructor
223223
@AllArgsConstructor
224-
public static class FtsProperties {
225-
boolean ngram = true;
224+
public static class NgramProperties {
226225
int ngramMin = 1;
227226
int ngramMax = 4;
228227
}
@@ -232,11 +231,10 @@ public static class FtsProperties {
232231
@AllArgsConstructor
233232
public static class ClusterFtsProperties {
234233
boolean enabled = false;
235-
FtsProperties topics = new FtsProperties(false, 3, 5);
236-
FtsProperties schemas = new FtsProperties(true, 1, 4);
237-
FtsProperties consumers = new FtsProperties(true, 1, 4);
238-
FtsProperties connect = new FtsProperties(true, 1, 4);
239-
FtsProperties acl = new FtsProperties(true, 1, 4);
234+
NgramProperties schemas = new NgramProperties(1, 4);
235+
NgramProperties consumers = new NgramProperties(1, 4);
236+
NgramProperties connect = new NgramProperties(1, 4);
237+
NgramProperties acl = new NgramProperties(1, 4);
240238
}
241239

242240
@PostConstruct

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public class AclBindingNgramFilter extends NgramFilter<AclBinding> {
1111
private final List<Tuple2<List<String>, AclBinding>> bindings;
1212

1313
public AclBindingNgramFilter(Collection<AclBinding> bindings) {
14-
this(bindings, true, new ClustersProperties.FtsProperties(true, 1, 4));
14+
this(bindings, true, new ClustersProperties.NgramProperties(1, 4));
1515
}
1616

1717
public AclBindingNgramFilter(
1818
Collection<AclBinding> bindings,
1919
boolean enabled,
20-
ClustersProperties.FtsProperties properties) {
20+
ClustersProperties.NgramProperties properties) {
2121
super(properties, enabled);
2222
this.bindings = bindings.stream().map(g -> Tuples.of(List.of(g.entry().principal()), g)).toList();
2323
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public class ConsumerGroupFilter extends NgramFilter<ConsumerGroupListing> {
1111
private final List<Tuple2<List<String>, ConsumerGroupListing>> groups;
1212

1313
public ConsumerGroupFilter(Collection<ConsumerGroupListing> groups) {
14-
this(groups, true, new ClustersProperties.FtsProperties(true, 1, 4));
14+
this(groups, true, new ClustersProperties.NgramProperties(1, 4));
1515
}
1616

1717
public ConsumerGroupFilter(
1818
Collection<ConsumerGroupListing> groups,
1919
boolean enabled,
20-
ClustersProperties.FtsProperties properties) {
20+
ClustersProperties.NgramProperties properties) {
2121
super(properties, enabled);
2222
this.groups = groups.stream().map(g -> Tuples.of(List.of(g.groupId()), g)).toList();
2323
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public class KafkaConnectNgramFilter extends NgramFilter<FullConnectorInfoDTO> {
1111
private final List<Tuple2<List<String>, FullConnectorInfoDTO>> connectors;
1212

1313
public KafkaConnectNgramFilter(Collection<FullConnectorInfoDTO> connectors) {
14-
this(connectors, true, new ClustersProperties.FtsProperties(true, 1, 4));
14+
this(connectors, true, new ClustersProperties.NgramProperties(1, 4));
1515
}
1616

1717
public KafkaConnectNgramFilter(
1818
Collection<FullConnectorInfoDTO> connectors,
1919
boolean enabled,
20-
ClustersProperties.FtsProperties properties) {
20+
ClustersProperties.NgramProperties properties) {
2121
super(properties, enabled);
2222
this.connectors = connectors.stream().map(this::getItem).toList();
2323
}

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

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.lucene.index.IndexWriter;
2525
import org.apache.lucene.index.IndexWriterConfig;
2626
import org.apache.lucene.index.Term;
27+
import org.apache.lucene.queryparser.classic.ParseException;
2728
import org.apache.lucene.queryparser.classic.QueryParser;
2829
import org.apache.lucene.search.BooleanClause;
2930
import org.apache.lucene.search.BooleanQuery;
@@ -46,26 +47,10 @@ public class LuceneTopicsIndex implements TopicsIndex {
4647
private final Analyzer analyzer;
4748
private final int maxSize;
4849
private final ReadWriteLock closeLock = new ReentrantReadWriteLock();
49-
private final boolean searchNgram;
5050
private final Map<String, InternalTopic> topicMap;
5151

5252
public LuceneTopicsIndex(List<InternalTopic> topics) throws IOException {
53-
this(topics, new ClustersProperties.FtsProperties(true, 1, 4));
54-
}
55-
56-
public LuceneTopicsIndex(List<InternalTopic> topics, ClustersProperties.FtsProperties properties) throws IOException {
57-
boolean ngram = properties.isNgram();
58-
if (ngram) {
59-
this.analyzer = new ShortWordNGramAnalyzer(
60-
properties.getNgramMin(),
61-
properties.getNgramMax(),
62-
false
63-
);
64-
} else {
65-
this.analyzer = new ShortWordAnalyzer();
66-
}
67-
68-
this.searchNgram = ngram;
53+
this.analyzer = new ShortWordAnalyzer();
6954
this.topicMap = topics.stream().collect(Collectors.toMap(InternalTopic::getName, Function.identity()));
7055
this.directory = build(topics);
7156
this.indexReader = DirectoryReader.open(directory);
@@ -124,24 +109,10 @@ public List<InternalTopic> find(String search, Boolean showInternal,
124109
}
125110
closeLock.readLock().lock();
126111
try {
127-
Query nameQuery;
128-
if (this.searchNgram) {
129-
List<String> ngrams = NgramFilter.tokenizeStringSimple(this.analyzer, search);
130-
BooleanQuery.Builder builder = new BooleanQuery.Builder();
131-
for (String ng : ngrams) {
132-
builder.add(new TermQuery(new Term(FIELD_NAME, ng)), BooleanClause.Occur.MUST);
133-
}
134-
nameQuery = builder.build();
135-
} else {
136-
QueryParser queryParser = new PrefixQueryParser(FIELD_NAME, this.analyzer);
137-
queryParser.setDefaultOperator(QueryParser.Operator.AND);
138112

139-
try {
140-
nameQuery = queryParser.parse(search);
141-
} catch (Exception e) {
142-
throw new RuntimeException(e);
143-
}
144-
}
113+
QueryParser queryParser = new PrefixQueryParser(FIELD_NAME, this.analyzer);
114+
queryParser.setDefaultOperator(QueryParser.Operator.AND);
115+
Query nameQuery = queryParser.parse(search);;
145116

146117
Query internalFilter = new TermQuery(new Term(FIELD_INTERNAL, "true"));
147118

@@ -172,6 +143,8 @@ public List<InternalTopic> find(String search, Boolean showInternal,
172143
return topics.stream().map(topicMap::get).filter(Objects::nonNull).toList();
173144
} catch (IOException e) {
174145
throw new UncheckedIOException(e);
146+
} catch (ParseException e) {
147+
throw new RuntimeException(e);
175148
} finally {
176149
this.closeLock.readLock().unlock();
177150
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class NgramFilter<T> {
2222
private final Analyzer analyzer;
2323
private final boolean enabled;
2424

25-
public NgramFilter(ClustersProperties.FtsProperties properties, boolean enabled) {
25+
public NgramFilter(ClustersProperties.NgramProperties properties, boolean enabled) {
2626
this.enabled = enabled;
2727
this.analyzer = new ShortWordNGramAnalyzer(properties.getNgramMin(), properties.getNgramMax(), false);
2828
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.kafbat.ui.service.index;
22

3-
import static org.apache.commons.lang3.Strings.CI;
4-
53
import io.kafbat.ui.config.ClustersProperties;
64
import java.util.Collection;
75
import java.util.List;
@@ -11,7 +9,7 @@
119
public class SchemasFilter extends NgramFilter<String> {
1210
private final List<Tuple2<List<String>, String>> subjects;
1311

14-
public SchemasFilter(Collection<String> subjects, boolean enabled, ClustersProperties.FtsProperties properties) {
12+
public SchemasFilter(Collection<String> subjects, boolean enabled, ClustersProperties.NgramProperties properties) {
1513
super(properties, enabled);
1614
this.subjects = subjects.stream().map(g -> Tuples.of(List.of(g), g)).toList();
1715
}

api/src/main/java/io/kafbat/ui/service/metrics/scrape/ScrapedClusterState.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,10 @@ public ScrapedClusterState updateTopics(Map<String, TopicDescription> descriptio
110110
);
111111
});
112112

113-
ScrapedClusterState state = toBuilder()
113+
return toBuilder()
114114
.topicStates(updatedTopicStates)
115115
.topicIndex(buildTopicIndex(clustersProperties, updatedTopicStates))
116116
.build();
117-
return state;
118117
}
119118

120119
public ScrapedClusterState topicDeleted(String topic) {
@@ -219,14 +218,12 @@ private static TopicsIndex buildTopicIndex(ClustersProperties clustersProperties
219218

220219
if (fts.isEnabled()) {
221220
try {
222-
return new LuceneTopicsIndex(topics, fts.getTopics());
221+
return new LuceneTopicsIndex(topics);
223222
} catch (Exception e) {
224-
log.error("Error creating topics index", e);
223+
log.error("Error creating lucene topics index", e);
225224
}
226-
} else {
227-
return new FilterTopicIndex(topics);
228225
}
229-
return null;
226+
return new FilterTopicIndex(topics);
230227
}
231228

232229
private static <T> Map<Integer, T> filterTopic(String topicForFilter, Map<TopicPartition, T> tpMap) {

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ void testFindTopicsByName() throws Exception {
6565
Map.entry("stat", 3),
6666
Map.entry("changes", 1),
6767
Map.entry("commands", 1),
68-
Map.entry("id", 2)
68+
Map.entry("id", 2),
69+
Map.entry("config_retention:compact", 1),
70+
Map.entry("partitions:10", 1),
71+
Map.entry("partitions:{1 TO *]", 1),
72+
Map.entry("partitions:{* TO 9]", topics.size() - 1)
6973
);
7074

7175
SoftAssertions softly = new SoftAssertions();
@@ -77,22 +81,6 @@ void testFindTopicsByName() throws Exception {
7781
.isEqualTo(entry.getValue());
7882
}
7983
}
80-
81-
HashMap<String, Integer> indexExamples = new HashMap<>(examples);
82-
indexExamples.put("config_retention:compact", 1);
83-
indexExamples.put("partitions:10", 1);
84-
indexExamples.put("partitions:{1 TO *]", 1);
85-
indexExamples.put("partitions:{* TO 9]", topics.size() - 1);
86-
87-
try (LuceneTopicsIndex index = new LuceneTopicsIndex(topics,
88-
new ClustersProperties.FtsProperties(false, 1, 4))) {
89-
for (Map.Entry<String, Integer> entry : indexExamples.entrySet()) {
90-
List<InternalTopic> resultAll = index.find(entry.getKey(), null, topics.size());
91-
softly.assertThat(resultAll.size())
92-
.withFailMessage("Expected %d results for '%s', but got %s", entry.getValue(), entry.getKey(), resultAll)
93-
.isEqualTo(entry.getValue());
94-
}
95-
}
9684
softly.assertAll();
9785
}
9886

0 commit comments

Comments
 (0)