java - Sphinx查询从mysql转换

标签 java mysql sql sphinx

我有一个具有 boolean 搜索模式和两个联接的有效 SQL 查询,需要重写它以与 sphinx 中的索引查询配合使用,以获得更好的性能。

sphinx配置中的sql查询:

 sql_query     = \
      SELECT id, title, description \
      FROM post

用于搜索的 SQL 查询:

 Select distinct p.id, 
     MATCH (title, description) AGAINST ('polopet*' IN BOOLEAN MODE ) AS score , 
     SQRT( POW(69.1 * (l.Latitude - 48.711982), 2) + POW(69.1 * (2.677415 - l.Longitude) * COS(l.Latitude / 57.3), 2)) AS distance 
 FROM post p 
     join location l on (l.id = p.location_id and acos(sin(l.Latitude * 0.0175) * sin(48.711982 * 0.0175) + cos(l.Latitude * 0.0175) * cos(48.711982 * 0.0175) * cos((2.677415 * 0.0175) - (l.Longitude * 0.0175))) * 3959 <= 31.5) 
     right join post_category as pc on ( p.id = (
         select distinct post_id 
             from post_category as pc 
             where pc.post_category in ("BUSINESS_SERVICES") and post_id=p.id
     )) 
 ORDER BY score Desc 
 LIMIT 30 OFFSET 0 ;

有人可以帮助我吗?

最佳答案

这可能不是您任务的完整解决方案,但应该是一个好的开始:

配置:

[snikolaev@dev01 ~]$ cat sphinx_49576229.conf
source min
{
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass =
    sql_db = test
    sql_query = select  p.id,  p.title, p.description, l.Latitude, l.Longitude FROM post p join location l on l.id = p.location_id
    sql_attr_multi = uint post_category_id from query; SELECT post_id, id from post_category
    sql_attr_float = Latitude
    sql_attr_float = Longitude
}

index idx_min
{
    path = idx_min
    source = min
    min_prefix_len = 3
}

searchd
{
    listen                       = 9314:mysql41
        log = sphinx_min.log
        pid_file = sphinx_min.pid
    binlog_path = binlog
}

索引:

[snikolaev@dev01 ~]$ indexer -c sphinx_49576229.conf --all --rotate
Manticore 2.6.1 9a706b4@180119 dev
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
Copyright (c) 2017-2018, Manticore Software LTD (http://manticoresearch.com)

using config file 'sphinx_49576229.conf'...
indexing index 'idx_min'...
collected 1 docs, 0.0 MB
collected 2 attr values
sorted 0.0 Mvalues, 100.0% done
sorted 0.0 Mhits, 100.0% done
total 1 docs, 16 bytes
total 0.005 sec, 3152 bytes/sec, 197.00 docs/sec
total 6 reads, 0.000 sec, 10.6 kb/call avg, 0.0 msec/call avg
total 14 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
rotating indices: successfully sent SIGHUP to searchd (pid=21241).

搜索:

[snikolaev@dev01 ~]$ mysql -P9314 -h0
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 2.6.3 5bc9e81@180316 dev

Copyright (c) 2009-2017 Percona LLC and/or its affiliates
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select *, geodist(48.712002, 2.677411, latitude, longitude) dist from idx_min where match('desc*') and dist < 20 and ANY(post_category_id) in (1);
+------+-----------+-----------+------------------+-----------+
| id   | latitude  | longitude | post_category_id | dist      |
+------+-----------+-----------+------------------+-----------+
|    1 | 48.712002 |  2.677410 | 1,2              | 11.099242 |
+------+-----------+-----------+------------------+-----------+
1 row in set (0.00 sec)

关于java - Sphinx查询从mysql转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49576229/

相关文章:

MySQL 分组并合并 JSON 值

mysql - 选择与另一列的最大/最小值相对应的列(无连接)

java - Android - 如何在 Android 中的 Expandable ListView 的 subview 下添加另一个 View ?

java - 解码字符串有多少种方法?

mysql - 连接两个表 SQL

mysql - 在具有 160M+ 行的 MySQL InnoDB 表上,选择查询非常慢

php - 用于从 SET 列中获取最常用项目的 SQL 查询

java - 什么限制了 Java 应用程序中创建的线程数?

java - 如何使用 ByteBuddy 子类化具有 1+ args 构造函数的抽象类

php - 我想用 Eloquent laravel 5 将人们添加到组中