M
M
Marat Dolotov2019-11-30 07:47:52
PostgreSQL
Marat Dolotov, 2019-11-30 07:47:52

How to implement facet search for related tables?

Let's say I have three tables, one parent and two children.

табл. (1)
 id | name
----+------
  1 | a
  2 | b
  3 | c
  4 | d

табл. (2)
branch_1
 id | parent_id | b1_name
----+-----------+---------
  1 |         1 | aaa
  2 |         1 | bbb
  3 |         1 | ccc
  4 |         2 | ddd
  5 |         2 | eee
  6 |         2 | fff
  7 |         2 | ggg
  8 |         2 | hhh

табл. (3)
branch_2
 id | b2_name | parent_id
----+---------+-----------
  1 | b2_aa   |         1
  2 | b2_ba   |         2
  3 | b2_bb   |         2
  4 | b2_bc   |         2

I'm trying to index them with this query in sphinx
select p.id, b1.b1_name, b2.b2_name
from parent p
left outer join lateral (select distinct b1.b1_name from branch_1 b1 where b1.parent_id=p.id order by b1.b1_name) as b1 on true
left outer join lateral (select distinct b2.b2_name from branch_2 b2 where b2.parent_id=p.id order by b2.b2_name) as b2 on true

I get this table
табл. (4)
id | b1_name | b2_name
----+---------+---------
  1 | aaa     | b2_aa
  1 | bbb     | b2_aa
  1 | ccc     | b2_aa
  2 | ddd     | b2_ba
  2 | ddd     | b2_bb
  2 | ddd     | b2_bc
  2 | eee     | b2_ba
  2 | eee     | b2_bb
  2 | eee     | b2_bc
  2 | fff     | b2_ba
  2 | fff     | b2_bb
  2 | fff     | b2_bc
  2 | ggg     | b2_ba
  2 | ggg     | b2_bb
  2 | ggg     | b2_bc
  2 | hhh     | b2_ba
  2 | hhh     | b2_bb
  2 | hhh     | b2_bc

And now with faceted search:
select * from facetIndex facet b1_name, b2_name
sphinx returns the wrong number of values, and all because the values ​​are repeated.
It would be ideal if the indexed table looked like this:
табл. (5)
 id | b1_name | b2_name
----+---------+---------
  1 | aaa     | b2_aa
  1 | bbb     | ---
  1 | ccc     | ---
  2 | ddd     | b2_ba
  2 | eee     | b2_bb
  2 | fff     | b2_bc
  2 | ggg     | ---
  2 | hhh     | ---

Then the facet search would return the correct number of values.
Question:
how to write the correct query for faceted search for an indexed table (Table 4)
or
how to rebuild tables (Table 1, Tab. 2, Tab. 3) in the form of tabs. (5)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Marat Dolotov, 2019-11-30
@bemulima

Rebuild tables (tab. 1, tab. 2, tab. 3) in tab. (5). Might be useful to someone.

select t.id, t.b1_name, b2.b2_name
from (
  select p.id, b1.b1_name, row_number() over (partition by p.id order by b1_name) as rn
  from parent p
    join branch_1 b1 on b1.parent_id = p.id
) t
  left join (
    select parent_id, b2_name, 
           row_number() over (partition by parent_id) as rn
    from branch_2 
  ) b2 on b2.parent_id = t.id and b2.rn = t.rn
order by t.id;

X
xmoonlight, 2019-11-30
@xmoonlight

People like you never cease to amaze me.
"The mice pricked and cried, but continued to eat the cactus" :))
You know how to do it right from scratch, but keep using Sphinx!
Why?!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question