Hi Joan

If you can make the query run completely from the perspective of Model B (so, 
any fields and attributes that you have in your Model A index should be copied 
over to Model B’s, with relevant association adjustments), and then it’s a 
single search query. And if you use an :include within the :sql option, you can 
ensure eager loading of Model A records:

  ModelB.search ‘foo’, :sql => {:include => :model_a} # and other options 
you’re already using

http://pat.github.io/thinking-sphinx/searching.html#advanced 
<http://pat.github.io/thinking-sphinx/searching.html#advanced>

Cheers

— 
Pat

> On 9 Jun 2015, at 1:24 am, [email protected] wrote:
> 
> Hi Pat,
> 
> Thanks for your answer. I'll have to apply your trick. I will run a search on 
> model B and then use the results to filter the search on model A. Now time 
> for me to look into how to run subqueries in TS...
> 
> Cheers,
> Joan
> 
> 
> On Monday, June 8, 2015 at 8:37:58 AM UTC+2, Pat Allan wrote:
> I’m afraid what you’re after isn’t quite possible as you’re hoping - Sphinx 
> has no concept of paired attribute values, or hashes, so there’s no 
> connection between particular items in one array of integers and another.
> 
> If it can suit what you’re after, I’d switch to searching on Model B instead, 
> creating an index for it that brings across all relevant data from the 
> associated Model A instances, and then you could group by Model A’s primary 
> key as an attribute (when searching) to limit results to just one per A 
> record. Then you can filter on specific values for attribute1 and attribute2 
> which are single values from the perspective of Model B records in Sphinx.
> 
> Would that do the trick?
> 
> — 
> Pat
> 
>> On 4 Jun 2015, at 8:33 pm, folk...@ <>gmail.com <http://gmail.com/> wrote:
>> 
>> Hi,
>> 
>> I am unsuccessfully trying to apply several AND filters on different 
>> attributes of a join table. In other words, I want to ensure that all 
>> attribute conditions are met by a single row of the join table. 
>> Unfortunately, Thinking Sphinx uses GROUP_CONCAT to include associations in 
>> the index. Therefore, TS evaluates attribute conditions one at a time on the 
>> overall join table. 
>> 
>> I am searching on Model A, which has many model B associated. Model B has 
>> attribute1 and attribute2.
>> I want to filter attribute1 < 10 and attribute2 > 30 at a row level.
>> 
>> I have the following Model A data: [id: 1, modelb: {{attribute1: 5, 
>> attribute2: 20}, {attribute1: 15, attribute2: 40}}]
>> 
>> I create a TS index on model A:
>> has modelb(:attribute1), :as => :attribute1
>> has modelb(:attribute2), :as => :attribute2
>> 
>> This index will convert my data into something like: [id: 1, attribute1: {5, 
>> 15}, attribute2: {20, 40}]
>> 
>> Therefore, when I execute the search with filters "attribute1 < 10 AND 
>> attribute2 > 30", TS will incorrectly return model A.
>> 
>> The question is: how can I tell Sphinx to evaluate several AND conditions on 
>> the join table, ensuring that the conditions are fulfilled by a single row 
>> of the join table?
>> 
>> Thanks!
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Thinking Sphinx" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to thinking-sphi...@ <>googlegroups.com <http://googlegroups.com/>.
>> To post to this group, send email to thinkin...@ <>googlegroups. 
>> <http://googlegroups.com/>com <http://googlegroups.com/>.
>> Visit this group at http://groups.google.com/group/thinking-sphinx 
>> <http://groups.google.com/group/thinking-sphinx>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/thinking-sphinx 
> <http://groups.google.com/group/thinking-sphinx>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/d/optout.

Reply via email to