- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Oracle 中,我可以使用“NEAR((a,b,c), 5)”这样的查询在 Clob 中进行搜索。这是来自 oracle 的文档:
Use the NEAR operator to return a score based on the proximity of two or more query terms. Oracle Text returns higher scores for terms closer together and lower scores for terms farther apart in a document.
我如何在 Postgres 中做到这一点?我只需要一个可以搜索另一个词附近的词的索引。
最佳答案
这是汉明距离函数 http://en.wikipedia.org/wiki/Hamming_distance
头文件
/**
@file HammingDistance.h A C header file to compute the Hamming Distance between two strings
as PostgreSQl C User Defined Function
*/
#ifndef HAMMINGDISTANCE_H_INCLUDED
#define HAMMINGDISTANCE_H_INCLUDED
DLLEXPORT Datum DistanceCstring(PG_FUNCTION_ARGS);
DLLEXPORT Datum Distance(PG_FUNCTION_ARGS);
#endif // HAMMINGDISTANCE_H_INCLUDED
源文件
/**
@file HammingDistance.c A C source file to compute the Hamming Distance between two strings
as PostgreSQl C User Defined Function
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "postgres.h"
#include "utils/geo_decls.h"
#include "utils/builtins.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#define VC_EXTRALEAN
#pragma warning (disable : 4996)
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#ifdef _WIN32
#define DLLEXPORT _declspsec(dllexport)
#else
#define DLLEXPORT
#endif // _WIN32
static int32_t DistanceString( unsigned char * a, int32_t la , unsigned char * b, int32_t lb);
static int32_t DistanceUChar( unsigned char a, unsigned char b);
PG_FUNCTION_INFO_V1(Distance);
/**
DistanceCstring An SQL function to Compute Hamming Distance between two text types
@param[in] A a Text type;
@param[in] B a text type
@return The hamming Distance between two Text types
**/
DLLEXPORT Datum Distance(PG_FUNCTION_ARGS)
{
text * a = PG_GETARG_TEXT_PP(0);
text * b = PG_GETARG_TEXT_PP(1);
unsigned char * ac;
unsigned char * bc;
int32_t distance = 0;
ac = text_to_cstring( a );
bc = text_to_cstring( b );
distance = DistanceString( ac, strlen(ac), bc, strlen(bc) );
PG_RETURN_INT32(distance);
}
PG_FUNCTION_INFO_V1(DistanceCstring);
/**
DistanceCstring An SQL function to Compute Hamming Distance between two strings
@param[in] A a Cstring type
@param[in] B a Cstring type
@return The hamming Distance between two Cstring types
**/
DLLEXPORT Datum DistanceCstring(PG_FUNCTION_ARGS)
{
unsigned char * ac = (unsigned char *) PG_GETARG_CSTRING(0);
unsigned char * bc = (unsigned char *) PG_GETARG_CSTRING(1);
int32_t distance = 0;
distance = DistanceString( ac, strlen(ac), bc, strlen(bc) );
PG_RETURN_INT32(distance);
}
/**
DistanceString Compute Hamming Distance between two unsigned char strings
@param[in] a an unsigned char array
@param[in] la length of a in char
@param[in] b an unsigned char array
@param[in] lb length of b in char
@return Hamming distance
**/
static int32_t DistanceString( unsigned char * a, int32_t la , unsigned char * b, int32_t lb)
{
unsigned char * smaller;
unsigned char * larger;
int i = 0;
int length = 0;
int32_t distance = 0;
int delta = 0;
if ( lb > la )
{
delta = lb - la;
length = la;
smaller = a;
larger = b;
}
else
{
delta = la - lb;
length = lb;
smaller = b;
larger = a;
}
for( i = 0; i < length; i++ )
{
distance += DistanceUChar( * smaller++, * larger++);
}
for( i = 0; i < delta ; i++ )
{
distance += DistanceUChar( 0, * larger++);
}
return distance;
}
/**
DistanceUChar Compute Hamming Distance between two unsigned chars
@param[in] a unsigned char
@param[in] b unsigned char
@return Hamming Distance between two unsigned chars
**/
static int32_t DistanceUChar( unsigned char a, unsigned char b)
{
unsigned char x = a ^ b;
int32_t distance = 0;
if ( (x & 0x1 )== 0x1 )
distance++;
if ( (x & 0x2) == 0x2 )
distance++;
if ( (x & 0x4) == 0x4 )
distance++;
if ( (x & 0x8) == 0x8 )
distance++;
if ( x & 0x10 == 0x10 )
distance++;
if ( (x & 0x20) == 0x20 )
distance++;
if ( (x & 0x40) == 0x40 )
distance++;
if ( (x & 0x80) == 0x80 )
distance++;
return distance;
}
生成文件
OPTS := -g -fpic -c -I /opt/PostgreSQL/9.1/include/postgresql/server
INSTALLDIR := /opt/PostgreSQL/9.1/lib/postgresql
all: HammingDistance.so
HammingDistance.so: HammingDistance.c HammingDistance.h
gcc HammingDistance.c $(OPTS) -o HammingDistance.o
gcc -shared -o HammingDistance.so HammingDistance.o
clean:
rm -rf *.o *.so
register:
psql -f install.sql -p 5433 -U postgres -d postgres ;
install:
sudo cp HammingDistance.so $(INSTALLDIR);
test:
psql -f Test.sql -p 5433 -U postgres -d postgres ;
安装SQL
-- define the schema
set search_path to public;
-- Remove existing function
drop function if exists Distance( text, text ) cascade;
drop function if exists Distance( cstring , cstring ) cascade;
-- Create the new one
create or replace function Distance( text, text ) returns integer
as '$libdir/HammingDistance', 'Distance'
language c strict;
create or replace function Distance( cstring, cstring ) returns integer
as '$libdir/HammingDistance', 'DistanceCstring'
language c strict
关于oracle - 使用近操作符 LIKE oracle 上下文索引的 Postgres 全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17190441/
我需要为登录用户生成一个建议列表。基本上是“既然你喜欢这些东西,而其他喜欢这些东西的人也喜欢这些东西,那么你可能也会喜欢这些东西”。 我可能会想出一个不错的算法来产生这样的结果,但在我重新发明轮子之前
此查询中有许多参数是用户可调整的,我希望用户能够创建一些文本文件,使用 sqlite3 db.sqlite ".read query.sql" > result.csv 运行查询,而不是直接将它们编辑
这个问题在这里已经有了答案: MySQL query finding values in a comma separated string (11 个回答) 11 个月前关闭。 我有一个名为 pape
我已将以下内容添加到我的 Blogger 模板中,以便在我的每个 Blogger 帖子上添加一个“赞”按钮: document.write('<iframe src="htt
当我点击“推荐”时,按钮打开的窗口隐藏(不可见)在页脚后面。其实我有截图来准确解释它:http://www.diigo.com/item/image/1q1ia/tw30 当然是XFBML,因为我知道
我在 facebook 上创建了一个页面“全民教育”。在我的网站中,我想提供一个点赞按钮来点赞此页面,而不是url,并且还想显示网站中该页面收到的点赞数以及点赞按钮。我使用了“添加这个”,但它显示了该
这个问题在这里已经有了答案: SQL Server, combining LIKE and IN? (3 个答案) 关闭 6 年前。 我想准备一个查询: SELECT name FROM Emplo
我有一个包含 200 条记录的表,其中 10 条记录的文本包含单词“TAX”。 当我执行时 Select * from tbl1 WHERE [TextCol] LIKE '%TAX%' 然后我正确地
我要执行查询: ---------------------- |name | ---------------------- |data
嗨,我正在为一个我没有设计的数据库编写 MySQL 请求,目前无法真正更改,我有一个问题,我还没有测试过这个,所以它可能按设计工作,但我没有当然。 所以我正在搜索的表只有三列 id、标题和描述,但描述
我已经尽力把这个问题说得很透彻了,所以如果你不耐烦,就跳到最后看看真正的问题是什么...... 我正在努力调整我们其中一个数据库中某些搜索功能的实现方式。为此,我正在向我们的应用程序 API 添加一些
我正在学习 MySQL,我尝试在查询中将“NOT LIKE”与“OR”结合起来,但没有成功。 假设我有一个 table1,其中有一列名为“word”,如下所示: word
Perl-Selenium 还提供了 Test::More 的标准方法,例如 ok()、like()、is() 等,也作为对象方法,例如$sel->like()。 ($sel 是 selenium p
我正在开发这个网站的前端:http://oq.totaleclips.com ,并从以下位置开发它:http://dev-jon.c2mx-hrd.appspot.com 我移动了社交按钮以使 UI
我正在创建一个搜索功能。我的 sql 查询使用 LIKE和 OR LIKE在 where 子句中。我正在搜索的表中的字段之一称为 quantity_types它只存储与在另一个表中找到的值相对应的代码
我有一个名为 platform 的表,其中有一列名为 entityid。 entityid 中的数据应该遵循 n.n.n 格式(其中 n = 1 个或多个数字,第一个数字是站点 ID)。 如果我运行这
所以,我正在练习考试(高中水平),虽然我们从未想过 SQL,但在处理 MS Access 时有必要了解一点. 任务是选择名称与其所属城镇不对应的区域ID。 解决方案中有以下示例: SELECT nam
1.作用与语法描述 作用: 正则表达式是使用指定字符串来描述、匹配一系列符合某个句法规则的字符串。许多程序设计语言都支持利用正则表达式进行字符串操作。MongoDB 使用 $regex 操作符来设
我们有一个网站,其中有文章页面,每个页面上都有一个类似 Facebook 的按钮,并与该文章 URL(URL 编码)相关联。例如,在一些带有 url http://www.site.com/artic
我在理解 SQL 中的 LIKE 和 NOT LIKE 运算符时遇到问题。这是我执行的查询: select serial_number from UNIT U group by serial_numb
我是一名优秀的程序员,十分优秀!