@@ -92,6 +92,7 @@ type IndexClient struct {
9292 PIndexNames []string
9393 QueryURL string
9494 CountURL string
95+ InsightsURL string
9596 TaskRequestURL string
9697 Consistency * cbgt.ConsistencyParams
9798 httpClient cbgt.HTTPClient
@@ -427,6 +428,7 @@ func GroupIndexClientsByHostPort(clients []*IndexClient) (rv []*IndexClient, err
427428 IndexUUID : client .IndexUUID ,
428429 QueryURL : baseURL + "/query" ,
429430 CountURL : baseURL + "/count" ,
431+ InsightsURL : baseURL + "/insights" ,
430432 TaskRequestURL : baseURL + "/tasks" ,
431433 Consistency : client .Consistency ,
432434 httpClient : client .httpClient ,
@@ -516,3 +518,90 @@ func completeTaskStatus(req *cbgt.TaskRequest, err error,
516518 }
517519 return rv
518520}
521+
522+ func (r * IndexClient ) fetchInsight (
523+ insight string ,
524+ field string ,
525+ limit int ,
526+ descending bool ,
527+ out any ,
528+ ) error {
529+ u , err := UrlWithAuth (r .AuthType (), r .InsightsURL )
530+ if err != nil {
531+ return fmt .Errorf ("remote: insights auth for query, insightsURL: %s," +
532+ " authType: %s, err: %w" , r .InsightsURL , r .AuthType (), err )
533+ }
534+
535+ body , err := MarshalJSON (map [string ]any {
536+ "field" : field ,
537+ "insight" : insight ,
538+ "limit" : limit ,
539+ "descending" : descending ,
540+ "local-only" : true ,
541+ })
542+ if err != nil {
543+ return fmt .Errorf ("remote: insights marshal request, err: %w" , err )
544+ }
545+
546+ req , err := http .NewRequest ("POST" , u , bytes .NewReader (body ))
547+ if err != nil {
548+ return fmt .Errorf ("remote: insights new request, err: %w" , err )
549+ }
550+ req .Header .Set ("Content-Type" , "application/json" )
551+
552+ resp , err := r .httpClient .Do (req )
553+ if err != nil {
554+ return fmt .Errorf ("remote: insights http request, err: %w" , err )
555+ }
556+ defer resp .Body .Close ()
557+
558+ if resp .StatusCode != http .StatusOK {
559+ return fmt .Errorf ("remote: insights got status code %d, insightsURL: %s" ,
560+ resp .StatusCode , r .InsightsURL )
561+ }
562+
563+ data , err := io .ReadAll (resp .Body )
564+ if err != nil {
565+ return fmt .Errorf ("remote: insights read failure, err: %w" , err )
566+ }
567+
568+ if err := UnmarshalJSON (data , out ); err != nil {
569+ return fmt .Errorf ("remote: insights parse response, err: %w" , err )
570+ }
571+
572+ return nil
573+ }
574+
575+ func (r * IndexClient ) TermFrequencies (field string , limit int , descending bool ) (
576+ []index.TermFreq , error ) {
577+ var rv struct {
578+ Status string `json:"status"`
579+ TermFrequencies []index.TermFreq `json:"termFrequencies"`
580+ }
581+
582+ if err := r .fetchInsight (
583+ "termFrequencies" , field , limit , descending , & rv ); err != nil {
584+ return nil , err
585+ }
586+ if rv .Status != "ok" {
587+ return nil , fmt .Errorf ("remote: insights unsupported" )
588+ }
589+ return rv .TermFrequencies , nil
590+ }
591+
592+ func (r * IndexClient ) CentroidCardinalities (field string , limit int , descending bool ) (
593+ []index.CentroidCardinality , error ) {
594+ var rv struct {
595+ Status string `json:"status"`
596+ CentroidCardinalities []index.CentroidCardinality `json:"centroidCardinalities"`
597+ }
598+
599+ if err := r .fetchInsight (
600+ "centroidCardinalities" , field , limit , descending , & rv ); err != nil {
601+ return nil , err
602+ }
603+ if rv .Status != "ok" {
604+ return nil , fmt .Errorf ("remote: insights unsupported" )
605+ }
606+ return rv .CentroidCardinalities , nil
607+ }
0 commit comments