简体   繁体   English

MySQL 对 JSON 数据的全文搜索

[英]MySQL full text search on JSON data

I'm trying to replicate the following LIKE query using a full text search on JSON data;我正在尝试使用对JSON数据的全文搜索来复制以下LIKE查询;

SELECT * FROM table
  WHERE response LIKE '%"prod_id": "foo"%'
  AND response LIKE '%"start_date": "2016-07-13"%'

In my database the above query returns 28 rows在我的数据库中,上述查询返回 28 行

This is my attempt:这是我的尝试:

SELECT * FROM table
  WHERE MATCH(response)
    AGAINST('+"\"prod_id\": \"foo\"",+"\"start_date\": \"2016-07-13\""')

However this returns over 4,500 rows (the same as running the first query for only the prod_id ~1,900 rows when running the first query on just the date)但是,这将返回超过 4,500 行(与仅在日期上运行第一个查询时仅对prod_id ~1,900 行运行第一个查询相同)

It was my understanding that +"text here" would indicate a required word, and that literal double quotes (present in the JSON data) should be escaped, and that , would indicate a split between the two strings I'm looking for.我的理解是+"text here"将表示一个必需的单词,并且应该对文字双引号(存在于JSON数据中)进行转义,而,将表示我正在寻找的两个字符串之间的拆分。 What am I not understanding correctly?我没有正确理解什么? Is there any point in running this as a full text query anyway?无论如何,将它作为全文查询运行有什么意义吗?

Thanks to @Sevle I've tweaked my query like so, and it's returning the correct results;感谢@Sevle,我像这样调整了我的查询,它返回了正确的结果;

SELECT * FROM table
  WHERE MATCH(response)
    AGAINST('+\"prod_id: foo\" +\"start_date: 2016-07-13\"' IN BOOLEAN MODE)

The comma was not helping and I was escaping the wrong characters, and of course I did need IN BOOLEAN MODE to be added.逗号没有帮助,我正在转义错误的字符,当然我确实需要添加IN BOOLEAN MODE Finally, I removed the double quotes I was searching for in the JSON string.最后,我删除了我在JSON字符串中搜索的双引号。

It may also be worth noting that as I'm using PHP PDO to run this query I also had to make the following tweaks.可能还值得注意的是,当我使用PHP PDO运行此查询时,我还必须进行以下调整。

Instead of constructing the query like so trying to bind the variables like I normally would;而不是像往常那样尝试绑定变量来构建查询;

$query = $db->prepare('...AGAINST('+\"prod_id: :prod_id\" +\"start_date: :start_date\"');
$query->execute(array('prod_id' => 'foo', 'start_date' => '2016-07-13'));

I had to do this, as I found I could not bind variables in full text searches我不得不这样做,因为我发现我无法在全文搜索中绑定变量

$sql_against = $db->quote('...AGAINST('+\"prod_id: foo\" +\"start_date: 2016-07-13\"');
$query = $db->prepare("...AGAINST($sql_against IN BOOLEAN MODE)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM