1

我有表結構如下上下文索引中包含特殊字符

mytable(id number, name varchar2(100), department varchar2(100), description varchar2(100));

,並創建了描述列上下文索引

create index myindex on mytable(description) indextype IS CTXSYS.CONTEXT parameters(lexer mylex);

描述列包含逗號分隔值當我做下面的搜索它做OR搜索。

select * from mytable where contains(description,'aaron,lord')>0;

它給出具有描述列亞倫或主的結果。

回答

0

使用\{...}escape accumulate operators

示例模式

--drop table mytable; 

create table mytable(id number, 
name varchar2(100), 
department varchar2(100), 
description varchar2(100)); 

insert into mytable values(1, 'A', 'A', 'aaron,lord'); 
insert into mytable values(2, 'B', 'B', 'aaron'); 
insert into mytable values(3, 'C', 'C', 'lord'); 
commit; 

create index myindex on mytable(description) 
indextype IS CTXSYS.CONTEXT; 

問題

默認情況下,逗號被視爲accumlate操作,並返回所有四個行,因爲他們都要麼「阿龍」或「主」。

select description from mytable where contains(description,'aaron,lord')>0; 

DESCRIPTION 
----------- 
aaron,lord 
aaron 
lord 
aaron lord 

解第1部分 - 逸出逗號

逃逸累加器將防止或門和排除「艾倫」和「主」。我假設真正的查詢使用一個綁定變量,而且沒有硬編碼,這就是爲什麼下面的查詢使用REPLACE||而不是簡單地修改字符串。

select description from mytable where contains(description, replace('aaron,lord', ',', '\,')) > 0; 
select description from mytable where contains(description, '{' || 'aaron,lord' || '}') > 0; 

DESCRIPTION 
----------- 
aaron,lord 
aaron lord 

解決部分2 - 添加逗號printjoin

drop index myindex; 

begin 
    ctx_ddl.create_preference('mylex', 'BASIC_LEXER'); 
    ctx_ddl.set_attribute('mylex', 'printjoins', ','); 
end; 
/

create index myindex on mytable(description) 
indextype IS CTXSYS.CONTEXT 
parameters ('LEXER mylex'); 

現在只有一行將被退回。

select description from mytable where contains(description, replace('aaron,lord', ',', '\,')) > 0; 
select description from mytable where contains(description, '{' || 'aaron,lord' || '}') > 0; 

DESCRIPTION 
----------- 
aaron,lord 

但結果是變得如此具體的我不知道是否會更好,以避免CONTAINS,只需使用常規的SQL函數和條件。

+0

謝謝喬恩。它對我有用:) – Mahi

+0

我已經使用過這種情況'select description from mytable where contains(description,'{'||'aaron,lord'||'}')> 0;'它顯示的結果有價值的aaron主也。你能讓我知道如何避免這種情況嗎? – Mahi

+0

@Mahi看到我更新的答案。 –