Oracle Contains不起作用

时间:2013-03-13 20:18:04

标签: sql oracle

我的表中有一个列是上下文索引的列。

CREATE INDEX CIDX_MUSTFIXBY ON TABLE
  (MUST_FIX_BY)
INDEXTYPE IS CTXSYS.CONTEXT
NOPARALLEL;

我正在尝试查询条件

  

AND must_fix_by LIKE'Q2%'

然后返回行。

但是当我尝试查询where condition

  

AND包含(must_fix_by,'Q2')> 0

并且它不会返回任何行。

有人可以告诉我为什么工作和包含不是吗?

2 个答案:

答案 0 :(得分:5)

两个可能的原因 - 索引可能无法同步,CONTAINS似乎匹配单词而LIKE匹配字符串。

两个字符串的示例,其中LIKE与两者匹配,但CONTAINS不匹配:

create table test1(must_fix_by varchar2(4000));
create index cidx_mustfixby on test1(must_fix_by) indextype is ctxsys.context;
insert into test1 values('Q234567');
insert into test1 values('Q2 234567');
select * from test1 where must_fix_by like 'Q2%';

MUST_FIX_BY
-----------
Q234567
Q2 234567

select * from test1 where contains(must_fix_by, 'Q2') > 0;

no rows selected

默认情况下,CONTEXT索引必须为manually synchronized。您需要运行:exec ctx_ddl.sync_index('cidx_mustfixby');,或者需要使用on commit创建索引。

exec ctx_ddl.sync_index('cidx_mustfixby');
select * from test1 where contains(must_fix_by, 'Q2') > 0;

MUST_FIX_BY
-----------
Q2 234567

这解决了其中一个问题。但Q234567仍然不匹配。我对Oracle Text知之甚少,甚至找不到CONTAINS如何工作的简单描述。但它似乎是基于完整的单词而不是字符串。需要某种词边界 在Q2和其他字符之间,可以通过简单的CONTAINS过滤器来获取它。

答案 1 :(得分:0)

使用CONTAINS时,您还需要添加%。

AND contains(must_fix_by, 'Q2%') > 0