Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/ru/rt/restream/reindexer/ReindexerIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -115,6 +117,50 @@ public void testUpdateIndex() {
assertThat(afterUpdated.get(0).getFieldType(), equalToIgnoringCase("string"));
}

@Test
public void testIndexingCondition() {
String namespaceName = "conditional_fulltext_items";
Namespace<Item> 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<Item> namespace, String condition,
Integer... expectedIds) {
IndexResponse index = indexNamed(getNameSpaceIndexes(namespaceName), "description2");
assertThat(index.getIndexingCondition(), is(condition));
List<Integer> 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";
Expand Down Expand Up @@ -198,11 +244,12 @@ private void createNamespaceWithRecords(String nameSpaceName, int recordsCnt) {
private List<IndexResponse> getNameSpaceIndexes(String nameSpaceName) {
Namespace<NamespaceResponse> serviceNamespace = db.openNamespace("#namespaces", NamespaceOptions.defaultOptions(), NamespaceResponse.class);
String query = String.format("select * from #namespaces where name = '%s'", nameSpaceName);
Iterator<NamespaceResponse> iterator = serviceNamespace.execSql(query);
if (iterator.hasNext()) {
List<IndexResponse> indexes = iterator.next().getIndexes();
indexes.sort(Comparator.comparing(IndexResponse::getName));
return indexes;
try (ResultIterator<NamespaceResponse> iterator = serviceNamespace.execSql(query)) {
if (iterator.hasNext()) {
List<IndexResponse> indexes = iterator.next().getIndexes();
indexes.sort(Comparator.comparing(IndexResponse::getName));
return indexes;
}
}
return Collections.emptyList();
}
Expand Down Expand Up @@ -276,5 +323,7 @@ public static class IndexResponse {
private String collateMode;
@Json("sort_order_letters")
private String sortOrderLetters;
@Json("indexing_condition")
private String indexingCondition;
}
}
Loading