如何在 sphinx 中对多面体的 MVA 字段进行分组

How to group MVA field for faceted in sphinx

我有一个索引,其中一些数据重复,所有字段都相似,除了纬度、经度和 ID(字段 ID 不是真正的 ID,刚刚生成 row_number() OVER () AS id)。

示例:

mysql> select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy;
+------+------------+---------------+----------+-----------+
| id   | vacancy_id | prof_area_ids | latitude | longitude |
+------+------------+---------------+----------+-----------+
|    1 |        917 | 11,199,202    | 0.973178 |  0.743566 |
|    2 |        916 | 17,283,288    | 0.973178 |  0.743566 |
|    3 |        915 | 17,288        | 0.973178 |  0.743566 |
|    4 |        914 | 30,482        | 0.973178 |  0.743566 |
|    5 |        919 | 15,243        | 0.825153 |  0.692837 |
|    6 |        919 | 15,243        | 0.825162 |  0.692828 |
|    7 |        918 | 8,154         | 0.825153 |  0.692837 |
|    8 |        918 | 8,154         | 0.825162 |  0.692828 |
|    9 |        920 | 17,283,288    | 0.958914 |  1.282161 |
|   10 |        920 | 17,283,288    | 0.958915 |  1.282215 |
|   11 |        924 | 12,208        |  0.97333 |  0.658246 |
|   12 |        924 | 12,208        | 0.973336 |  0.658237 |
|   13 |        923 | 21,365        |  0.97333 |  0.658246 |
|   14 |        923 | 21,365        | 0.973336 |  0.658237 |
|   15 |        922 | 20,359        |  0.97333 |  0.658246 |
|   16 |        922 | 20,359        | 0.973336 |  0.658237 |
|   17 |        921 | 19,346        |  0.97333 |  0.658246 |
|   18 |        921 | 19,346        | 0.973336 |  0.658237 |
|   19 |        926 | 12,17,208,292 |  0.88396 |  2.389868 |
|   20 |        925 | 12,208        |  0.88396 |  2.389868 |
+------+------------+---------------+----------+-----------+
20 rows in set (0.00 sec)

现在我想按 vacancy_id

对数据进行分组
mysql> select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy group by vacancy_id;
+------+------------+---------------+----------+-----------+
| id   | vacancy_id | prof_area_ids | latitude | longitude |
+------+------------+---------------+----------+-----------+
|    1 |        917 | 11,199,202    | 0.973178 |  0.743566 |
|    2 |        916 | 17,283,288    | 0.973178 |  0.743566 |
|    3 |        915 | 17,288        | 0.973178 |  0.743566 |
|    4 |        914 | 30,482        | 0.973178 |  0.743566 |
|    5 |        919 | 15,243        | 0.825153 |  0.692837 |
|    7 |        918 | 8,154         | 0.825153 |  0.692837 |
|    9 |        920 | 17,283,288    | 0.958914 |  1.282161 |
|   11 |        924 | 12,208        |  0.97333 |  0.658246 |
|   13 |        923 | 21,365        |  0.97333 |  0.658246 |
|   15 |        922 | 20,359        |  0.97333 |  0.658246 |
|   17 |        921 | 19,346        |  0.97333 |  0.658246 |
|   19 |        926 | 12,17,208,292 |  0.88396 |  2.389868 |
|   20 |        925 | 12,208        |  0.88396 |  2.389868 |
|   21 |        961 | 4,105         | 0.959217 |  1.280721 |
|   23 |        960 | 8,155         | 0.959217 |  1.280721 |
|   25 |        959 | 12,208        | 0.959217 |  1.280721 |
|   27 |        928 | 1,60          | 0.963734 |  1.070297 |
|   29 |        927 | 32,513        | 0.963734 |  1.070297 |
|   31 |        929 | 6,140         | 0.786553 |  0.678649 |
|   33 |        932 | 1,40,46       | 0.824627 |  0.694182 |
+------+------------+---------------+----------+-----------+
20 rows in set (0.00 sec)

结果太棒了!但是当我想使用 faceted

获取所有分组数据时,问题就开始了
mysql> select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy where prof_area_ids=199 group by vacancy_id facet prof_area_ids;
+------+------------+-----------------+----------+-----------+
| id   | vacancy_id | prof_area_ids   | latitude | longitude |
+------+------------+-----------------+----------+-----------+
|    1 |        917 | 11,199,202      | 0.973178 |  0.743566 |
|  191 |       1004 | 11,196,199      | 0.925335 |  2.768874 |
|  313 |       1072 | 1,11,60,197,199 | 0.963968 |  1.070624 |
|  318 |       1136 | 11,196,199      |  0.96071 |  1.448998 |
|  374 |       1097 | 11,199          | 0.785255 |  0.678504 |
+------+------------+-----------------+----------+-----------+
5 rows in set (0.00 sec)

+---------------+----------+
| prof_area_ids | count(*) |
+---------------+----------+
|           202 |        1 |
|           199 |       12 |
|            11 |       12 |
|           196 |        5 |
|           197 |        3 |
|            60 |        3 |
|             1 |        3 |
+---------------+----------+
7 rows in set (0.02 sec)

分面结果不正确。因为实际上 prof_area_ids=199 处的数据计数必须是 5 而不是 12。那么我如何为分面分组字段?

另外

我在这里 http://sphinxsearch.com/blog/2013/06/21/faceted-search-with-sphinx/ 但只是写了“如果你有一个 MVA 方面,你需要使用 GROUPBY() 函数,returns 进行分组的实际值。”没有考试。

mysql> select id,vacancy_id,prof_area_ids,latitude,longitude,GROUPBY() as selected,COUNT(*) from jobVacancy where prof_area_ids=199 group by vacancy_id facet prof_area_ids;
+------+------------+-----------------+----------+-----------+----------+----------+
| id   | vacancy_id | prof_area_ids   | latitude | longitude | selected | count(*) |
+------+------------+-----------------+----------+-----------+----------+----------+
|    1 |        917 | 11,199,202      | 0.973178 |  0.743566 |      917 |        1 |
|  191 |       1004 | 11,196,199      | 0.925335 |  2.768874 |     1004 |        2 |
|  313 |       1072 | 1,11,60,197,199 | 0.963968 |  1.070624 |     1072 |        3 |
|  318 |       1136 | 11,196,199      |  0.96071 |  1.448998 |     1136 |        3 |
|  374 |       1097 | 11,199          | 0.785255 |  0.678504 |     1097 |        3 |
+------+------------+-----------------+----------+-----------+----------+----------+
5 rows in set (0.00 sec)

+---------------+----------+
| prof_area_ids | count(*) |
+---------------+----------+
|           202 |        1 |
|           199 |       12 |
|            11 |       12 |
|           196 |        5 |
|           197 |        3 |
|            60 |        3 |
|             1 |        3 |
+---------------+----------+
7 rows in set (0.02 sec)

分面结果也是错误的。

Faceted result is incorrect. Because in fact data's count where prof_area_ids=199 must be 5 and not 12. So how I can group field for faceted?

您似乎误解了 FACET 的工作原理。在我看来,您认为它以主查询的结果为基础,但实际上它只是进行了另一个分组。例如。这里:

mysql> select g, t from idx_mva where t = 11 group by g facet t;
+------+----------+
| g    | t        |
+------+----------+
|    1 | 11,12    |
|    2 | 11,13,15 |
|    3 | 9,11     |
|    5 | 11,12,15 |
+------+----------+
4 rows in set (0.00 sec)

+------+----------+
| t    | count(*) |
+------+----------+
|   12 |        2 |
|   11 |        6 |
|   15 |        4 |
|   13 |        1 |
|    9 |        1 |
|    3 |        1 |
+------+----------+
6 rows in set (0.00 sec)

对于 t=11 你可以看到,在你的情况下,它在第一个查询的结果中找到了 3 次,但在 FACET 的查询结果中它的计数是 6 次。这是因为它在索引中实际出现了6次:

mysql> select * from idx_mva where t = 11;
+------+------+----------+
| id   | g    | t        |
+------+------+----------+
|    2 |    1 | 11,12    |
|    3 |    1 | 11,15    |
|    3 |    2 | 11,13,15 |
|    6 |    3 | 9,11     |
|    8 |    5 | 11,12,15 |
|   11 |    2 | 3,11,15  |
+------+------+----------+
6 rows in set, 1 warning (0.00 sec)

在第一种情况下它发生了 3 次,只是因为每个组只返回一次 t 的值。您可以使用 group_concat() 查看同一组中的更多值:

mysql> select g, group_concat(to_string(t)) from idx_mva where t = 11 group by g facet t;
+------+----------------------------+
| g    | group_concat(to_string(t)) |
+------+----------------------------+
|    1 | 11,12,11,15                |
|    2 | 11,13,15,3,11,15           |
|    3 | 9,11                       |
|    5 | 11,12,15                   |
+------+----------------------------+
4 rows in set (0.00 sec)

+------+----------+
| t    | count(*) |
+------+----------+
|   12 |        2 |
|   11 |        6 |
|   15 |        4 |
|   13 |        1 |
|    9 |        1 |
|    3 |        1 |
+------+----------+
6 rows in set (0.00 sec)

如果你想了解更多关于分面的知识,这里有一个互动课程 - https://play.manticoresearch.com/faceting/

似乎,想要在 FACET 上有效地 COUNT(DISTINCT vacancy_id) 而不是默认的 COUNT(*),但结果是

... FACET prof_area_ids,COUNT(DISTINCT vacancy_id) AS vacancies BY prof_area_ids

不起作用。 BY 之前的位只支持属性,不支持自定义函数。

...只需要写很长的路,用完整的查询...

select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy
    where prof_area_ids=199 group by vacancy_id;

SELECT GROUPBY() AS prof_area_id, COUNT(DISTINCT vacancy_id) FROM jobVacancy
    WHERE prof_area_ids=199 GROUP BY prof_area_id;

相同的结果,只是稍微更冗长。 ie 而不是使用 FACET shorthand,写它 完整地作为多个单独的查询。