From 3934e613f0c9b7a13ff18d35b47780ce2983d464 Mon Sep 17 00:00:00 2001 From: "igor.polyakov" Date: Tue, 10 Nov 2026 16:42:39 +0300 Subject: [PATCH] #2765 indexing condition support added --- README.md | 29 +++++++++ .../rt/restream/reindexer/ReindexerIndex.java | 19 ++++++ .../binding/definition/IndexDefinition.java | 12 ++++ .../connector/IndexOperationTest.java | 59 +++++++++++++++++-- 4 files changed, 114 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3993fb65..64ef3e10 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,35 @@ db.query("items", Item.class) Full text search usage documentation and examples are [here](https://github.com/Restream/reindexer/blob/master/fulltext.md). +#### Conditional indexing + +The optional `indexingCondition` selects which documents are included in a text +index. It uses a SQL `WHERE` expression without the `WHERE` keyword; excluded +documents remain in the namespace. + +```java +ReindexerIndex titleIndex = ReindexerIndex.builder() + .name("title") + .jsonPaths(Collections.singletonList("title")) + .indexType(IndexType.TEXT) + .fieldType(FieldType.STRING) + .collateMode(CollateMode.NONE) + .indexingCondition("type = 'series'") + .build(); +db.addIndex("items", titleIndex); + +titleIndex.setIndexingCondition("type = 'movie'"); +db.updateIndex("items", titleIndex); + +// Remove the condition and index all documents with text values again. +titleIndex.setIndexingCondition(null); +db.updateIndex("items", titleIndex); +``` + +Changing or removing the condition rebuilds the index for existing documents. +An empty string also removes the condition. Pass the complete index definition +to `updateIndex`, preserving its other settings. + ### Joins Reindexer can join documents from multiple namespaces into a single result: diff --git a/src/main/java/ru/rt/restream/reindexer/ReindexerIndex.java b/src/main/java/ru/rt/restream/reindexer/ReindexerIndex.java index d53ca95f..831577f4 100644 --- a/src/main/java/ru/rt/restream/reindexer/ReindexerIndex.java +++ b/src/main/java/ru/rt/restream/reindexer/ReindexerIndex.java @@ -107,6 +107,25 @@ public class ReindexerIndex { private boolean isAppendable; + /** + * SQL WHERE expression without the WHERE keyword, used to select documents for a text index. + * Null or an empty string removes the condition when updating the index. + */ + private String indexingCondition; + + /** + * Constructs an index without an indexing condition. + */ + public ReindexerIndex(String name, List jsonPaths, IndexType indexType, FieldType fieldType, + CollateMode collateMode, String sortOrder, FullTextConfig fullTextConfig, + HnswConfig hnswConfig, IvfConfig ivfConfig, VecBfConfig vecBfConfig, + ReindexerPrecept precept, boolean isArray, boolean isPk, boolean isDense, + boolean isNoColumn, boolean isSparse, boolean isUuid, boolean isAppendable) { + this(name, jsonPaths, indexType, fieldType, collateMode, sortOrder, fullTextConfig, hnswConfig, + ivfConfig, vecBfConfig, precept, isArray, isPk, isDense, isNoColumn, isSparse, isUuid, + isAppendable, null); + } + /** * Set full text search config for current index, if the index is text index. * diff --git a/src/main/java/ru/rt/restream/reindexer/binding/definition/IndexDefinition.java b/src/main/java/ru/rt/restream/reindexer/binding/definition/IndexDefinition.java index 5a3c7b22..e9418a75 100644 --- a/src/main/java/ru/rt/restream/reindexer/binding/definition/IndexDefinition.java +++ b/src/main/java/ru/rt/restream/reindexer/binding/definition/IndexDefinition.java @@ -58,6 +58,17 @@ public class IndexDefinition { private boolean isNoColumn; + private String indexingCondition; + + /** + * Sets the indexing condition. Empty conditions are omitted from the serialized definition. + * + * @param indexingCondition SQL WHERE expression without the WHERE keyword, or null to remove it + */ + public void setIndexingCondition(String indexingCondition) { + this.indexingCondition = "".equals(indexingCondition) ? null : indexingCondition; + } + /** * Construct a new IndexDefinition from a {@link ReindexerIndex} object. * @@ -81,6 +92,7 @@ public static IndexDefinition fromIndex(ReindexerIndex index) { indexDefinition.setAppendable(index.isAppendable()); indexDefinition.setConfig(index.getConfig()); indexDefinition.setUuid(index.isUuid()); + indexDefinition.setIndexingCondition(index.getIndexingCondition()); return indexDefinition; } } diff --git a/src/test/java/ru/rt/restream/reindexer/connector/IndexOperationTest.java b/src/test/java/ru/rt/restream/reindexer/connector/IndexOperationTest.java index 74d8c398..3ec6bac5 100644 --- a/src/test/java/ru/rt/restream/reindexer/connector/IndexOperationTest.java +++ b/src/test/java/ru/rt/restream/reindexer/connector/IndexOperationTest.java @@ -24,6 +24,7 @@ import ru.rt.restream.reindexer.Namespace; import ru.rt.restream.reindexer.NamespaceOptions; import ru.rt.restream.reindexer.ReindexerIndex; +import ru.rt.restream.reindexer.ResultIterator; import ru.rt.restream.reindexer.annotations.Json; import ru.rt.restream.reindexer.annotations.Reindex; import ru.rt.restream.reindexer.db.DbBaseTest; @@ -39,6 +40,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.hamcrest.Matchers.is; +import static ru.rt.restream.reindexer.Query.Condition.EQ; /** * Base index operation Test. @@ -115,6 +117,50 @@ public void testUpdateIndex() { assertThat(afterUpdated.get(0).getFieldType(), equalToIgnoringCase("string")); } + @Test + public void testIndexingCondition() { + String namespaceName = "conditional_fulltext_items"; + Namespace namespace = db.openNamespace(namespaceName, NamespaceOptions.defaultOptions(), Item.class); + ReindexerIndex index = ReindexerIndex.builder() + .name("description2") + .jsonPaths(Collections.singletonList("description2")) + .indexType(IndexType.TEXT) + .fieldType(FieldType.STRING) + .collateMode(CollateMode.NONE) + .indexingCondition("id = 1") + .build(); + db.addIndex(namespaceName, index); + for (int id = 1; id <= 2; id++) { + Item item = new Item(); + item.setId(id); + item.setDescription1("description"); + item.setDescription2("token"); + namespace.insert(item); + } + assertIndexingCondition(namespaceName, namespace, "id = 1", 1); + + for (String emptyCondition : Arrays.asList("", null)) { + index.setIndexingCondition("id = 2"); + db.updateIndex(namespaceName, index); + assertIndexingCondition(namespaceName, namespace, "id = 2", 2); + + index.setIndexingCondition(emptyCondition); + db.updateIndex(namespaceName, index); + assertIndexingCondition(namespaceName, namespace, null, 1, 2); + } + } + + private void assertIndexingCondition(String namespaceName, Namespace namespace, String condition, + Integer... expectedIds) { + IndexResponse index = indexNamed(getNameSpaceIndexes(namespaceName), "description2"); + assertThat(index.getIndexingCondition(), is(condition)); + List ids = new ArrayList<>(); + for (Item item : namespace.query().where("description2", EQ, "token").sort("id", false).toList()) { + ids.add(item.getId()); + } + assertThat(ids, is(Arrays.asList(expectedIds))); + } + @Test public void testCollateMode() { String nameSpaceName = "collate_mode_items"; @@ -198,11 +244,12 @@ private void createNamespaceWithRecords(String nameSpaceName, int recordsCnt) { private List getNameSpaceIndexes(String nameSpaceName) { Namespace serviceNamespace = db.openNamespace("#namespaces", NamespaceOptions.defaultOptions(), NamespaceResponse.class); String query = String.format("select * from #namespaces where name = '%s'", nameSpaceName); - Iterator iterator = serviceNamespace.execSql(query); - if (iterator.hasNext()) { - List indexes = iterator.next().getIndexes(); - indexes.sort(Comparator.comparing(IndexResponse::getName)); - return indexes; + try (ResultIterator iterator = serviceNamespace.execSql(query)) { + if (iterator.hasNext()) { + List indexes = iterator.next().getIndexes(); + indexes.sort(Comparator.comparing(IndexResponse::getName)); + return indexes; + } } return Collections.emptyList(); } @@ -276,5 +323,7 @@ public static class IndexResponse { private String collateMode; @Json("sort_order_letters") private String sortOrderLetters; + @Json("indexing_condition") + private String indexingCondition; } }