Possibiltity to index a field with both text and non-unique/unique filter
Closed this issue · 3 comments
When I was trying to add an index to a field that is text-based, I found weird that a index cannot be added to the same field twice. The actual index applied and behavior depends on the combinations of indexes applied:
- If
IndexType.FULL_TEXT
andIndexType.NON_UNIQUE
, aneq
filter fails with:org.dizitart.no2.exceptions.FilterException: Text index only supports a single TextFilter
- If
IndexType.FULL_TEXT
andIndexType.UNIQUE
, atext
filter does not fail, but it is not able to find any result.
An example class that can be used to show the behavior:
package io.github.magicdgs.nitrite_test;
import org.dizitart.no2.Nitrite;
import org.dizitart.no2.common.mapper.JacksonMapperModule;
import org.dizitart.no2.filters.FluentFilter;
import org.dizitart.no2.index.IndexType;
import org.dizitart.no2.repository.ObjectRepository;
import org.dizitart.no2.repository.annotations.Index;
import org.dizitart.no2.repository.annotations.Indices;
import org.junit.jupiter.api.Test;
import java.util.function.Function;
public class NitritePlaygroundTests {
@Test
public void testUnique() {
try (final Nitrite nitrite = Nitrite.builder()
.loadModule(new JacksonMapperModule())
.openOrCreate()) {
final ObjectRepository<EntityUniqueFullText> repository = nitrite.getRepository(EntityUniqueFullText.class);
testRepository(repository, EntityUniqueFullText::new);
}
}
@Test
public void testNoUnique() {
try (final Nitrite nitrite = Nitrite.builder()
.loadModule(new JacksonMapperModule())
.openOrCreate()) {
final ObjectRepository<EntityNoUniqueFullText> repository = nitrite.getRepository(EntityNoUniqueFullText.class);
testRepository(repository, EntityNoUniqueFullText::new);
}
}
private <T> void testRepository(ObjectRepository<T> repository,
Function<String, T> constructorFunction) {
final String prefix = "test";
final String entityValue1 = prefix + " 1";
final String entityValue2 = prefix + " 2";
repository.insert(constructorFunction.apply(entityValue1));
repository.insert(constructorFunction.apply(entityValue2));
try {
System.out.println("Equals search:");
repository.find(FluentFilter.where("value").eq(entityValue1)).forEach(System.out::println);
repository.find(FluentFilter.where("value").eq(entityValue2)).forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("\nText search:");
repository.find(FluentFilter.where("value").text(prefix)).forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
@Indices({
@Index(fields = "value", type = IndexType.FULL_TEXT),
@Index(fields = "value", type = IndexType.UNIQUE)
})
public record EntityUniqueFullText(String value) {
}
@Indices({
@Index(fields = "value", type = IndexType.FULL_TEXT),
@Index(fields = "value", type = IndexType.NON_UNIQUE)
})
public record EntityNoUniqueFullText(String value) {
}
}
Where if run, testNoUnique
produces:
18:15:48.096 [main] WARN nitrite -- No persistent storage module found, creating an in-memory database
Equals search:
org.dizitart.no2.exceptions.FilterException: Text index only supports a single TextFilter
at org.dizitart.no2.index.TextIndex.findNitriteIds(TextIndex.java:155)
at org.dizitart.no2.index.NitriteTextIndexer.findByFilter(NitriteTextIndexer.java:95)
at org.dizitart.no2.collection.operation.ReadOperations.findSuitableStream(ReadOperations.java:146)
at org.dizitart.no2.collection.operation.ReadOperations.createCursor(ReadOperations.java:110)
at org.dizitart.no2.collection.operation.ReadOperations.find(ReadOperations.java:70)
at org.dizitart.no2.collection.operation.CollectionOperations.find(CollectionOperations.java:114)
at org.dizitart.no2.collection.DefaultNitriteCollection.find(DefaultNitriteCollection.java:175)
at org.dizitart.no2.repository.RepositoryOperations.find(RepositoryOperations.java:193)
at org.dizitart.no2.repository.DefaultObjectRepository.find(DefaultObjectRepository.java:160)
at org.dizitart.no2.repository.ObjectRepository.find(ObjectRepository.java:277)
at io.github.magicdgs.nitrite_test.NitritePlaygroundTests.testRepository(NitritePlaygroundTests.java:47)
at io.github.magicdgs.nitrite_test.NitritePlaygroundTests.testNoUnique(NitritePlaygroundTests.java:32)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:728)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:218)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:214)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:139)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:198)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:169)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:93)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:58)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:141)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:57)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:103)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:85)
at org.junit.platform.launcher.core.DelegatingLauncher.execute(DelegatingLauncher.java:47)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:63)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Text search:
EntityNoUniqueFullText[value=test 1]
EntityNoUniqueFullText[value=test 2]
18:15:48.423 [main] INFO nitrite -- Nitrite database has been closed successfully.
And testUnique
:
18:15:48.431 [main] WARN nitrite -- No persistent storage module found, creating an in-memory database
Equals search:
EntityUniqueFullText[value=test 1]
EntityUniqueFullText[value=test 2]
Text search:
18:15:48.440 [main] INFO nitrite -- Nitrite database has been closed successfully.
Is there a way to provide a text and another index to a single field?
Originally posted by @magicDGS in nitrite/.github#50
On a closer look I see it is a bug for ObejctRepository
. In Nitrite, you cannot create a new index on a field or a set of fields which is already indexed with a different type of index. This restriction is working properly in NitriteCollection
, but not properly for ObjectRepository
. I'll give a fix soon.
Thank you for fixing it that quick!
Fix is in the 4.2.2-SNAPSHOT version. Documentation is also updated to explain the restriction clearly with examples.