How to give preference to a particular index while searching through multiple indices in thinking sphinx

27 Views Asked by At

I am using Thinking Sphinx with rails 5.2.2. I need to give preference to data of one index than the other, while searching through multiple indices, in the final output. For example I have an index index1 with an attribute attr = foo and an other index index2 with an attribute attr = foo. What I want is, ThinkingSphinx.search should always return data from index2 if attr matches foo(or some other value). If there is no record with attr matching foo in index2 records from index1 should be returned. Note: I need to group the result on attr

Here is an example of a query I am using.

ThinkingSphinx.search("@attr =foo|foo*", group_by: :attr, middleware: ThinkingSphinx::Middlewares::RAW_ONLY, indices: ['index1', 'index2'])

According to this documentation(Searching Multiple Indexes) data from later index (index2 in this case) should be returned. But some records are being returned from index1 and some are being returned from index2.

1

There are 1 best solutions below

0
pat On

Sphinx doesn't have a direct concept of prioritising one index over another, but you could add an attribute to each model to indicate importance, and then sort by that?

In the index definition:

# SQL-backed indices:
has "1", as: :priority

# Or, real-time indices:
has priority
# And add a method to each model returning an appropriate integer

And then searching:

ThinkingSphinx.search(
  "@attr =foo|foo*",
  group_by: :attr,
  middleware: ThinkingSphinx::Middlewares::RAW_ONLY,
  indices: ['index1', 'index2'],
  select: '*, weight() as w',
  order: 'priority ASC, w DESC'
)