Sphinx正确匹配json数组与所有值

时间:2014-11-18 19:16:18

标签: php json search sphinx sphinxql

我试图匹配包含所有给定元素的json数组的行。

搜索项目示例:

['gym', 'sofa']

匹配的预期行:

['gym', 'sofa']
['gym', 'sofa', 'bed']
['pool', 'gym', 'sofa']
['pool', 'gym', 'sofa', 'bed']

不应该匹配:

['pool', 'gym', 'bed']
['pool', 'sofa', 'bed']

我将这些项目存储在json文本中,该文本被索引为json属性。

表示例:

ID  ITEMS
1   {'items': ['gym', 'sofa']}
2   {'items': ['gym', 'sofa', 'bed']}
3   {'items': ['pool', 'gym', 'bed']}
4   {'items': ['pool', 'sofa', 'bed']}

我的sphinx.conf类似于:

source srcItems
{
    type            = mysql

    sql_host        = localhost
    sql_user        = root
    sql_pass        =
    sql_db          = items
    sql_port        = 3306    # optional, default is 3306

    sql_query        = \
        SELECT id, items \
        FROM items

    sql_attr_json        = items
}

index items
{
    source          = srcItems
    path            = /opt/local/var/sphinx/data/items
}

indexer
{
    mem_limit        = 128M
}

searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    log             = /opt/local/var/sphinx/log/searchd.log
    query_log       = /opt/local/var/sphinx/log/query.log
    read_timeout    = 5
    max_children    = 30
    pid_file        = /opt/local/var/sphinx/log/searchd.pid
    max_matches     = 1000
    seamless_rotate = 1
    preopen_indexes = 1
    unlink_old      = 1
    workers         = threads # for RT to work
    binlog_path     = /opt/local/var/sphinx/data
}

我尝试使用以下内容但没有结果:

SELECT id, 
    ALL(var='gym' AND var='sofa' FOR var IN items.items) as i 
FROM items 
WHERE i=1;

SELECT id, 
    ANY(var='gym' AND var='sofa' FOR var IN items.items) as i 
FROM items 
WHERE i=1;

还尝试了以下操作,返回错误的结果:

SELECT id, 
    ALL(var='gym' OR var='sofa' FOR var IN items.items) as i 
FROM items 
WHERE i=1;

SELECT id, 
    ANY(var='gym' OR var='sofa' FOR var IN items.items) as i 
FROM items 
WHERE i=1;

当我这样做时,我得到了预期的结果:

SELECT id, 
    IN(items.items, 'gym') AS gym, 
    IN(items.items, 'sofa') AS sofa 
FROM offers 
WHERE gym = 1 AND sofa = 1 

但它大大减慢了查询速度,并使查询的构建变得更加复杂。

我做错了什么?

在Sphinx中执行此查询的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

我认为

SELECT id FROM offers  
  WHERE items.items IN('gym')
  AND items.items IN('sofa')

可能会奏效。如果没有,请尝试

SELECT id, 
    IN(items.items, 'gym')+IN(items.items, 'sofa') AS i
FROM offers 
WHERE i = 2 

,否则

SELECT id, 
   ANY(var='gym' FOR var IN items.items)+ANY(var='sofa' FOR var IN items.items) as i 
FROM items 
WHERE i=2;

答案 1 :(得分:0)

结束使用:

SELECT id, 
    IN(items.items, 'gym') AS gym, 
    IN(items.items, 'sofa') AS sofa 
FROM offers 
WHERE gym = 1 AND sofa = 1

它工作正常,额外的时间不是很大的问题,可以通过更好的硬件来缓解。

由于