如何在 Oracle 的 CONTAINS 运算符中获得 text_query 4000 个字符左右的限制?

发布于 2024-09-07 08:16:49 字数 596 浏览 5 评论 0原文

在 Oracle 中,全文搜索语法 包含运算符 is:

 CONTAINS(
          [schema.]column,
          text_query    VARCHAR2
          [,label       NUMBER]) RETURN NUMBER;

表示text_query的长度不能超过4000个字符,否则会出错。在很多情况下,我反复使用超过 4000 个字符的 text_query。如果可能的话,作为 Oracle 专家,您建议如何绕过这种限制?

进一步澄清容易达到 4000 的情况是,如果结合许多 包含查询运算符来构建您的text_query,很可能会超过4000个字符的限制。

In Oracle, the full text search syntax of Contains operator is:

 CONTAINS(
          [schema.]column,
          text_query    VARCHAR2
          [,label       NUMBER]) RETURN NUMBER;

which means the text_query can not be more than 4000 characters long or an error will occur. I repeatedly have text_query longer than 4000 characters long in many cases. How would you, as an Oracle expert, suggest to get around such limitation if possible?

To further clarify the situation in which 4000 is easily reached is that if you combine many Contains Query Operators to construct your text_query, it is quite possible to exceed such 4000 characters limitation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-09-14 08:16:49

4000 个字符的限制并不是任意的界限:它是 Oracle SQL 可以处理的 VARCHAR2 字符的最大数量。

4000 个字符是很多文本。英文版大约有 600 个单词,相当于一张 A4 纸,并且使用了一些合理的点号字体。我能想到的应用程序并不多需要搜索如此大块的废话。即使大学检查学生论文是否抄袭,也只会在段落层面进行操作。

但是,如果确实遇到这样的情况,即匹配不足 4000 个字符会产生误报,那么您所能做的就是将查询字符串拆分成块并对其进行搜索。这意味着您必须使用 PL/SQL:

create or replace function big_search (p_search_text in varchar2) 
    return sys_refcursor
is
    return_value sys_refcursor;
    p_srch1 varchar2(4000);
    p_srch2 varchar2(4000);
begin

    dbms_output.put_line('search_length='||to_char(length(p_search_text)));

    p_srch1 := substr(p_search_text, 1, 4000);
    p_srch2 := substr(p_search_text, 4001, 4000);


    open return_value for 
        select docname
                , (score(1) + score(2))/2 as score
        from t23
        where contains ( text_column, p_srch1 , 1) != 0
        and  contains ( text_column, p_srch2 , 2) != 0;

    return return_value;
end;
/

如果您事先不知道搜索文本的大小,那么您需要使用动态 SQL 来组装它。请注意,将空搜索项传递给 CONTAINS() 将抛出 DRG-50901:文本查询解析器语法错误

The 4000 character limit is not some arbitrary boundary: it is the maximum amount of VARCHAR2 characters that Oracle SQL can handle.

4000 characters is a lot of text. In English it's around 600 words, or an A4 page and a bit in a reasonable point font. There are not many applications I can think of which require searching for such large chunks of verbiage. Even colleges checking students' essays for plagiarism would operate at no more than the paragraph level.

However, if you really have a situation in which matching on a scant 4000 characters generates false positives all you can do is split the query string into chunks and search on them. This means you have to use PL/SQL:

create or replace function big_search (p_search_text in varchar2) 
    return sys_refcursor
is
    return_value sys_refcursor;
    p_srch1 varchar2(4000);
    p_srch2 varchar2(4000);
begin

    dbms_output.put_line('search_length='||to_char(length(p_search_text)));

    p_srch1 := substr(p_search_text, 1, 4000);
    p_srch2 := substr(p_search_text, 4001, 4000);


    open return_value for 
        select docname
                , (score(1) + score(2))/2 as score
        from t23
        where contains ( text_column, p_srch1 , 1) != 0
        and  contains ( text_column, p_srch2 , 2) != 0;

    return return_value;
end;
/

If you don't know the size of the search text beforehand, then you'll need to use dynamic SQL to assemble this. Note that passing null search terms to CONTAINS() will hurl DRG-50901: text query parser syntax error.

赏烟花じ飞满天 2024-09-14 08:16:49

当前版本现在支持 CLOB 参数

CONTAINS(
     [schema.]column,
     text_query    [VARCHAR2|CLOB]
     [,label       NUMBER])
RETURN NUMBER;

http://docs .oracle.com/cd/B28359_01/text.111/b28304/csql.htm#i997503

The current version supports now a CLOB parameter

CONTAINS(
     [schema.]column,
     text_query    [VARCHAR2|CLOB]
     [,label       NUMBER])
RETURN NUMBER;

http://docs.oracle.com/cd/B28359_01/text.111/b28304/csql.htm#i997503

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文