internal ElasticsearchVectorStoreRecordCollection()

in Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchVectorStoreRecordCollection.cs [94:156]


    internal ElasticsearchVectorStoreRecordCollection(MockableElasticsearchClient elasticsearchClient,
        string collectionName, ElasticsearchVectorStoreRecordCollectionOptions<TRecord>? options = null)
    {
        // Verify.
        Verify.NotNull(elasticsearchClient);
        Verify.NotNullOrWhiteSpace(collectionName);
        VectorStoreRecordPropertyVerification.VerifyGenericDataModelKeyType(typeof(TRecord), false /* TODO: options?.CustomMapper is not null */, SupportedKeyTypes);
        VectorStoreRecordPropertyVerification.VerifyGenericDataModelDefinitionSupplied(typeof(TRecord), options?.VectorStoreRecordDefinition is not null);

        // Assign.
        _elasticsearchClient = elasticsearchClient;
        CollectionName = collectionName;
        _options = options ?? new ElasticsearchVectorStoreRecordCollectionOptions<TRecord>();
        _propertyReader = new VectorStoreRecordPropertyReader(
            typeof(TRecord),
            _options.VectorStoreRecordDefinition,
            new VectorStoreRecordPropertyReaderOptions
            {
                RequiresAtLeastOneVector = false,
                SupportsMultipleKeys = false,
                SupportsMultipleVectors = true
            });

        if (typeof(TRecord) == typeof(VectorStoreGenericDataModel<string>))
        {
            // Prioritize the user provided `StoragePropertyName` or fall-back to using the `DefaultFieldNameInferrer`
            // function of the Elasticsearch client which by default redirects to the
            // `JsonSerializerOptions.PropertyNamingPolicy.Convert() method.
            _propertyToStorageName = _propertyReader.Properties.ToDictionary(k => k, v => v.StoragePropertyName ??
                _elasticsearchClient.ElasticsearchClient.ElasticsearchClientSettings.DefaultFieldNameInferrer(v.DataModelPropertyName));

            _mapper = (new ElasticsearchGenericDataModelMapper(_propertyToStorageName, elasticsearchClient.ElasticsearchClient.ElasticsearchClientSettings) as
                IVectorStoreRecordMapper<TRecord, (string id, JsonObject document)>)!;
        }
        else
        {
            // Use the built-in property name inference of the Elasticsearch client. The default implementation
            // prioritizes `JsonPropertyName` attributes and falls-back to the `DefaultFieldNameInferrer` function,
            // which by default redirects to the `JsonSerializerOptions.PropertyNamingPolicy.Convert() method.
            _propertyToStorageName = _propertyReader.Properties.ToDictionary(k => k, v =>
            {
                var info = _propertyReader.KeyPropertiesInfo.FirstOrDefault(x => string.Equals(x.Name, v.DataModelPropertyName, StringComparison.Ordinal)) ??
                           _propertyReader.VectorPropertiesInfo.FirstOrDefault(x => string.Equals(x.Name, v.DataModelPropertyName, StringComparison.Ordinal)) ??
                           _propertyReader.DataPropertiesInfo.FirstOrDefault(x => string.Equals(x.Name, v.DataModelPropertyName, StringComparison.Ordinal));

                if (info is null)
                {
                    throw new InvalidOperationException("unreachable");
                }

                return _elasticsearchClient.ElasticsearchClient.Infer.PropertyName(info);
            });

            _mapper = new ElasticsearchDataModelMapper<TRecord>(_propertyToStorageName, elasticsearchClient.ElasticsearchClient.ElasticsearchClientSettings);
        }

        // Validate property types.
        _propertyReader.VerifyKeyProperties(SupportedKeyTypes);
        VectorStoreRecordPropertyVerification.VerifyPropertyTypes(_propertyReader.VectorProperties,
            [typeof(ReadOnlyMemory<float>), typeof(ReadOnlyMemory<float>?)],
            [typeof(float)],
            "Vector");
    }