github.com/weaviate/weaviate@v1.24.6/usecases/classification/classifier_run_knn.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package classification
    13  
    14  import (
    15  	"fmt"
    16  	"time"
    17  
    18  	"github.com/weaviate/weaviate/entities/models"
    19  	"github.com/weaviate/weaviate/entities/search"
    20  )
    21  
    22  func (c *Classifier) classifyItemUsingKNN(item search.Result, itemIndex int,
    23  	params models.Classification, filters Filters, writer Writer,
    24  ) error {
    25  	ctx, cancel := contextWithTimeout(2 * time.Second)
    26  	defer cancel()
    27  
    28  	// this type assertion is safe to make, since we have passed the parsing stage
    29  	settings := params.Settings.(*ParamsKNN)
    30  
    31  	// K is guaranteed to be set by now, no danger in dereferencing the pointer
    32  	res, err := c.vectorRepo.AggregateNeighbors(ctx, item.Vector,
    33  		item.ClassName,
    34  		params.ClassifyProperties, int(*settings.K), filters.TrainingSet())
    35  	if err != nil {
    36  		return fmt.Errorf("classify %s/%s: %v", item.ClassName, item.ID, err)
    37  	}
    38  
    39  	var classified []string
    40  
    41  	for _, agg := range res {
    42  		meta := agg.Meta()
    43  		item.Schema.(map[string]interface{})[agg.Property] = models.MultipleRef{
    44  			&models.SingleRef{
    45  				Beacon:         agg.Beacon,
    46  				Classification: meta,
    47  			},
    48  		}
    49  
    50  		// append list of actually classified (can differ from scope!) properties,
    51  		// so we can build the object meta information
    52  		classified = append(classified, agg.Property)
    53  	}
    54  
    55  	c.extendItemWithObjectMeta(&item, params, classified)
    56  	err = writer.Store(item)
    57  	if err != nil {
    58  		return fmt.Errorf("store %s/%s: %v", item.ClassName, item.ID, err)
    59  	}
    60  
    61  	return nil
    62  }