Two searches in one roundtrip?

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. 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. Hey, that's awesome. Can't believe I didn't stumble across this when googling.

    Thanks, Pat!

  3. 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.

  4. roger closed this discussion on 03 May, 2022 08:47 PM.

Comments are currently closed for this discussion. You can start a new one.

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

Recent Discussions