在Oracle全文搜索中合并条件

时间:2019-02-22 06:00:54

标签: sql oracle

我正在尝试在Oracle中实现全文搜索,但是我不明白如何正确创建搜索查询?

任务如下:

  1. 如果我正在写一个单词,则应显示所有条目并按相关性排序。在这种情况下,当我的查询中包含“单词”时,搜索结果应显示按相关性排序的“ dddwordddd”,“ word”,“ wordddd”和“ dddword”。
  2. 如果我在搜索查询中写了几个单词,则应显示同时包含所有单词和某些查询的结果。也按相关性排序。
  3. 而且应该可以将单词组合在一起。例如, 当我用引号(或任何其他包装,如“ /”或“ [”)结束单词时,必须严格搜索此类条目,而不能按单词单独搜索。

使用ABOUT运算符只能实现其中的一部分。它非常适合任务的第二项。如果我写这样的查询:

SELECT SCORE(1), name 
from table 
WHERE CONTAINS(name, 'ABOUT('some text')', 1) > 0 
ORDER BY SCORE(1) DESC;

然后我将得到以下结果:

some text
some
text

但是如何将其与短语搜索(不按单词单独搜索)以及对部分出现的搜索(例如%word%)结合起来?

1 个答案:

答案 0 :(得分:2)

所以让我们用示例文本加载表格:

create table t (
  c1 varchar2(100)
);

insert into t values ( 'some text' );
insert into t values ( 'some' );
insert into t values ( 'text' );
insert into t values ( 'exact phrase text' );
insert into t values ( 'phrase text' );
insert into t values ( 'exact phrase text word' );
insert into t values ( 'exact some wordddd phrase ' );
insert into t values ( 'phrase exact text dddword' );

commit;

create index i 
  on t ( c1 )
  indextype is ctxsys.context;

1)您可以使用通配符在字符串中搜索字符串:

SELECT SCORE(1), c1 
from   t
WHERE  CONTAINS( c1, '%word%', 1 ) > 0 
ORDER BY SCORE(1) DESC;

SCORE(1)   C1                           
         5 exact phrase text word       
         5 phrase exact text dddword    
         5 exact some wordddd phrase    

如果要对包含完全匹配的字符串“ word”的文本进行优先级排序,请使用OR搜索完全匹配。为了确保精确匹配的排名更高,您还可以通过将其与权重运算符“相乘”来对该权重进行加权:

SELECT SCORE(1), c1 
from   t
WHERE  CONTAINS( c1, '%word% or word*2', 1 ) > 0 
ORDER BY SCORE(1) DESC;

SCORE(1)   C1                           
        11 exact phrase text word       
         5 phrase exact text dddword    
         5 exact some wordddd phrase  

2)ABOUT运算符搜索相关的术语或短语。例如,当您搜索“文本”时,可以使用它返回包含“字符串”的文档。如果要搜索包含列表中任何单词的文本,或者将它们组合在一起:

SELECT SCORE(1), c1 
from   t
WHERE  CONTAINS( c1, 'some OR text', 1 ) > 0 
ORDER BY SCORE(1) DESC;

SCORE(1)   C1                          
         3 some text                   
         3 text                        
         3 phrase exact text dddword   
         3 phrase text                 
         3 exact phrase text word      
         3 exact phrase text 

3)要查找特定短语,请在您的包含内容中输入确切的短语:

SELECT SCORE(1), c1 
from   t
WHERE  CONTAINS( c1, 'exact phrase', 1 ) > 0 
ORDER BY SCORE(1) DESC;

SCORE(1)   C1                       
         4 exact phrase text word   
         4 exact phrase text      

如果您要搜索的文本包含上述搜索的任意组合,则可以尝试将它们全部填充到一个CONTAINS调用中。但是,将“包含”的调用进行“或”比较容易。为每个参数的第三个参数提供一个新值。然后,您可以通过将相同的值传递给SCORE来获得分数:

SELECT SCORE(1), SCORE(2), SCORE(3), 
       SCORE(1) + SCORE(2) + SCORE(3) tot_score,
       c1 
from   t
WHERE  CONTAINS( c1, 'exact phrase', 1 ) > 0 
OR     CONTAINS( c1, '%word% or word*2', 2 ) > 0 
OR     CONTAINS( c1, 'some or text', 3 ) > 0 
ORDER BY SCORE(1) + SCORE(2) + SCORE(3) DESC;

SCORE(1)   SCORE(2)   SCORE(3)   TOT_SCORE   C1                           
         4         11          3          18 exact phrase text word       
         0          5          3           8 phrase exact text dddword    
         4          0          3           7 exact phrase text            
         0          5          0           5 exact some wordddd phrase    
         0          0          3           3 text                         
         0          0          3           3 some text                    
         0          0          3           3 phrase text  

如果您想了解更多信息,请查看文档中的CONTAINS query operators