@@ -16735,7 +16735,179 @@ func SetDatastoreNGramItem(ctx context.Context, key string, ngramItem *NGramItem
1673516735 return nil
1673616736}
1673716737
16738+ func GetDatastoreNgramItems (ctx context.Context , orgId , searchKey string , maxAmount int ) ([]NGramItem , error ) {
16739+ var items []NGramItem
16740+ var err error
16741+
16742+ nameKey := "datastore_ngram"
16743+ if project .DbType == "opensearch" {
16744+ var buf bytes.Buffer
16745+
16746+ query := map [string ]interface {}{
16747+ "size" : maxAmount ,
16748+ "query" : map [string ]interface {}{
16749+ "bool" : map [string ]interface {}{
16750+ "must" : []map [string ]interface {}{
16751+ map [string ]interface {}{
16752+ "match" : map [string ]interface {}{
16753+ "org_id" : orgId ,
16754+ },
16755+ },
16756+ map [string ]interface {}{
16757+ "match" : map [string ]interface {}{
16758+ "ref" : searchKey ,
16759+ },
16760+ },
16761+ },
16762+ },
16763+ },
16764+ }
16765+
16766+ if err := json .NewEncoder (& buf ).Encode (query ); err != nil {
16767+ log .Printf ("[WARNING] Error encoding find user query: %s" , err )
16768+ return items , err
16769+ }
16770+
16771+ resp , err := project .Es .Search (ctx , & opensearchapi.SearchReq {
16772+ Indices : []string {strings .ToLower (GetESIndexPrefix (nameKey ))},
16773+ Body : & buf ,
16774+ Params : opensearchapi.SearchParams {
16775+ TrackTotalHits : true ,
16776+ },
16777+ })
16778+ if err != nil {
16779+ if strings .Contains (err .Error (), "index_not_found_exception" ) {
16780+ return items , nil
16781+ }
16782+
16783+ log .Printf ("[ERROR] Error getting response from Opensearch (get ngram items): %s" , err )
16784+ return items , err
16785+ }
16786+
16787+ res := resp .Inspect ().Response
16788+ defer res .Body .Close ()
16789+ if res .StatusCode == 404 {
16790+ return items , nil
16791+ }
16792+
16793+ if res .IsError () {
16794+ var e map [string ]interface {}
16795+ if err := json .NewDecoder (res .Body ).Decode (& e ); err != nil {
16796+ log .Printf ("[WARNING] Error parsing the response body: %s" , err )
16797+ return items , err
16798+ } else {
16799+ // Print the response status and error information.
16800+ log .Printf ("[%s] %s: %s" ,
16801+ res .Status (),
16802+ e ["error" ].(map [string ]interface {})["type" ],
16803+ e ["error" ].(map [string ]interface {})["reason" ],
16804+ )
16805+ }
16806+ }
16807+
16808+ if res .StatusCode != 200 && res .StatusCode != 201 {
16809+ return items , errors .New (fmt .Sprintf ("Bad statuscode: %d" , res .StatusCode ))
16810+ }
16811+
16812+ respBody , err := ioutil .ReadAll (res .Body )
16813+ if err != nil {
16814+ return items , err
16815+ }
16816+
16817+ wrapped := NGramSearchWrapper {}
16818+ err = json .Unmarshal (respBody , & wrapped )
16819+ if err != nil {
16820+ return items , err
16821+ }
16822+
16823+ //log.Printf("Found items: %d", len(wrapped.Hits.Hits))
16824+ for _ , hit := range wrapped .Hits .Hits {
16825+ if hit .Source .Key == "" {
16826+ continue
16827+ }
16828+
16829+ if hit .Source .OrgId == orgId {
16830+ items = append (items , hit .Source )
16831+ }
16832+ }
16833+
16834+ } else {
16835+
16836+ if len (orgId ) == 0 {
16837+ return items , errors .New ("No org to find ngrams for found" )
16838+ }
16839+
16840+ cursorStr := ""
16841+ query := datastore .NewQuery (nameKey ).Filter ("OrgId =" , orgId ).Filter ("Ref =" , searchKey ).Limit (maxAmount )
16842+ for {
16843+ it := project .Dbclient .Run (ctx , query )
16844+ if len (items ) >= maxAmount {
16845+ break
16846+ }
16847+
16848+ for {
16849+ innerItem := NGramItem {}
16850+ _ , err = it .Next (& innerItem )
16851+ if err != nil {
16852+ if strings .Contains (fmt .Sprintf ("%s" , err ), "cannot load field" ) {
16853+
16854+ } else {
16855+ if ! strings .Contains (fmt .Sprintf ("%s" , err ), "no more items in iterator" ) {
16856+ log .Printf ("[WARNING] NGram iterator issue: %s" , err )
16857+ }
16858+
16859+ break
16860+ }
16861+ }
16862+
16863+ found := false
16864+ for _ , loopedItem := range items {
16865+ if loopedItem .Key == innerItem .Key {
16866+ found = true
16867+ break
16868+ }
16869+ }
16870+
16871+ if ! found {
16872+ items = append (items , innerItem )
16873+ }
16874+
16875+ if len (items ) >= maxAmount {
16876+ break
16877+ }
16878+ }
16879+
16880+ if err != iterator .Done {
16881+ log .Printf ("[INFO] Failed fetching ngrams: %v" , err )
16882+ break
16883+ }
16884+
16885+ // Get the cursor for the next page of results.
16886+ nextCursor , err := it .Cursor ()
16887+ if err != nil {
16888+ log .Printf ("[ERROR] Problem with cursor (ngram): %s" , err )
16889+ break
16890+ } else {
16891+ nextStr := fmt .Sprintf ("%s" , nextCursor )
16892+ if cursorStr == nextStr {
16893+ break
16894+ }
16895+
16896+ cursorStr = nextStr
16897+ query = query .Start (nextCursor )
16898+ }
16899+ }
16900+ }
16901+
16902+ if len (items ) > maxAmount {
16903+ items = items [:maxAmount ]
16904+ }
16905+
16906+ return items , nil
16907+ }
16908+
1673816909// Key itself contains the orgId so this should "just work"
16910+ // To get ALL items matching a key, use GetDatastoreNgramItems()
1673916911func GetDatastoreNGramItem (ctx context.Context , key string ) (* NGramItem , error ) {
1674016912
1674116913 nameKey := "datastore_ngram"
0 commit comments