CavalliumDBEngine/src/test/java/it/cavallium/dbengine/StringIndicizer.java

70 lines
2.3 KiB
Java
Raw Normal View History

2021-10-16 00:50:06 +02:00
package it.cavallium.dbengine;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import it.cavallium.dbengine.client.Indicizer;
2021-11-07 17:46:40 +01:00
import it.cavallium.dbengine.database.LLUpdateDocument;
import it.cavallium.dbengine.database.LLItem;
import it.cavallium.dbengine.database.LLTerm;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsAnalyzer;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsSimilarity;
2022-03-05 15:46:40 +01:00
import it.cavallium.dbengine.rpc.current.data.IndicizerAnalyzers;
import it.cavallium.dbengine.rpc.current.data.IndicizerSimilarities;
import java.util.LinkedList;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
2022-03-10 01:43:37 +01:00
import org.apache.lucene.index.IndexableField;
2022-02-25 15:46:32 +01:00
import org.apache.lucene.util.BytesRef;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;
public class StringIndicizer extends Indicizer<String, String> {
@Override
2021-11-07 17:46:40 +01:00
public @NotNull Mono<LLUpdateDocument> toIndexRequest(@NotNull String key, @NotNull String value) {
return Mono.fromCallable(() -> {
var fields = new LinkedList<LLItem>();
fields.add(LLItem.newStringField("uid", key, Field.Store.YES));
fields.add(LLItem.newTextField("text", value, Store.NO));
@SuppressWarnings("UnstableApiUsage")
var numInt = Ints.tryParse(value);
if (numInt != null) {
fields.add(LLItem.newIntPoint("intpoint", numInt));
2021-11-20 16:09:00 +01:00
fields.add(LLItem.newNumericDocValuesField("intsort", numInt));
}
@SuppressWarnings("UnstableApiUsage")
var numLong = Longs.tryParse(value);
if (numLong != null) {
fields.add(LLItem.newLongPoint("longpoint", numLong));
2021-11-20 16:09:00 +01:00
fields.add(LLItem.newNumericDocValuesField("longsort", numLong));
}
2022-03-18 19:16:06 +01:00
return new LLUpdateDocument(fields);
});
}
@Override
public @NotNull LLTerm toIndex(@NotNull String key) {
return new LLTerm("uid", key);
}
@Override
public @NotNull String getKeyFieldName() {
return "uid";
}
@Override
2022-03-10 01:43:37 +01:00
public @NotNull String getKey(IndexableField key) {
return key.stringValue();
}
@Override
public IndicizerAnalyzers getPerFieldAnalyzer() {
2022-03-05 15:46:40 +01:00
return it.cavallium.dbengine.client.IndicizerAnalyzers.of(TextFieldsAnalyzer.ICUCollationKey);
}
@Override
public IndicizerSimilarities getPerFieldSimilarity() {
2022-03-05 15:46:40 +01:00
return it.cavallium.dbengine.client.IndicizerSimilarities.of(TextFieldsSimilarity.BM25Standard);
}
}