Two searches in one roundtrip?

Roger K. Kristiansen's Avatar

Roger K. Kristiansen

20 Mar, 2018 11:55 AM

Hi,

I'm not really expecting a positive answer for this one, but just in case a solution happens to exist:

To reduce load times, I'm wondering if there might be a way of performing two separate searches in one round trip?

Background

I'm working on adding some extra ad entries to the top of our search results, somewhat like Google does, and these should actually come from a subset of the data we are already searching. I'm currently fetching the ad results using an extra ThinkingSphinx.search call, on the same index, with some additional filtering to find the "advertisable" entries.

  1. Support Staff 1 Posted by Pat Allan on 20 Mar, 2018 12:37 PM

    Pat Allan's Avatar

    Hi Roger,

    It is indeed possible to send two search queries in a single call to Sphinx. Presuming you're using TS v3, this syntax should do the trick:

    batch = ThinkingSphinx::BatchedSearch.new
    batch.searches << Model.search("foo")
    batch.searches << Model.search("bar")
    batch.populate
    batch.searches.each do |search|
      # ...
    end
    

    Keep in mind that if you're testing this in a Rails console, the inspection of a search results set populates the data immediately, which would make this fail. An easy way around this is to add ; "" at the end of lines that involve search calls. e.g.:

    batch.searches << Model.search("foo"); ""
    

    All of that said… if you need the values from the first search to be used in the second, I'm afraid that's not possible in Sphinx.

  2. 2 Posted by Roger K. Kristi... on 20 Mar, 2018 12:48 PM

    Roger K. Kristiansen's Avatar

    Hey, that's awesome. Can't believe I didn't stumble across this when googling.

    Thanks, Pat!

  3. Support Staff 3 Posted by Pat Allan on 20 Mar, 2018 01:09 PM

    Pat Allan's Avatar

    Mostly it's not documented, and it's rarely requested, so it would have taken quite some googling!

    Though I've realised it's not in the documentation at all - and I've been working on that for the upcoming v4 release, so I'll make sure it gets added shortly.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac