Can the `preset` field be embedded in a search API...
# community-help
g
Can the
preset
field be embedded in a search API key? I tried and it seems other parameters are properly taken into account by Typesense, but defining a
preset
(which contains the
query_by
for the collection) in my API key does not seem to work. Typesense reports that no search fields are in the query. If I set the
preset
field on the query (not the API key), it works as expected.
k
preset
field should be embeddable, just like any other search parameter. Can you produce a small reproducible example that shows the issue?
g
@Kishore Nallan do you have a demo project that showcase scoped API keys? I notice that I didn't mention the issue is with scoped keys. Basically, what I do is: 1. generate a scoped key with preset embedded. The key embed the
query_by
and
collection
params, along with an
exclude_fields
2. send the key to the front end 3. in the front end, do a multisearch with the
preset
and
q
parameters.
This is an example of query I send in my multisearch:
Copy code
{
  "preset": "companies-search-preset",
  "q": "sales",
  "page": 1,
  "per_page": 10
}
This is the preset:
Copy code
{
  "collection": "companies",
  "query_by": "name,thematics.label,segments.label"
}
This is how I generate the key:
Copy code
{
      preset: `companies-search-preset`,
      exclude_fields: ["restricted_users"],
    }
k
Post the full code for generating the scoped api key.
g
I'll send it, please wait a minute 🙂
This is the service that generate the key. I generate multiple keys at once, but I included only one for the demo. This is the error I get:
Copy code
{
  "0": {
    "code": 400,
    "error": "No search fields specified for the query."
  }
}
This is the whole query sent to typesense. I included the api keys in case you want to inspect them. They are derived from a local setup of typesense, so no security risk.
k
I just tried it out and it works fine for me. I created a scoped api key with a
preset
embedded. Then I created a preset with the same name that had a query_by field definiton. Then I hit the multi_search API with the scoped API key in one of the search parameters and search works fine without errors.
Can you please provide a stand-alone node example that does the same: 1. Create collection, index 1 document 2. Create preset with
query_by
param 3. Create a scoped api key with the preset embedded 4. Try querying that collection with the scoped api key
g
Sure, would you have a demo / sample project I could quickly modify to integrate the change ? 🙂
k
g
Hi @Kishore Nallan After further investigations, the problem was that I used the admin API key and not a search-only API key locally (only in dev env). This led the scoped keys to be invalid, and thus the preset ignored, but only locally.
Thanks for your time and support 🙂
k
Good to know!
Welcome.