Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose result or errors on search #6

Open
sobrinho opened this issue Jan 31, 2024 · 3 comments
Open

Expose result or errors on search #6

sobrinho opened this issue Jan 31, 2024 · 3 comments

Comments

@sobrinho
Copy link

Hi there!

Would be nice to have request failures exposed, for instance:

result = SomeIndex.search(query: 'something', query_by: 'typo_here')
=> #<Typesensual::Search::Results:0x00007f721ca676c0 @results={"code"=>404, "error"=>"Could not find a field named `typo_here` in the schema."}>

result.hits
Traceback (most recent call last):
        1: from (irb):51
NoMethodError (undefined method `map' for nil:NilClass)

result.results
Traceback (most recent call last):
        1: from (irb):54
NoMethodError (undefined method `results' for #<Typesensual::Search::Results:0x00007f721ca676c0>)

I had to workaround that with:

      if result.count.nil?
        raise RequestFailed, result.instance_variable_get(:@results).inspect
      end
@NuckChorris
Copy link
Member

Damn you're right, I assumed the Typesense client would raise for these, I'll add that 👍

@NuckChorris
Copy link
Member

Hmm, looking closer, it seems the Typesense client is supposed to raise for these (in fact, we even test that behavior on loading a collection!)

I was unable to reproduce on the single-search endpoint, but I was able to reproduce with a multi-search like so:

client.collections.create(
  name: 'test',
  fields: [{ name: 'foo', type: 'string' }]
)
# Do an invalid multi search
client.multi_search.perform({ searches: [{ collection: 'test', q: 'query', query_by: 'bar' }] })
# => {"results"=>[{"code"=>404, "error"=>"Could not find a field named `bar` in the schema."}]}

This sorta makes sense, given that it allows you to return a partial result set, but you seem to be using the single-search endpoint, so I'm not sure why you're not getting an Exception 🤔 If you can get a reproducible example with the latest Typesense gem and an unmodified Typesensual, let me know and I can send it upstream for them to fix. It could also be a version of Typesense itself which fails to return the right HTTP status code, tbh.

All that being said, there's no reason Typesensual can't provide a better API for this. I've got an idea to structure the response as 'foo' => Error, 'bar' => Results for multi-search (allowing code to just check which type it got) and (building on my fix for #5) raise if there's an Error on single-search

@NuckChorris
Copy link
Member

Just released 1.0.0 with the change to how multi-search handles exceptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants