Spaces:
Running
Running
File size: 28,216 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 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package inverted
import (
"context"
"encoding/binary"
"fmt"
"strconv"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/weaviate/sroar"
"github.com/weaviate/weaviate/adapters/repos/db/helpers"
"github.com/weaviate/weaviate/adapters/repos/db/inverted/stopwords"
"github.com/weaviate/weaviate/adapters/repos/db/lsmkv"
"github.com/weaviate/weaviate/adapters/repos/db/propertyspecific"
"github.com/weaviate/weaviate/adapters/repos/db/sorter"
"github.com/weaviate/weaviate/entities/additional"
"github.com/weaviate/weaviate/entities/filters"
"github.com/weaviate/weaviate/entities/inverted"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/entities/storobj"
"golang.org/x/sync/errgroup"
)
type Searcher struct {
logger logrus.FieldLogger
store *lsmkv.Store
schema schema.Schema
classSearcher ClassSearcher // to allow recursive searches on ref-props
propIndices propertyspecific.Indices
stopwords stopwords.StopwordDetector
shardVersion uint16
isFallbackToSearchable IsFallbackToSearchable
tenant string
// nestedCrossRefLimit limits the number of nested cross refs returned for a query
nestedCrossRefLimit int64
}
func NewSearcher(logger logrus.FieldLogger, store *lsmkv.Store,
schema schema.Schema, propIndices propertyspecific.Indices,
classSearcher ClassSearcher, stopwords stopwords.StopwordDetector,
shardVersion uint16, isFallbackToSearchable IsFallbackToSearchable,
tenant string, nestedCrossRefLimit int64,
) *Searcher {
return &Searcher{
logger: logger,
store: store,
schema: schema,
propIndices: propIndices,
classSearcher: classSearcher,
stopwords: stopwords,
shardVersion: shardVersion,
isFallbackToSearchable: isFallbackToSearchable,
tenant: tenant,
nestedCrossRefLimit: nestedCrossRefLimit,
}
}
// Objects returns a list of full objects
func (s *Searcher) Objects(ctx context.Context, limit int,
filter *filters.LocalFilter, sort []filters.Sort, additional additional.Properties,
className schema.ClassName,
) ([]*storobj.Object, error) {
allowList, err := s.docIDs(ctx, filter, additional, className, limit)
if err != nil {
return nil, err
}
var it docIDsIterator
if len(sort) > 0 {
docIDs, err := s.sort(ctx, limit, sort, allowList, additional, className)
if err != nil {
return nil, errors.Wrap(err, "sort doc ids")
}
it = newSliceDocIDsIterator(docIDs)
} else {
it = allowList.LimitedIterator(limit)
}
return s.objectsByDocID(it, additional)
}
func (s *Searcher) sort(ctx context.Context, limit int, sort []filters.Sort, docIDs helpers.AllowList,
additional additional.Properties, className schema.ClassName,
) ([]uint64, error) {
lsmSorter, err := sorter.NewLSMSorter(s.store, s.schema, className)
if err != nil {
return nil, err
}
return lsmSorter.SortDocIDs(ctx, limit, sort, docIDs)
}
func (s *Searcher) objectsByDocID(it docIDsIterator,
additional additional.Properties,
) ([]*storobj.Object, error) {
bucket := s.store.Bucket(helpers.ObjectsBucketLSM)
if bucket == nil {
return nil, errors.Errorf("objects bucket not found")
}
out := make([]*storobj.Object, it.Len())
docIDBytes := make([]byte, 8)
i := 0
for docID, ok := it.Next(); ok; docID, ok = it.Next() {
binary.LittleEndian.PutUint64(docIDBytes, docID)
res, err := bucket.GetBySecondary(0, docIDBytes)
if err != nil {
return nil, err
}
if res == nil {
continue
}
var unmarshalled *storobj.Object
if additional.ReferenceQuery {
unmarshalled, err = storobj.FromBinaryUUIDOnly(res)
} else {
unmarshalled, err = storobj.FromBinaryOptional(res, additional)
}
if err != nil {
return nil, errors.Wrapf(err, "unmarshal data object at position %d", i)
}
out[i] = unmarshalled
i++
}
return out[:i], nil
}
// DocIDs is similar to Objects, but does not actually resolve the docIDs to
// full objects. Instead it returns the pure object id pointers. They can then
// be used in a secondary index (e.g. vector index)
//
// DocID queries does not contain a limit by design, as we won't know if the limit
// wouldn't remove the item that is most important for the follow up query.
// Imagine the user sets the limit to 1 and the follow-up is a vector search.
// If we already limited the allowList to 1, the vector search would be
// pointless, as only the first element would be allowed, regardless of which
// had the shortest distance
func (s *Searcher) DocIDs(ctx context.Context, filter *filters.LocalFilter,
additional additional.Properties, className schema.ClassName,
) (helpers.AllowList, error) {
return s.docIDs(ctx, filter, additional, className, 0)
}
func (s *Searcher) docIDs(ctx context.Context, filter *filters.LocalFilter,
additional additional.Properties, className schema.ClassName,
limit int,
) (helpers.AllowList, error) {
pv, err := s.extractPropValuePair(filter.Root, className)
if err != nil {
return nil, err
}
if err := pv.fetchDocIDs(s, limit); err != nil {
return nil, errors.Wrap(err, "fetch doc ids for prop/value pair")
}
dbm, err := pv.mergeDocIDs()
if err != nil {
return nil, errors.Wrap(err, "merge doc ids by operator")
}
return helpers.NewAllowListFromBitmap(dbm.docIDs), nil
}
func (s *Searcher) extractPropValuePair(filter *filters.Clause,
className schema.ClassName,
) (*propValuePair, error) {
class := s.schema.FindClassByName(schema.ClassName(className))
if class == nil {
return nil, fmt.Errorf("class %q not found", className)
}
out, err := newPropValuePair(class)
if err != nil {
return nil, errors.Wrap(err, "new prop value pair")
}
if filter.Operands != nil {
// nested filter
children, err := s.extractPropValuePairs(filter.Operands, className)
if err != nil {
return nil, err
}
out.children = children
out.operator = filter.Operator
return out, nil
}
if filter.Operator == filters.ContainsAny || filter.Operator == filters.ContainsAll {
return s.extractContains(filter.On, filter.Value.Type, filter.Value.Value, filter.Operator, class)
}
// on value or non-nested filter
props := filter.On.Slice()
propName := props[0]
if s.onInternalProp(propName) {
return s.extractInternalProp(propName, filter.Value.Type, filter.Value.Value, filter.Operator, class)
}
if extractedPropName, ok := schema.IsPropertyLength(propName, 0); ok {
property, err := s.schema.GetProperty(className, schema.PropertyName(extractedPropName))
if err != nil {
return nil, err
}
return s.extractPropertyLength(property, filter.Value.Type, filter.Value.Value, filter.Operator, class)
}
property, err := s.schema.GetProperty(className, schema.PropertyName(propName))
if err != nil {
return nil, err
}
if s.onRefProp(property) && len(props) != 1 {
return s.extractReferenceFilter(property, filter, class)
}
if s.onRefProp(property) && filter.Value.Type == schema.DataTypeInt {
// ref prop and int type is a special case, the user is looking for the
// reference count as opposed to the content
return s.extractReferenceCount(property, filter.Value.Value, filter.Operator, class)
}
if filter.Operator == filters.OperatorIsNull {
return s.extractPropertyNull(property, filter.Value.Type, filter.Value.Value, filter.Operator, class)
}
if s.onGeoProp(property) {
return s.extractGeoFilter(property, filter.Value.Value, filter.Value.Type, filter.Operator, class)
}
if s.onUUIDProp(property) {
return s.extractUUIDFilter(property, filter.Value.Value, filter.Value.Type, filter.Operator, class)
}
if s.onTokenizableProp(property) {
return s.extractTokenizableProp(property, filter.Value.Type, filter.Value.Value, filter.Operator, class)
}
return s.extractPrimitiveProp(property, filter.Value.Type, filter.Value.Value, filter.Operator, class)
}
func (s *Searcher) extractPropValuePairs(operands []filters.Clause, className schema.ClassName) ([]*propValuePair, error) {
children := make([]*propValuePair, len(operands))
eg := errgroup.Group{}
// prevent unbounded concurrency, see
// https://github.com/weaviate/weaviate/issues/3179 for details
eg.SetLimit(2 * _NUMCPU)
for i, clause := range operands {
i, clause := i, clause
eg.Go(func() error {
child, err := s.extractPropValuePair(&clause, className)
if err != nil {
return errors.Wrapf(err, "nested clause at pos %d", i)
}
children[i] = child
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, fmt.Errorf("nested query: %w", err)
}
return children, nil
}
func (s *Searcher) extractReferenceFilter(prop *models.Property,
filter *filters.Clause, class *models.Class,
) (*propValuePair, error) {
ctx := context.TODO()
return newRefFilterExtractor(s.logger, s.classSearcher, filter, class, prop, s.tenant, s.nestedCrossRefLimit).
Do(ctx)
}
func (s *Searcher) extractPrimitiveProp(prop *models.Property, propType schema.DataType,
value interface{}, operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var extractValueFn func(in interface{}) ([]byte, error)
switch propType {
case schema.DataTypeBoolean:
extractValueFn = s.extractBoolValue
case schema.DataTypeInt:
extractValueFn = s.extractIntValue
case schema.DataTypeNumber:
extractValueFn = s.extractNumberValue
case schema.DataTypeDate:
extractValueFn = s.extractDateValue
case "":
return nil, fmt.Errorf("data type cannot be empty")
default:
return nil, fmt.Errorf("data type %q not supported in query", propType)
}
byteValue, err := extractValueFn(value)
if err != nil {
return nil, err
}
hasFilterableIndex := HasFilterableIndex(prop)
hasSearchableIndex := HasSearchableIndex(prop)
if !hasFilterableIndex && !hasSearchableIndex {
return nil, inverted.NewMissingFilterableIndexError(prop.Name)
}
return &propValuePair{
value: byteValue,
prop: prop.Name,
operator: operator,
hasFilterableIndex: hasFilterableIndex,
hasSearchableIndex: hasSearchableIndex,
Class: class,
}, nil
}
func (s *Searcher) extractReferenceCount(prop *models.Property, value interface{},
operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
byteValue, err := s.extractIntCountValue(value)
if err != nil {
return nil, err
}
hasFilterableIndex := HasFilterableIndexMetaCount && HasInvertedIndex(prop)
hasSearchableIndex := HasSearchableIndexMetaCount && HasInvertedIndex(prop)
if !hasFilterableIndex && !hasSearchableIndex {
return nil, inverted.NewMissingFilterableMetaCountIndexError(prop.Name)
}
return &propValuePair{
value: byteValue,
prop: helpers.MetaCountProp(prop.Name),
operator: operator,
hasFilterableIndex: hasFilterableIndex,
hasSearchableIndex: hasSearchableIndex,
Class: class,
}, nil
}
func (s *Searcher) extractGeoFilter(prop *models.Property, value interface{},
valueType schema.DataType, operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
if valueType != schema.DataTypeGeoCoordinates {
return nil, fmt.Errorf("prop %q is of type geoCoordinates, it can only"+
"be used with geoRange filters", prop.Name)
}
parsed := value.(filters.GeoRange)
return &propValuePair{
value: nil, // not going to be served by an inverted index
valueGeoRange: &parsed,
prop: prop.Name,
operator: operator,
hasFilterableIndex: HasFilterableIndex(prop),
hasSearchableIndex: HasSearchableIndex(prop),
Class: class,
}, nil
}
func (s *Searcher) extractUUIDFilter(prop *models.Property, value interface{},
valueType schema.DataType, operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var byteValue []byte
switch valueType {
case schema.DataTypeText:
asStr, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected to see uuid as string in filter, got %T", value)
}
parsed, err := uuid.Parse(asStr)
if err != nil {
return nil, fmt.Errorf("parse uuid string: %w", err)
}
byteValue = parsed[:]
default:
return nil, fmt.Errorf("prop %q is of type uuid, the uuid to filter "+
"on must be specified as a string (e.g. valueText:<uuid>)", prop.Name)
}
hasFilterableIndex := HasFilterableIndex(prop)
hasSearchableIndex := HasSearchableIndex(prop)
if !hasFilterableIndex && !hasSearchableIndex {
return nil, inverted.NewMissingFilterableIndexError(prop.Name)
}
return &propValuePair{
value: byteValue,
prop: prop.Name,
operator: operator,
hasFilterableIndex: hasFilterableIndex,
hasSearchableIndex: hasSearchableIndex,
Class: class,
}, nil
}
func (s *Searcher) extractInternalProp(propName string, propType schema.DataType, value interface{},
operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
switch propName {
case filters.InternalPropBackwardsCompatID, filters.InternalPropID:
return s.extractIDProp(propName, propType, value, operator, class)
case filters.InternalPropCreationTimeUnix, filters.InternalPropLastUpdateTimeUnix:
return s.extractTimestampProp(propName, propType, value, operator, class)
default:
return nil, fmt.Errorf(
"failed to extract internal prop, unsupported internal prop '%s'", propName)
}
}
func (s *Searcher) extractIDProp(propName string, propType schema.DataType,
value interface{}, operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var byteValue []byte
switch propType {
case schema.DataTypeText:
v, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected value to be string, got '%T'", value)
}
byteValue = []byte(v)
default:
return nil, fmt.Errorf(
"failed to extract id prop, unsupported type '%T' for prop '%s'", propType, propName)
}
return &propValuePair{
value: byteValue,
prop: filters.InternalPropID,
operator: operator,
hasFilterableIndex: HasFilterableIndexIdProp,
hasSearchableIndex: HasSearchableIndexIdProp,
Class: class,
}, nil
}
func (s *Searcher) extractTimestampProp(propName string, propType schema.DataType, value interface{},
operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var byteValue []byte
switch propType {
case schema.DataTypeText:
v, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected value to be string, got '%T'", value)
}
_, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, fmt.Errorf("expected value to be timestamp, got '%s'", v)
}
byteValue = []byte(v)
case schema.DataTypeDate:
v, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected value to be string, got '%T'", value)
}
t, err := time.Parse(time.RFC3339, v)
if err != nil {
return nil, errors.Wrap(err, "trying parse time as RFC3339 string")
}
// if propType is a `valueDate`, we need to convert
// it to ms before fetching. this is the format by
// which our timestamps are indexed
byteValue = []byte(strconv.FormatInt(t.UnixMilli(), 10))
default:
return nil, fmt.Errorf(
"failed to extract timestamp prop, unsupported type '%T' for prop '%s'", propType, propName)
}
return &propValuePair{
value: byteValue,
prop: propName,
operator: operator,
hasFilterableIndex: HasFilterableIndexTimestampProp, // TODO text_rbm_inverted_index & with settings
hasSearchableIndex: HasSearchableIndexTimestampProp, // TODO text_rbm_inverted_index & with settings
Class: class,
}, nil
}
func (s *Searcher) extractTokenizableProp(prop *models.Property, propType schema.DataType,
value interface{}, operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var terms []string
valueString, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected value to be string, got '%T'", value)
}
switch propType {
case schema.DataTypeText:
// if the operator is like, we cannot apply the regular text-splitting
// logic as it would remove all wildcard symbols
if operator == filters.OperatorLike {
terms = helpers.TokenizeWithWildcards(prop.Tokenization, valueString)
} else {
terms = helpers.Tokenize(prop.Tokenization, valueString)
}
default:
return nil, fmt.Errorf("expected value type to be text, got %v", propType)
}
hasFilterableIndex := HasFilterableIndex(prop) && !s.isFallbackToSearchable()
hasSearchableIndex := HasSearchableIndex(prop)
if !hasFilterableIndex && !hasSearchableIndex {
return nil, inverted.NewMissingFilterableIndexError(prop.Name)
}
propValuePairs := make([]*propValuePair, 0, len(terms))
for _, term := range terms {
if s.stopwords.IsStopword(term) {
continue
}
propValuePairs = append(propValuePairs, &propValuePair{
value: []byte(term),
prop: prop.Name,
operator: operator,
hasFilterableIndex: hasFilterableIndex,
hasSearchableIndex: hasSearchableIndex,
Class: class,
})
}
if len(propValuePairs) > 1 {
return &propValuePair{operator: filters.OperatorAnd, children: propValuePairs, Class: class}, nil
}
if len(propValuePairs) == 1 {
return propValuePairs[0], nil
}
return nil, errors.Errorf("invalid search term, only stopwords provided. Stopwords can be configured in class.invertedIndexConfig.stopwords")
}
func (s *Searcher) extractPropertyLength(prop *models.Property, propType schema.DataType,
value interface{}, operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var byteValue []byte
switch propType {
case schema.DataTypeInt:
b, err := s.extractIntValue(value)
if err != nil {
return nil, err
}
byteValue = b
default:
return nil, fmt.Errorf(
"failed to extract length of prop, unsupported type '%T' for length of prop '%s'", propType, prop.Name)
}
return &propValuePair{
value: byteValue,
prop: helpers.PropLength(prop.Name),
operator: operator,
hasFilterableIndex: HasFilterableIndexPropLength, // TODO text_rbm_inverted_index & with settings
hasSearchableIndex: HasSearchableIndexPropLength, // TODO text_rbm_inverted_index & with settings
Class: class,
}, nil
}
func (s *Searcher) extractPropertyNull(prop *models.Property, propType schema.DataType,
value interface{}, operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var valResult []byte
switch propType {
case schema.DataTypeBoolean:
b, err := s.extractBoolValue(value)
if err != nil {
return nil, err
}
valResult = b
default:
return nil, fmt.Errorf(
"failed to extract null prop, unsupported type '%T' for null prop '%s'", propType, prop.Name)
}
return &propValuePair{
value: valResult,
prop: helpers.PropNull(prop.Name),
operator: operator,
hasFilterableIndex: HasFilterableIndexPropNull, // TODO text_rbm_inverted_index & with settings
hasSearchableIndex: HasSearchableIndexPropNull, // TODO text_rbm_inverted_index & with settings
Class: class,
}, nil
}
func (s *Searcher) extractContains(path *filters.Path, propType schema.DataType, value interface{},
operator filters.Operator, class *models.Class,
) (*propValuePair, error) {
var operands []filters.Clause
switch propType {
case schema.DataTypeText, schema.DataTypeTextArray:
valueStringArray, err := s.extractStringArray(value)
if err != nil {
return nil, err
}
operands = getContainsOperands(propType, path, valueStringArray)
case schema.DataTypeInt, schema.DataTypeIntArray:
valueIntArray, err := s.extractIntArray(value)
if err != nil {
return nil, err
}
operands = getContainsOperands(propType, path, valueIntArray)
case schema.DataTypeNumber, schema.DataTypeNumberArray:
valueFloat64Array, err := s.extractFloat64Array(value)
if err != nil {
return nil, err
}
operands = getContainsOperands(propType, path, valueFloat64Array)
case schema.DataTypeBoolean, schema.DataTypeBooleanArray:
valueBooleanArray, err := s.extractBoolArray(value)
if err != nil {
return nil, err
}
operands = getContainsOperands(propType, path, valueBooleanArray)
case schema.DataTypeDate, schema.DataTypeDateArray:
valueDateArray, err := s.extractStringArray(value)
if err != nil {
return nil, err
}
operands = getContainsOperands(propType, path, valueDateArray)
default:
return nil, fmt.Errorf("unsupported type '%T' for '%v' operator", propType, operator)
}
children, err := s.extractPropValuePairs(operands, schema.ClassName(class.Class))
if err != nil {
return nil, err
}
out, err := newPropValuePair(class)
if err != nil {
return nil, errors.Wrap(err, "new prop value pair")
}
out.children = children
// filters.ContainsAny
out.operator = filters.OperatorOr
if operator == filters.ContainsAll {
out.operator = filters.OperatorAnd
}
out.Class = class
return out, nil
}
// TODO: repeated calls to on... aren't too efficient because we iterate over
// the schema each time, might be smarter to have a single method that
// determines the type and then we switch based on the result. However, the
// effect of that should be very small unless the schema is absolutely massive.
func (s *Searcher) onRefProp(property *models.Property) bool {
return schema.IsRefDataType(property.DataType)
}
// TODO: repeated calls to on... aren't too efficient because we iterate over
// the schema each time, might be smarter to have a single method that
// determines the type and then we switch based on the result. However, the
// effect of that should be very small unless the schema is absolutely massive.
func (s *Searcher) onGeoProp(prop *models.Property) bool {
return schema.DataType(prop.DataType[0]) == schema.DataTypeGeoCoordinates
}
// Note: A UUID prop is a user-specified prop of type UUID. This has nothing to
// do with the primary ID of an object which happens to always be a UUID in
// Weaviate v1
//
// TODO: repeated calls to on... aren't too efficient because we iterate over
// the schema each time, might be smarter to have a single method that
// determines the type and then we switch based on the result. However, the
// effect of that should be very small unless the schema is absolutely massive.
func (s *Searcher) onUUIDProp(prop *models.Property) bool {
switch dt, _ := schema.AsPrimitive(prop.DataType); dt {
case schema.DataTypeUUID, schema.DataTypeUUIDArray:
return true
default:
return false
}
}
func (s *Searcher) onInternalProp(propName string) bool {
return filters.IsInternalProperty(schema.PropertyName(propName))
}
func (s *Searcher) onTokenizableProp(prop *models.Property) bool {
switch dt, _ := schema.AsPrimitive(prop.DataType); dt {
case schema.DataTypeText, schema.DataTypeTextArray:
return true
default:
return false
}
}
func (s *Searcher) extractStringArray(value interface{}) ([]string, error) {
switch v := value.(type) {
case []string:
return v, nil
case []interface{}:
vals := make([]string, len(v))
for i := range v {
val, ok := v[i].(string)
if !ok {
return nil, fmt.Errorf("value[%d] type should be string but is %T", i, v[i])
}
vals[i] = val
}
return vals, nil
default:
return nil, fmt.Errorf("value type should be []string but is %T", value)
}
}
func (s *Searcher) extractIntArray(value interface{}) ([]int, error) {
switch v := value.(type) {
case []int:
return v, nil
case []interface{}:
vals := make([]int, len(v))
for i := range v {
// in this case all number values are unmarshalled to float64, so we need to cast to float64
// and then make int
val, ok := v[i].(float64)
if !ok {
return nil, fmt.Errorf("value[%d] type should be float64 but is %T", i, v[i])
}
vals[i] = int(val)
}
return vals, nil
default:
return nil, fmt.Errorf("value type should be []int but is %T", value)
}
}
func (s *Searcher) extractFloat64Array(value interface{}) ([]float64, error) {
switch v := value.(type) {
case []float64:
return v, nil
case []interface{}:
vals := make([]float64, len(v))
for i := range v {
val, ok := v[i].(float64)
if !ok {
return nil, fmt.Errorf("value[%d] type should be float64 but is %T", i, v[i])
}
vals[i] = val
}
return vals, nil
default:
return nil, fmt.Errorf("value type should be []float64 but is %T", value)
}
}
func (s *Searcher) extractBoolArray(value interface{}) ([]bool, error) {
switch v := value.(type) {
case []bool:
return v, nil
case []interface{}:
vals := make([]bool, len(v))
for i := range v {
val, ok := v[i].(bool)
if !ok {
return nil, fmt.Errorf("value[%d] type should be bool but is %T", i, v[i])
}
vals[i] = val
}
return vals, nil
default:
return nil, fmt.Errorf("value type should be []bool but is %T", value)
}
}
func getContainsOperands[T any](propType schema.DataType, path *filters.Path, values []T) []filters.Clause {
operands := make([]filters.Clause, len(values))
for i := range values {
operands[i] = filters.Clause{
Operator: filters.OperatorEqual,
On: path,
Value: &filters.Value{
Type: propType,
Value: values[i],
},
}
}
return operands
}
type docIDsIterator interface {
Next() (uint64, bool)
Len() int
}
type sliceDocIDsIterator struct {
docIDs []uint64
pos int
}
func newSliceDocIDsIterator(docIDs []uint64) docIDsIterator {
return &sliceDocIDsIterator{docIDs: docIDs, pos: 0}
}
func (it *sliceDocIDsIterator) Next() (uint64, bool) {
if it.pos >= len(it.docIDs) {
return 0, false
}
pos := it.pos
it.pos++
return it.docIDs[pos], true
}
func (it *sliceDocIDsIterator) Len() int {
return len(it.docIDs)
}
type docBitmap struct {
docIDs *sroar.Bitmap
}
// newUninitializedDocBitmap can be used whenever we can be sure that the first
// user of the docBitmap will set or replace the bitmap, such as a row reader
func newUninitializedDocBitmap() docBitmap {
return docBitmap{docIDs: nil}
}
func newDocBitmap() docBitmap {
return docBitmap{docIDs: sroar.NewBitmap()}
}
func (dbm *docBitmap) count() int {
if dbm.docIDs == nil {
return 0
}
return dbm.docIDs.GetCardinality()
}
func (dbm *docBitmap) IDs() []uint64 {
if dbm.docIDs == nil {
return []uint64{}
}
return dbm.docIDs.ToArray()
}
func (dbm *docBitmap) IDsWithLimit(limit int) []uint64 {
card := dbm.docIDs.GetCardinality()
if limit >= card {
return dbm.IDs()
}
out := make([]uint64, limit)
for i := range out {
// safe to ignore error, it can only error if the index is >= cardinality
// which we have already ruled out
out[i], _ = dbm.docIDs.Select(uint64(i))
}
return out
}
type docPointerWithScore struct {
id uint64
frequency float32
propLength float32
}
|