From ea2e0cef66d82fc0a9822088628715049139671d Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Thu, 9 Nov 2023 18:07:58 +0100 Subject: [PATCH] HSEARCH-4948 Register codec --- .../impl/HibernateSearchLuceneCodec.java | 19 +++++++++++++-- .../services/org.apache.lucene.codecs.Codec | 1 + .../impl/HibernateSearchLuceneCodecTest.java | 24 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 backend/lucene/src/main/resources/META-INF/services/org.apache.lucene.codecs.Codec create mode 100644 backend/lucene/src/test/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodecTest.java diff --git a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodec.java b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodec.java index 18119b57147..aa70a266afc 100644 --- a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodec.java +++ b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodec.java @@ -15,14 +15,29 @@ import org.apache.lucene.codecs.Codec; import org.apache.lucene.codecs.FilterCodec; import org.apache.lucene.codecs.KnnVectorsFormat; +import org.apache.lucene.codecs.lucene95.Lucene95Codec; import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat; public class HibernateSearchLuceneCodec extends FilterCodec { + + private static final Codec DEFAULT_CODEC = new Lucene95Codec(); + private final KnnVectorsFormat knnVectorsFormat; public HibernateSearchLuceneCodec(LuceneIndexModel model) { - super( HibernateSearchLuceneCodec.class.getSimpleName(), Codec.getDefault() ); - this.knnVectorsFormat = new IndexModelBasedPerFieldKnnVectorsFormat( model ); + this( new IndexModelBasedPerFieldKnnVectorsFormat( model ) ); + } + + public HibernateSearchLuceneCodec(KnnVectorsFormat knnVectorsFormat) { + super( HibernateSearchLuceneCodec.class.getSimpleName(), DEFAULT_CODEC ); + this.knnVectorsFormat = knnVectorsFormat; + } + + public HibernateSearchLuceneCodec() { + this( (KnnVectorsFormat) null ); + // NOTE: we are not providing access to a PerFieldKnnVectorsFormat based on a model, + // since max connections and beam width parameters configured by vector format are only used by an index writer; + // and we also are crating and passing the codec to the writer ourselves. } @Override diff --git a/backend/lucene/src/main/resources/META-INF/services/org.apache.lucene.codecs.Codec b/backend/lucene/src/main/resources/META-INF/services/org.apache.lucene.codecs.Codec new file mode 100644 index 00000000000..30dcaa14dc7 --- /dev/null +++ b/backend/lucene/src/main/resources/META-INF/services/org.apache.lucene.codecs.Codec @@ -0,0 +1 @@ +org.hibernate.search.backend.lucene.lowlevel.codec.impl.HibernateSearchLuceneCodec \ No newline at end of file diff --git a/backend/lucene/src/test/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodecTest.java b/backend/lucene/src/test/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodecTest.java new file mode 100644 index 00000000000..3e89662bda0 --- /dev/null +++ b/backend/lucene/src/test/java/org/hibernate/search/backend/lucene/lowlevel/codec/impl/HibernateSearchLuceneCodecTest.java @@ -0,0 +1,24 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.search.backend.lucene.lowlevel.codec.impl; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +import org.apache.lucene.codecs.Codec; +import org.apache.lucene.codecs.lucene95.Lucene95Codec; + +class HibernateSearchLuceneCodecTest { + @Test + void checkDefaultCodec() { + assertThat( Codec.getDefault() ) + .isNotNull() + .extracting( Object::getClass ) + .isEqualTo( Lucene95Codec.class ); + } +}