-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor kNN codec related classes (#582)
* Refactor codec related classes, create KNNCodecVersion abstraction Signed-off-by: Martin Gaievski <[email protected]>
- Loading branch information
1 parent
6d77882
commit 3d0a9d7
Showing
15 changed files
with
237 additions
and
334 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/java/org/opensearch/knn/index/codec/BasePerFieldKnnVectorsFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.codec; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.lucene.codecs.KnnVectorsFormat; | ||
import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat; | ||
import org.opensearch.index.mapper.MapperService; | ||
import org.opensearch.knn.common.KNNConstants; | ||
import org.opensearch.knn.index.mapper.KNNVectorFieldMapper; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Base class for PerFieldKnnVectorsFormat, builds KnnVectorsFormat based on specific Lucene version | ||
*/ | ||
@AllArgsConstructor | ||
@Log4j2 | ||
public abstract class BasePerFieldKnnVectorsFormat extends PerFieldKnnVectorsFormat { | ||
|
||
private final Optional<MapperService> mapperService; | ||
private final int defaultMaxConnections; | ||
private final int defaultBeamWidth; | ||
private final Supplier<KnnVectorsFormat> defaultFormatSupplier; | ||
private final BiFunction<Integer, Integer, KnnVectorsFormat> formatSupplier; | ||
|
||
@Override | ||
public KnnVectorsFormat getKnnVectorsFormatForField(final String field) { | ||
if (isKnnVectorFieldType(field) == false) { | ||
log.debug( | ||
"Initialize KNN vector format for field [{}] with default params [max_connections] = \"{}\" and [beam_width] = \"{}\"", | ||
field, | ||
defaultMaxConnections, | ||
defaultBeamWidth | ||
); | ||
return defaultFormatSupplier.get(); | ||
} | ||
var type = (KNNVectorFieldMapper.KNNVectorFieldType) mapperService.orElseThrow( | ||
() -> new IllegalStateException( | ||
String.format("Cannot read field type for field [%s] because mapper service is not available", field) | ||
) | ||
).fieldType(field); | ||
var params = type.getKnnMethodContext().getMethodComponent().getParameters(); | ||
int maxConnections = getMaxConnections(params); | ||
int beamWidth = getBeamWidth(params); | ||
log.debug( | ||
"Initialize KNN vector format for field [{}] with params [max_connections] = \"{}\" and [beam_width] = \"{}\"", | ||
field, | ||
maxConnections, | ||
beamWidth | ||
); | ||
return formatSupplier.apply(maxConnections, beamWidth); | ||
} | ||
|
||
private boolean isKnnVectorFieldType(final String field) { | ||
return mapperService.isPresent() && mapperService.get().fieldType(field) instanceof KNNVectorFieldMapper.KNNVectorFieldType; | ||
} | ||
|
||
private int getMaxConnections(final Map<String, Object> params) { | ||
if (params != null && params.containsKey(KNNConstants.METHOD_PARAMETER_M)) { | ||
return (int) params.get(KNNConstants.METHOD_PARAMETER_M); | ||
} | ||
return defaultMaxConnections; | ||
} | ||
|
||
private int getBeamWidth(final Map<String, Object> params) { | ||
if (params != null && params.containsKey(KNNConstants.METHOD_PARAMETER_EF_CONSTRUCTION)) { | ||
return (int) params.get(KNNConstants.METHOD_PARAMETER_EF_CONSTRUCTION); | ||
} | ||
return defaultBeamWidth; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.