Answer the question
In order to leave comments, you need to log in
How to index one to many tables on sphinx?
I have a one-to-many parent and child table.
parent
- id
- name
- age
child
- id
- parent_id
- name
- age
source mainSourse
{
sql_query = \
SELECT row_number() OVER () AS id, p.id as parent_id, p."name" as parent_name, \
p.age as parent_age, ch.id as child_id, ch.name as child_name, ch.age as child_age\
FROM parent p\
JOIN child ch ON ch.parent_id=p.id\
sql_field_string = parent_name
sql_field_string = child_name
sql_attr_uint = parent_age
sql_attr_uint = child_age
}
| id | parent_id | parent_name | parent_age | child_id | child_name | child_age |
| 1 | 1| a | 35| 1| aa| 2|
| 2 | 1| a | 35| 2| ab| 3|
| 3 | 1| a | 35| 3| ac| 4|
| 4 | 1| a | 35| 4| ad| 5|
Answer the question
In order to leave comments, you need to log in
I propose to look from the side of adjusting the architecture for a particular case.
The parent and child tables can be combined into one - family, having a structure similar to the child table.
- id
- parent_id
- name
- age Let
's get something like:
--------------------------------
id | parent_id | name | age
--------------------------------
1 | 0 | Father | 30
2 | 1 | Daughter | 9
3 | 1 | Daughter2 | 7
4 | 0 | Father2 | 41
5 | 4 | Daughter3 | 12
Then we will get three fields in the index itself:
- name
- age
- parent_id We are
looking for among parents:
WHERE parent_id = 0 AND MATCH('@name "Father"')
Search among children: WHERE
parent_id
> 0 AND MATCH('@name "Daughter"')
using the MySQL GROUP_CONCAT function using a delimiter to further access the array.
For example, when grouping by the parent_name field, you need to concatenate 3 fields - child_id, child_name, child_age:
SELECT \
row_number() OVER () AS id, \
p.id as parent_id, \
p."name" as parent_name, \
p.age as parent_age, \
GROUP_CONCAT(ch.id SEPARATOR '|') as child_id, \
GROUP_CONCAT(ch.name SEPARATOR '|') as child_name, \
GROUP_CONCAT(ch.age SEPARATOR '|') as child_age\
FROM parent p\
JOIN child ch ON ch.parent_id=p.id\
GROUP BY parent_id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question