I want to implement an auction house function, where players can list equipment with different attributes. Players can filter and search based on the attributes of the equipment. Can I index the attributes of each equipment in the db?
Hello @hexun80149128,
Have a look at Nakama storage search.
There are examples of arrays here. Is there an example of map? @sesposito
I found SELECT(â{âfooâ:1, âbarâ:2}â::JSONB?&array[âfooâ,âbarâ]); can workă I wonder the
query code examaple.Thanks
Iâm confused, your snippet looks like SQL which is not how the query syntax for the storage engine search works, can you elaborate on what the data youâll be indexing looks like?
the json is like {âitemâ:{âconfig_idâ:100029,âentryâ:{â405â:333,â406â:509,â408â:95,â409â:90},ânumâ:1},âpriceâ:5}. how can i write query to seach as entry has 405 key and 408 key?.thanks @sesposito
Assuming youâve set up the index correctly, the query would look something like:
+value.item.entry.405>0 +value.item.entry.408>0
thanksăI will try
if err := initializer.RegisterStorageIndex(logic.STORAGE_AUCTION_INDEX_NAME, logic.STORAGE_AUCTION_COLLECTION_NAME,
logic.STORAGE_AUCTION_INDEX_KEY_NAME, []string{"item.entry"}, 1000, false); err != nil {
logger.Error("Unable to RegisterStorageIndex: %v", err)
return err
}
Is this rightďźand I want to know what the last parameter indexOnly true or false means respectively. Thanks
[]string{"item.entry"}
You can only index top-level keys. If you set it to just item
itâll index the whole object under that key, and the query should work.
@sesposito Sorry to bother you again.I created an index and inserted a piece of data. But StorageIndexList does not return results. What is the reasonă
if err := initializer.RegisterStorageIndex("auctionIndex", "auction",
"value", []string{"Item.Entry"}, 100000, false); err != nil {
logger.Error("Unable to RegisterStorageIndex: %v", err)
return err
}
and the log is
{"level":"info","ts":"2023-12-13T06:30:41.702Z","caller":"server/storage_index.go:630","msg":"Initialized storage engine index","configuration":{"collection":"auction","fields":["Item.Entry"],"index_only":false,"key":"value","max_entries":100000,"name":"auctionIndex"}} {"level":"info","ts":"2023-12-13T06:30:41.711Z","caller":"server/storage_index.go:325","msg":"Storage index loaded.","config":{"Name":"auctionIndex","MaxEntries":100000,"Collection":"auction","Key":"value","Fields":["Item.Entry"],"IndexOnly":false,"Index":{}},"elapsed_time_ms":2}
I think the index is created successă
then insert dataďźsuch as
Then I write simple code to search.
indexName := "auctionIndex"
query := "*"
limit := 10
result, err := nk.StorageIndexList(ctx, "", indexName, query, limit)
logger.Info("GetAuctionItemList", result, err)
but the log is "GetAuctionItemList%!(EXTRA *api.StorageObjects=, <nil>)"
It return nothing. thanks for help
another question @sesposito
A player lists items for auction. For example, if a player lists 2 items, I want to generate 2 pieces of data, but I found that StorageWrite can only write one piece of data per player. What is a good recommended method? Thanks
and query = â+value.Item.Entry.501>0 +value.Item.Entry.502>0â also return nothing
I think I wasnât clear - []string{"Item.Entry"}
nesting when setting the fields does not work, it has to be top level, so if your json contains a top level key called âitemâ, you need to set fields as []string{"Item"}
The key
field (youâve set it to âvalueâ) should be the storage object key
âs you wish to be considered for indexing, so with your current configuration, only the objects stored in Collection âauctionâ with Key âvalueâ will be considered. You can also set the key param to ââ so all the keys in that collection are indexed.
Hope this clarifies.
I want to know what the last parameter indexOnly true or false means respectively. Thanks
If indexOnly
is false, then the lookups will be done in the index first, and then if there are matches the corresponding storage objects will be fetched from the DB and returned.
If indexOnly
is true, then all the storage object data will be stored in the index when writes happen (uses more memory), and the lookups will never go through the DB at all.
after I change
initializer.RegisterStorageIndex("auctionIndex", "auction", "", []string{"Item"}, 100000, false)
,the
query := "*"
works. but
query = â+value.Item.Entry.500>0"
not works.
but the sql can find objects
after I change key to value,
initializer.RegisterStorageIndex("auctionIndex", "auction", "value", []string{"Item"}, 100000, false)
,
both query did not work.
@sesposito can you help me?thanks.
@sesposito and this also need your help.thanks.
@sesposito since I see Heroic Labs Documentation | Query Syntax slovedăneed addďź
â+value.Item.Entry.500:>0" will work.
another question, the query-syntax how to implement page turning functionďźthanks
Can I use jsnb such as {âuseridâ:xxx,âauctionidâ:yyy} to store it under user_id? Any better recommendations? @sesposito thanks
Iâm sorry I did not understand the question
@sesposito Since there are 1000 auction itemsďźStorageIndexList max return 100 itemsďźso I need query-syntax to return another items not 100 oldă