github.com/weaviate/weaviate@v1.24.6/modules/sum-transformers/additional/summary/summary_result.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 summary
    13  
    14  import (
    15  	"context"
    16  	"errors"
    17  	"fmt"
    18  
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/entities/search"
    21  	"github.com/weaviate/weaviate/modules/sum-transformers/ent"
    22  )
    23  
    24  func (p *SummaryProvider) findSummary(ctx context.Context,
    25  	in []search.Result, params *Params,
    26  ) ([]search.Result, error) {
    27  	if len(in) == 0 {
    28  		return nil, nil
    29  	} else {
    30  		if params == nil {
    31  			return nil, fmt.Errorf("no params provided")
    32  		}
    33  
    34  		properties := params.GetProperties()
    35  
    36  		// check if user parameter values are valid
    37  		if len(properties) == 0 {
    38  			return in, errors.New("no properties provided")
    39  		}
    40  
    41  		for i := range in { // for each result of the general GraphQL Query
    42  			ap := in[i].AdditionalProperties
    43  			if ap == nil {
    44  				ap = models.AdditionalProperties{}
    45  			}
    46  
    47  			// check if the schema of the GraphQL data object contains the properties and they are text or string values
    48  			textProperties := map[string]string{}
    49  			schema := in[i].Object().Properties.(map[string]interface{})
    50  			for property, value := range schema {
    51  				if p.containsProperty(property, properties) {
    52  					if valueString, ok := value.(string); ok && len(valueString) > 0 {
    53  						textProperties[property] = valueString
    54  					}
    55  				}
    56  			}
    57  
    58  			summaryList := []ent.SummaryResult{}
    59  
    60  			// for each text property result, call the SUM function and add to additional result
    61  			for property, value := range textProperties {
    62  				summary, err := p.sum.GetSummary(ctx, property, value)
    63  				if err != nil {
    64  					return in, err
    65  				}
    66  
    67  				summaryList = append(summaryList, summary...)
    68  			}
    69  
    70  			ap["summary"] = summaryList
    71  
    72  			in[i].AdditionalProperties = ap
    73  		}
    74  	}
    75  	return in, nil
    76  }
    77  
    78  func (p *SummaryProvider) containsProperty(property string, properties []string) bool {
    79  	if len(properties) == 0 {
    80  		return true
    81  	}
    82  	for i := range properties {
    83  		if properties[i] == property {
    84  			return true
    85  		}
    86  	}
    87  	return false
    88  }