如何使用oracle包含特殊字符搜索整个单词?

时间:2013-08-09 15:50:58

标签: sql oracle

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

当我在下面搜索时

select * from table1 where CONTAINS (short_desc,'{product\_id}') > 0 

我得到的结果如

  

abc product_id

     

abc产品ID

转义字符的行为类似于OR,查询会搜索“product_id”或“产品ID”

我如何只搜索“product_id”这个词?

提前致谢

2 个答案:

答案 0 :(得分:2)

根据Oracle*Text documentation,非字母字符被视为空格(因此产品$ id和product_id都被视为'产品ID')。

此行为在this SQLFiddle处展示。在'product'和'id'之间放置哪个非aplhanumeric符号无关紧要。

要改变您必须使用CONTEXT索引使用的词法分析器define underscore as printjoin

我无法在SQLFiddle中证明这一点,因为此处对ctx_ddl包的访问受限制,但此代码必须完成此任务:

create table table1(short_desc varchar2(200))
/

-- Add lexer definition and set '_' as "printjoin"
begin
  ctx_ddl.create_preference('mylex', 'BASIC_LEXER');
  ctx_ddl.set_attribute('mylex', 'printjoins', '_');
end;
/

-- Specify new lexer definition while creating index
create index table1_ctx_index on table12(short_desc) 
  indextype is ctxsys.context 
  parameters ( 'LEXER mylex' )
/

insert into table1(short_desc) values('1 product id 2')
/
insert into table1(short_desc) values('3 product_id 4')
/
insert into table1(short_desc) values('5 product#id 6')
/
insert into table1(short_desc) values('7 productXid 8')
/
insert into table1(short_desc) values('9 product-id 10')
/

alter index table1_ctx_index rebuild
/

答案 1 :(得分:0)

可以尝试这个

 select * from table1 where CONTAINS (short_desc,'{product'||'\_id}') > 0
相关问题