简体   繁体   English

JSON上的Postgres全文搜索

[英]Postgres full text search on json

I am dumping a json value into text field as I have some invalid json data that cannot be stored in json / jsonb . 我将json值转储到text字段中,因为我有一些无法存储在json / jsonb无效json数据。 I would like to search and highlight "Value16": 16711680 in the following example, but I am also getting a hit on "Value20": 16711680 . 在下面的示例中,我想搜索并突出显示"Value16": 16711680 ,但我也对"Value20": 16711680

{
  "Value01": 122,
  "Value02": 25,
  "Value03": 9,
  "Value04": 538,
  "Value05": false,
  "Value06": 65534,
  "Value07": 2,
  "Value08": 0,
  "Value09": 2,
  "Value10": 1,
  "Value11": 0,
  "Value12": 0,
  "Value13": false,
  "Value14": 0,
  "Value15": 0,
  "Value16": 16711680,
  "Value17": 0,
  "Value18": 0,
  "Value19": 0,
  "Value20": 16711680,
  "Value21": 0,
  "Value22": 0
}

I tried following queries but did not work. 我尝试了以下查询,但是没有用。 Do I need to use any escape characters here? 我需要在这里使用任何转义字符吗?

to_tsquery('Value16<->16711680')

to_tsquery('Value16&16711680')

Basically I would like to hit only if key and value match together . 基本上,我只想在键和值匹配才能命中。
How to search for a key/value pair in the json text? 如何在json文本中搜索键/值对?

Since your key and value are directly adjacent, I would suggest a phrase search (requires Postgres 9.6 or later): 由于您的键和值直接相邻,因此建议您进行词组搜索 (需要Postgres 9.6或更高版本):

...
WHERE my_tsvector @@ to_tsquery('simple', 'Value16 <-> 16711680');

Or: 要么:

...
WHERE my_tsvector @@ phraseto_tsquery('simple','Value16 16711680');

Only qualifies with the two search terms next to each other (ignoring noise characters in between). 仅限定两个相邻的搜索词(忽略它们之间的噪音字符)。 There is also the <N> variant of the operator for terms a couple of words apart. 运算符还有<N>变体,用于分开几个单词。

Detailed explanation: 详细说明:

You can even combine this with prefix matching: 您甚至可以将其与前缀匹配结合使用:

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

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