Spaces:
Running
Running
File size: 18,275 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package inverted
import (
"encoding/json"
"fmt"
"time"
"unicode/utf8"
"github.com/go-openapi/strfmt"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/weaviate/weaviate/adapters/repos/db/helpers"
"github.com/weaviate/weaviate/entities/filters"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/usecases/objects/validation"
)
func (a *Analyzer) Object(input map[string]any, props []*models.Property,
uuid strfmt.UUID,
) ([]Property, error) {
propsMap := map[string]*models.Property{}
for _, prop := range props {
propsMap[prop.Name] = prop
}
properties, err := a.analyzeProps(propsMap, input)
if err != nil {
return nil, fmt.Errorf("analyze props: %w", err)
}
idProp, err := a.analyzeIDProp(uuid)
if err != nil {
return nil, fmt.Errorf("analyze uuid prop: %w", err)
}
properties = append(properties, *idProp)
tsProps, err := a.analyzeTimestampProps(input)
if err != nil {
return nil, fmt.Errorf("analyze timestamp props: %w", err)
}
// tsProps will be nil here if weaviate is
// not setup to index by timestamps
if tsProps != nil {
properties = append(properties, tsProps...)
}
return properties, nil
}
func (a *Analyzer) analyzeProps(propsMap map[string]*models.Property,
input map[string]any,
) ([]Property, error) {
var out []Property
for key, prop := range propsMap {
if len(prop.DataType) < 1 {
return nil, fmt.Errorf("prop %q has no datatype", prop.Name)
}
if !HasInvertedIndex(prop) {
continue
}
if schema.IsBlobDataType(prop.DataType) {
continue
}
if schema.IsRefDataType(prop.DataType) {
if err := a.extendPropertiesWithReference(&out, prop, input, key); err != nil {
return nil, err
}
} else if schema.IsArrayDataType(prop.DataType) {
if err := a.extendPropertiesWithArrayType(&out, prop, input, key); err != nil {
return nil, err
}
} else {
if err := a.extendPropertiesWithPrimitive(&out, prop, input, key); err != nil {
return nil, err
}
}
}
return out, nil
}
func (a *Analyzer) analyzeIDProp(id strfmt.UUID) (*Property, error) {
value, err := id.MarshalText()
if err != nil {
return nil, fmt.Errorf("marshal id prop: %w", err)
}
return &Property{
Name: filters.InternalPropID,
Items: []Countable{
{
Data: value,
},
},
HasFilterableIndex: HasFilterableIndexIdProp,
HasSearchableIndex: HasSearchableIndexIdProp,
}, nil
}
func (a *Analyzer) analyzeTimestampProps(input map[string]any) ([]Property, error) {
createTime, createTimeOK := input[filters.InternalPropCreationTimeUnix]
updateTime, updateTimeOK := input[filters.InternalPropLastUpdateTimeUnix]
var props []Property
if createTimeOK {
b, err := json.Marshal(createTime)
if err != nil {
return nil, fmt.Errorf("analyze create timestamp prop: %w", err)
}
props = append(props, Property{
Name: filters.InternalPropCreationTimeUnix,
Items: []Countable{{Data: b}},
HasFilterableIndex: HasFilterableIndexTimestampProp,
HasSearchableIndex: HasSearchableIndexTimestampProp,
})
}
if updateTimeOK {
b, err := json.Marshal(updateTime)
if err != nil {
return nil, fmt.Errorf("analyze update timestamp prop: %w", err)
}
props = append(props, Property{
Name: filters.InternalPropLastUpdateTimeUnix,
Items: []Countable{{Data: b}},
HasFilterableIndex: HasFilterableIndexTimestampProp,
HasSearchableIndex: HasSearchableIndexTimestampProp,
})
}
return props, nil
}
func (a *Analyzer) extendPropertiesWithArrayType(properties *[]Property,
prop *models.Property, input map[string]any, propName string,
) error {
value, ok := input[propName]
if !ok {
// skip any primitive prop that's not set
return nil
}
var err error
value, err = typedSliceToUntyped(value)
if err != nil {
return fmt.Errorf("extend properties with array type: %w", err)
}
values, ok := value.([]any)
if !ok {
// skip any primitive prop that's not set
return errors.New("analyze array prop: expected array prop")
}
property, err := a.analyzeArrayProp(prop, values)
if err != nil {
return fmt.Errorf("analyze array prop: %w", err)
}
if property == nil {
return nil
}
*properties = append(*properties, *property)
return nil
}
// extendPropertiesWithPrimitive mutates the passed in properties, by extending
// it with an additional property - if applicable
func (a *Analyzer) extendPropertiesWithPrimitive(properties *[]Property,
prop *models.Property, input map[string]any, propName string,
) error {
var property *Property
var err error
value, ok := input[propName]
if !ok {
// skip any primitive prop that's not set
return nil
}
property, err = a.analyzePrimitiveProp(prop, value)
if err != nil {
return fmt.Errorf("analyze primitive prop: %w", err)
}
if property == nil {
return nil
}
*properties = append(*properties, *property)
return nil
}
func (a *Analyzer) analyzeArrayProp(prop *models.Property, values []any) (*Property, error) {
var items []Countable
hasFilterableIndex := HasFilterableIndex(prop)
hasSearchableIndex := HasSearchableIndex(prop)
switch dt := schema.DataType(prop.DataType[0]); dt {
case schema.DataTypeTextArray:
hasFilterableIndex = hasFilterableIndex && !a.isFallbackToSearchable()
in, err := stringsFromValues(prop, values)
if err != nil {
return nil, err
}
items = a.TextArray(prop.Tokenization, in)
case schema.DataTypeIntArray:
in := make([]int64, len(values))
for i, value := range values {
if asJsonNumber, ok := value.(json.Number); ok {
var err error
value, err = asJsonNumber.Float64()
if err != nil {
return nil, err
}
}
if asFloat, ok := value.(float64); ok {
// unmarshaling from json into a dynamic schema will assume every number
// is a float64
value = int64(asFloat)
}
asInt, ok := value.(int64)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type int64, but got %T", prop.Name, value)
}
in[i] = asInt
}
var err error
items, err = a.IntArray(in)
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeNumberArray:
in := make([]float64, len(values))
for i, value := range values {
if asJsonNumber, ok := value.(json.Number); ok {
var err error
value, err = asJsonNumber.Float64()
if err != nil {
return nil, err
}
}
asFloat, ok := value.(float64)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type float64, but got %T", prop.Name, value)
}
in[i] = asFloat
}
var err error
items, err = a.FloatArray(in) // convert to int before analyzing
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeBooleanArray:
in := make([]bool, len(values))
for i, value := range values {
asBool, ok := value.(bool)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type bool, but got %T", prop.Name, value)
}
in[i] = asBool
}
var err error
items, err = a.BoolArray(in) // convert to int before analyzing
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeDateArray:
in := make([]int64, len(values))
for i, value := range values {
// dates can be either a date-string or directly a time object. Try to parse both
if asTime, okTime := value.(time.Time); okTime {
in[i] = asTime.UnixNano()
} else if asString, okString := value.(string); okString {
parsedTime, err := time.Parse(time.RFC3339Nano, asString)
if err != nil {
return nil, fmt.Errorf("parse time: %w", err)
}
in[i] = parsedTime.UnixNano()
} else {
return nil, fmt.Errorf("expected property %s to be a time-string or time object, but got %T", prop.Name, value)
}
}
var err error
items, err = a.IntArray(in)
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeUUIDArray:
parsed, err := validation.ParseUUIDArray(values)
if err != nil {
return nil, fmt.Errorf("parse uuid array: %w", err)
}
items, err = a.UUIDArray(parsed)
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
default:
// ignore unsupported prop type
return nil, nil
}
return &Property{
Name: prop.Name,
Items: items,
Length: len(values),
HasFilterableIndex: hasFilterableIndex,
HasSearchableIndex: hasSearchableIndex,
}, nil
}
func stringsFromValues(prop *models.Property, values []any) ([]string, error) {
in := make([]string, len(values))
for i, value := range values {
asString, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type string, but got %T", prop.Name, value)
}
in[i] = asString
}
return in, nil
}
func (a *Analyzer) analyzePrimitiveProp(prop *models.Property, value any) (*Property, error) {
var items []Countable
propertyLength := -1 // will be overwritten for string/text, signals not to add the other types.
hasFilterableIndex := HasFilterableIndex(prop)
hasSearchableIndex := HasSearchableIndex(prop)
switch dt := schema.DataType(prop.DataType[0]); dt {
case schema.DataTypeText:
hasFilterableIndex = hasFilterableIndex && !a.isFallbackToSearchable()
asString, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type string, but got %T", prop.Name, value)
}
items = a.Text(prop.Tokenization, asString)
propertyLength = utf8.RuneCountInString(asString)
case schema.DataTypeInt:
if asFloat, ok := value.(float64); ok {
// unmarshaling from json into a dynamic schema will assume every number
// is a float64
value = int64(asFloat)
}
if asInt, ok := value.(int); ok {
// when merging an existing object we may retrieve an untyped int
value = int64(asInt)
}
asInt, ok := value.(int64)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type int64, but got %T", prop.Name, value)
}
var err error
items, err = a.Int(asInt)
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeNumber:
asFloat, ok := value.(float64)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type float64, but got %T", prop.Name, value)
}
var err error
items, err = a.Float(asFloat) // convert to int before analyzing
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeBoolean:
asBool, ok := value.(bool)
if !ok {
return nil, fmt.Errorf("expected property %s to be of type bool, but got %T", prop.Name, value)
}
var err error
items, err = a.Bool(asBool) // convert to int before analyzing
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeDate:
var err error
if asString, ok := value.(string); ok {
// for example when patching the date may have been loaded as a string
value, err = time.Parse(time.RFC3339Nano, asString)
if err != nil {
return nil, fmt.Errorf("parse stringified timestamp: %w", err)
}
}
asTime, ok := value.(time.Time)
if !ok {
return nil, fmt.Errorf("expected property %s to be time.Time, but got %T", prop.Name, value)
}
items, err = a.Int(asTime.UnixNano())
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
case schema.DataTypeUUID:
var err error
if asString, ok := value.(string); ok {
// for example when patching the uuid may have been loaded as a string
value, err = uuid.Parse(asString)
if err != nil {
return nil, fmt.Errorf("parse stringified uuid: %w", err)
}
}
asUUID, ok := value.(uuid.UUID)
if !ok {
return nil, fmt.Errorf("expected property %s to be uuid.UUID, but got %T", prop.Name, value)
}
items, err = a.UUID(asUUID)
if err != nil {
return nil, fmt.Errorf("analyze property %s: %w", prop.Name, err)
}
default:
// ignore unsupported prop type
return nil, nil
}
return &Property{
Name: prop.Name,
Items: items,
Length: propertyLength,
HasFilterableIndex: hasFilterableIndex,
HasSearchableIndex: hasSearchableIndex,
}, nil
}
// extendPropertiesWithReference extends the specified properties arrays with
// either 1 or 2 entries: If the ref is not set, only the ref-count property
// will be added. If the ref is set the ref-prop itself will also be added and
// contain all references as values
func (a *Analyzer) extendPropertiesWithReference(properties *[]Property,
prop *models.Property, input map[string]any, propName string,
) error {
value, ok := input[propName]
if !ok {
// explicitly set zero-value, so we can index for "ref not set"
value = make(models.MultipleRef, 0)
}
var asRefs models.MultipleRef
asRefs, ok = value.(models.MultipleRef)
if !ok {
// due to the fix introduced in https://github.com/weaviate/weaviate/pull/2320,
// MultipleRef's can appear as empty []any when no actual refs are provided for
// an object's reference property.
//
// if we encounter []any, assume it indicates an empty ref prop, and skip it.
_, ok := value.([]any)
if !ok {
return fmt.Errorf("expected property %q to be of type models.MutlipleRef,"+
" but got %T", prop.Name, value)
}
return nil
}
property, err := a.analyzeRefPropCount(prop, asRefs)
if err != nil {
return fmt.Errorf("ref count: %w", err)
}
*properties = append(*properties, *property)
if len(asRefs) == 0 {
return nil
}
property, err = a.analyzeRefProp(prop, asRefs)
if err != nil {
return fmt.Errorf("refs: %w", err)
}
*properties = append(*properties, *property)
return nil
}
func (a *Analyzer) analyzeRefPropCount(prop *models.Property,
value models.MultipleRef,
) (*Property, error) {
items, err := a.RefCount(value)
if err != nil {
return nil, fmt.Errorf("analyze ref-property %q: %w", prop.Name, err)
}
return &Property{
Name: helpers.MetaCountProp(prop.Name),
Items: items,
Length: len(value),
HasFilterableIndex: HasFilterableIndex(prop),
HasSearchableIndex: HasSearchableIndex(prop),
}, nil
}
func (a *Analyzer) analyzeRefProp(prop *models.Property,
value models.MultipleRef,
) (*Property, error) {
items, err := a.Ref(value)
if err != nil {
return nil, fmt.Errorf("analyze ref-property %q: %w", prop.Name, err)
}
return &Property{
Name: prop.Name,
Items: items,
HasFilterableIndex: HasFilterableIndex(prop),
HasSearchableIndex: HasSearchableIndex(prop),
}, nil
}
func typedSliceToUntyped(in any) ([]any, error) {
switch typed := in.(type) {
case []any:
// nothing to do
return typed, nil
case []string:
return convertToUntyped[string](typed), nil
case []int:
return convertToUntyped[int](typed), nil
case []time.Time:
return convertToUntyped[time.Time](typed), nil
case []bool:
return convertToUntyped[bool](typed), nil
case []float64:
return convertToUntyped[float64](typed), nil
case []uuid.UUID:
return convertToUntyped[uuid.UUID](typed), nil
default:
return nil, errors.Errorf("unsupported type %T", in)
}
}
func convertToUntyped[T comparable](in []T) []any {
out := make([]any, len(in))
for i := range out {
out[i] = in[i]
}
return out
}
// Indicates whether property should be indexed
// Index holds document ids with property of/containing particular value
// and number of its occurrences in that property
// (index created using bucket of StrategyMapCollection)
func HasSearchableIndex(prop *models.Property) bool {
switch dt, _ := schema.AsPrimitive(prop.DataType); dt {
case schema.DataTypeText, schema.DataTypeTextArray:
// by default property has searchable index only for text/text[] props
if prop.IndexSearchable == nil {
return true
}
return *prop.IndexSearchable
default:
return false
}
}
// Indicates whether property should be indexed
// Index holds document ids with property of/containing particular value
// (index created using bucket of StrategyRoaringSet)
func HasFilterableIndex(prop *models.Property) bool {
// by default property has filterable index
if prop.IndexFilterable == nil {
return true
}
return *prop.IndexFilterable
}
func HasInvertedIndex(prop *models.Property) bool {
return HasFilterableIndex(prop) || HasSearchableIndex(prop)
}
const (
// always
HasFilterableIndexIdProp = true
HasSearchableIndexIdProp = false
// only if index.invertedIndexConfig.IndexTimestamps set
HasFilterableIndexTimestampProp = true
HasSearchableIndexTimestampProp = false
// only if property.indexFilterable or property.indexSearchable set
HasFilterableIndexMetaCount = true
HasSearchableIndexMetaCount = false
// only if index.invertedIndexConfig.IndexNullState set
// and either property.indexFilterable or property.indexSearchable set
HasFilterableIndexPropNull = true
HasSearchableIndexPropNull = false
// only if index.invertedIndexConfig.IndexPropertyLength set
// and either property.indexFilterable or property.indexSearchable set
HasFilterableIndexPropLength = true
HasSearchableIndexPropLength = false
)
|