-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add dims parameter to dense_vector mapping #43444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
bac0c34
c8f397a
e9be0c5
3f747bf
7b551a8
d8046cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,11 @@ | |
import org.apache.lucene.index.IndexableField; | ||
import org.apache.lucene.search.DocValuesFieldExistsQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.util.ArrayUtil; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser.Token; | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.elasticsearch.index.fielddata.IndexFieldData; | ||
import org.elasticsearch.index.mapper.ArrayValueMapperParser; | ||
import org.elasticsearch.index.mapper.FieldMapper; | ||
|
@@ -56,12 +57,28 @@ public static class Defaults { | |
} | ||
|
||
public static class Builder extends FieldMapper.Builder<Builder, DenseVectorFieldMapper> { | ||
private int dims = 0; | ||
|
||
public Builder(String name) { | ||
super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); | ||
builder = this; | ||
} | ||
|
||
public Builder dims(int dims) { | ||
if ((dims > MAX_DIMS_COUNT) || (dims < 1)) { | ||
throw new MapperParsingException("The number of dimensions for field [" + name + | ||
"] should be in the range [1, " + MAX_DIMS_COUNT + "]"); | ||
} | ||
this.dims = dims; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void setupFieldType(BuilderContext context) { | ||
super.setupFieldType(context); | ||
fieldType().setDims(dims); | ||
} | ||
|
||
@Override | ||
public DenseVectorFieldType fieldType() { | ||
return (DenseVectorFieldType) super.fieldType(); | ||
|
@@ -80,11 +97,17 @@ public static class TypeParser implements Mapper.TypeParser { | |
@Override | ||
public Mapper.Builder<?,?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException { | ||
DenseVectorFieldMapper.Builder builder = new DenseVectorFieldMapper.Builder(name); | ||
return builder; | ||
Object dimsField = node.remove("dims"); | ||
jtibshirani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (dimsField == null) { | ||
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. Using the same reasoning as above, it would be nice to move this check to 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. @jtibshirani Thanks, but we need this check here to ensure we don't get
Also, I was thinking that 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. One approach would be to check if the node is null, and only parse it and call |
||
throw new MapperParsingException("The [dims] property must be specified for field [" + name + "]."); | ||
} | ||
int dims = XContentMapValues.nodeIntegerValue(dimsField); | ||
return builder.dims(dims); | ||
} | ||
} | ||
|
||
public static final class DenseVectorFieldType extends MappedFieldType { | ||
private int dims; | ||
|
||
public DenseVectorFieldType() {} | ||
|
||
|
@@ -96,6 +119,14 @@ public DenseVectorFieldType clone() { | |
return new DenseVectorFieldType(this); | ||
} | ||
|
||
int dims() { | ||
return dims; | ||
} | ||
|
||
void setDims(int dims) { | ||
this.dims = dims; | ||
} | ||
|
||
@Override | ||
public String typeName() { | ||
return CONTENT_TYPE; | ||
|
@@ -143,39 +174,47 @@ public DenseVectorFieldType fieldType() { | |
@Override | ||
public void parse(ParseContext context) throws IOException { | ||
if (context.externalValueSet()) { | ||
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] can't be used in multi-fields"); | ||
throw new MapperParsingException("Field [" + name() + "] of type [" + typeName() + "] can't be used in multi-fields"); | ||
} | ||
int dims = fieldType().dims(); //number of vector dimensions | ||
|
||
// encode array of floats as array of integers and store into buf | ||
// this code is here and not int the VectorEncoderDecoder so not to create extra arrays | ||
byte[] buf = new byte[0]; | ||
byte[] buf = new byte[dims * INT_BYTES]; | ||
int offset = 0; | ||
int dim = 0; | ||
for (Token token = context.parser().nextToken(); token != Token.END_ARRAY; token = context.parser().nextToken()) { | ||
if (dim++ >= dims) { | ||
throw new MapperParsingException("Field [" + name() + "] of type [" + typeName() + "] of doc [" + | ||
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. It seems like 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. @jtibshirani I agree with you. This should be |
||
context.sourceToParse().id() + "] has exceeded the number of dimensions [" + dims + "] defined in mapping"); | ||
} | ||
ensureExpectedToken(Token.VALUE_NUMBER, token, context.parser()::getTokenLocation); | ||
float value = context.parser().floatValue(true); | ||
if (buf.length < (offset + INT_BYTES)) { | ||
buf = ArrayUtil.grow(buf, (offset + INT_BYTES)); | ||
} | ||
int intValue = Float.floatToIntBits(value); | ||
buf[offset] = (byte) (intValue >> 24); | ||
buf[offset+1] = (byte) (intValue >> 16); | ||
buf[offset+2] = (byte) (intValue >> 8); | ||
buf[offset+3] = (byte) intValue; | ||
offset += INT_BYTES; | ||
if (dim++ >= MAX_DIMS_COUNT) { | ||
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + | ||
"] has exceeded the maximum allowed number of dimensions of [" + MAX_DIMS_COUNT + "]"); | ||
} | ||
buf[offset++] = (byte) (intValue >> 24); | ||
buf[offset++] = (byte) (intValue >> 16); | ||
buf[offset++] = (byte) (intValue >> 8); | ||
buf[offset++] = (byte) intValue; | ||
} | ||
if (dim != dims) { | ||
throw new MapperParsingException("Field [" + name() + "] of type [" + typeName() + "] of doc [" + | ||
context.sourceToParse().id() + "] has number of dimensions [" + dim + | ||
"] less than defined in the mapping [" + dims +"]"); | ||
} | ||
BinaryDocValuesField field = new BinaryDocValuesField(fieldType().name(), new BytesRef(buf, 0, offset)); | ||
if (context.doc().getByKey(fieldType().name()) != null) { | ||
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + | ||
throw new MapperParsingException("Field [" + name() + "] of type [" + typeName() + | ||
"] doesn't not support indexing multiple values for the same field in the same document"); | ||
} | ||
context.doc().addWithKey(fieldType().name(), field); | ||
} | ||
|
||
@Override | ||
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { | ||
super.doXContentBody(builder, includeDefaults, params); | ||
builder.field("dims", fieldType().dims()); | ||
} | ||
|
||
@Override | ||
protected void parseCreateField(ParseContext context, List<IndexableField> fields) { | ||
throw new AssertionError("parse is implemented directly"); | ||
|
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 think this callout
<1>
should go on the next line.