This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
k-NN plugin support for Elasticsearch version 7.10.0 #271
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ | |
# permissions and limitations under the License. | ||
# | ||
|
||
version=1.6 | ||
version=1.11.0 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,17 +34,18 @@ | |
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.mapper.Mapper; | ||
import org.elasticsearch.index.mapper.MapperParsingException; | ||
import org.elasticsearch.index.mapper.MapperService; | ||
import org.elasticsearch.index.mapper.ParametrizedFieldMapper; | ||
import org.elasticsearch.index.mapper.ParseContext; | ||
import org.elasticsearch.index.mapper.TextSearchInfo; | ||
import org.elasticsearch.index.mapper.TypeParsers; | ||
import org.elasticsearch.index.mapper.ValueFetcher; | ||
import org.elasticsearch.index.query.QueryShardContext; | ||
import org.elasticsearch.index.query.QueryShardException; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
@@ -78,9 +79,9 @@ public static class Builder extends ParametrizedFieldMapper.Builder { | |
m -> toType(m).stored, false); | ||
private final Parameter<Boolean> hasDocValues = Parameter.boolParam("doc_values", false, | ||
m -> toType(m).hasDocValues, true); | ||
private final Parameter<Integer> dimension = new Parameter<>(KNNConstants.DIMENSION, false, -1, | ||
(n, o) -> { | ||
int value = XContentMapValues.nodeIntegerValue(o); | ||
private final Parameter<Integer> dimension = new Parameter<>(KNNConstants.DIMENSION, false, () -> -1, | ||
(n, c, o) -> { | ||
int value = (o == null ? null : XContentMapValues.nodeIntegerValue(o)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if o is null, then i don't think we can assign null to value since it is primitive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Fixed. |
||
if (value > MAX_DIMENSION) { | ||
throw new IllegalArgumentException("Dimension value cannot be greater than " + | ||
MAX_DIMENSION + " for vector: " + name); | ||
|
@@ -92,9 +93,7 @@ public static class Builder extends ParametrizedFieldMapper.Builder { | |
} | ||
return value; | ||
}, m -> toType(m).dimension); | ||
|
||
private final Parameter<Map<String, String>> meta = new Parameter<>("meta", true, | ||
Collections.emptyMap(), TypeParsers::parseMeta, m -> m.fieldType().meta()); | ||
private final Parameter<Map<String, String>> meta = Parameter.metaParam(); | ||
|
||
public Builder(String name) { | ||
super(name); | ||
|
@@ -177,10 +176,15 @@ public static class KNNVectorFieldType extends MappedFieldType { | |
int dimension; | ||
|
||
public KNNVectorFieldType(String name, Map<String, String> meta, int dimension) { | ||
super(name, false, true, TextSearchInfo.NONE, meta); | ||
super(name, false, false, true, TextSearchInfo.NONE, meta); | ||
this.dimension = dimension; | ||
} | ||
|
||
@Override | ||
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { | ||
throw new UnsupportedOperationException("KNN Vector do not support fields search"); | ||
} | ||
|
||
@Override | ||
public String typeName() { | ||
return CONTENT_TYPE; | ||
|
@@ -245,7 +249,6 @@ public static class Defaults { | |
} | ||
} | ||
|
||
|
||
@Override | ||
protected String contentType() { | ||
return CONTENT_TYPE; | ||
|
@@ -346,6 +349,5 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, | |
if (includeDefaults || ignoreMalformed.explicit()) { | ||
builder.field(Names.IGNORE_MALFORMED, ignoreMalformed.value()); | ||
} | ||
builder.field(KNNConstants.DIMENSION, fieldType().dimension); | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
...ain/java/com/amazon/opendistroforelasticsearch/knn/index/codec/KNN87Codec/KNN87Codec.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,138 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copyright 2020 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Thanks |
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.knn.index.codec.KNN87Codec; | ||
|
||
import com.amazon.opendistroforelasticsearch.knn.index.codec.KNN80Codec.KNN80CompoundFormat; | ||
import com.amazon.opendistroforelasticsearch.knn.index.codec.KNN80Codec.KNN80DocValuesFormat; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.lucene.codecs.Codec; | ||
import org.apache.lucene.codecs.CompoundFormat; | ||
import org.apache.lucene.codecs.DocValuesFormat; | ||
import org.apache.lucene.codecs.FieldInfosFormat; | ||
import org.apache.lucene.codecs.LiveDocsFormat; | ||
import org.apache.lucene.codecs.NormsFormat; | ||
import org.apache.lucene.codecs.PointsFormat; | ||
import org.apache.lucene.codecs.PostingsFormat; | ||
import org.apache.lucene.codecs.SegmentInfoFormat; | ||
import org.apache.lucene.codecs.StoredFieldsFormat; | ||
import org.apache.lucene.codecs.TermVectorsFormat; | ||
import org.apache.lucene.codecs.perfield.PerFieldDocValuesFormat; | ||
|
||
/** | ||
* Extends the Codec to support a new file format for KNN index | ||
* based on the mappings. | ||
* | ||
*/ | ||
public final class KNN87Codec extends Codec { | ||
|
||
private static final Logger logger = LogManager.getLogger(KNN87Codec.class); | ||
private final DocValuesFormat docValuesFormat; | ||
private final DocValuesFormat perFieldDocValuesFormat; | ||
private final CompoundFormat compoundFormat; | ||
private Codec lucene87Codec; | ||
private PostingsFormat postingsFormat = null; | ||
|
||
public static final String KNN_87 = "KNN87Codec"; | ||
public static final String LUCENE_87 = "Lucene87"; // Lucene Codec to be used | ||
|
||
public KNN87Codec() { | ||
super(KNN_87); | ||
// Note that DocValuesFormat can use old Codec's DocValuesFormat. For instance Lucene84 uses Lucene80 | ||
// DocValuesFormat. Refer to defaultDVFormat in LuceneXXCodec.java to find out which version it uses | ||
this.docValuesFormat = new KNN80DocValuesFormat(); | ||
this.perFieldDocValuesFormat = new PerFieldDocValuesFormat() { | ||
@Override | ||
public DocValuesFormat getDocValuesFormatForField(String field) { | ||
return docValuesFormat; | ||
} | ||
}; | ||
this.compoundFormat = new KNN80CompoundFormat(); | ||
} | ||
|
||
/* | ||
* This function returns the Codec. | ||
*/ | ||
public Codec getDelegatee() { | ||
if (lucene87Codec == null) | ||
lucene87Codec = Codec.forName(LUCENE_87); | ||
return lucene87Codec; | ||
} | ||
|
||
@Override | ||
public DocValuesFormat docValuesFormat() { | ||
return this.perFieldDocValuesFormat; | ||
} | ||
|
||
/* | ||
* For all the below functions, we could have extended FilterCodec, but this brings | ||
* SPI related issues while loading Codec in the tests. So fall back to traditional | ||
* approach of manually overriding. | ||
*/ | ||
|
||
|
||
public void setPostingsFormat(PostingsFormat postingsFormat) { | ||
this.postingsFormat = postingsFormat; | ||
} | ||
|
||
@Override | ||
public PostingsFormat postingsFormat() { | ||
if (this.postingsFormat == null) { | ||
return getDelegatee().postingsFormat(); | ||
} | ||
return this.postingsFormat; | ||
} | ||
|
||
@Override | ||
public StoredFieldsFormat storedFieldsFormat() { | ||
return getDelegatee().storedFieldsFormat(); | ||
} | ||
|
||
@Override | ||
public TermVectorsFormat termVectorsFormat() { | ||
return getDelegatee().termVectorsFormat(); | ||
} | ||
|
||
@Override | ||
public FieldInfosFormat fieldInfosFormat() { | ||
return getDelegatee().fieldInfosFormat(); | ||
} | ||
|
||
@Override | ||
public SegmentInfoFormat segmentInfoFormat() { | ||
return getDelegatee().segmentInfoFormat(); | ||
} | ||
|
||
@Override | ||
public NormsFormat normsFormat() { | ||
return getDelegatee().normsFormat(); | ||
} | ||
|
||
@Override | ||
public LiveDocsFormat liveDocsFormat() { | ||
return getDelegatee().liveDocsFormat(); | ||
} | ||
|
||
@Override | ||
public CompoundFormat compoundFormat() { | ||
return this.compoundFormat; | ||
} | ||
|
||
@Override | ||
public PointsFormat pointsFormat() { | ||
return getDelegatee().pointsFormat(); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this variable is not in use. It was pointing to older version. So i updated to latest version. This variable could be used in build.gradle file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see makes sense.