使用Oracle db中的CONTAINS子句进行精确短语搜索

时间:2017-12-20 12:20:21

标签: oracle

我有以下查询,我希望返回与短语&#34完全匹配的行;我最喜欢的东西"

SELECT TestColl.tID, TestMetavalues.*
FROM TestColl, TestMetavalues
WHERE TestColl.tID=TestMetavalues.tID 
AND ( (CONTAINS(TestFullText,'(My favorite thing)') > 0 )) ;

但是上面的查询返回的行只有"我最喜欢的东西"以及最喜欢"

的行

表TestColl有一个BLOB列 - TestFullText

我希望查询只显示那些具有"我最喜欢的东西的行#34;

如何实现这一目标?

我试过这些解决方案,但没有运气

Expecting exact results when using contains clause in Oracle

search criteria difference between Like vs Contains() in oracle

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT TestColl.tID, TestMetavalues.*
FROM TestColl, TestMetavalues
WHERE TestColl.tID=TestMetavalues.tID 
AND TestColl.TestFullText LIKE '%My favorite thing%' ;

答案 1 :(得分:0)

  

我希望查询只显示那些具有"我最喜欢的东西的行#34;

如果是这样的话,实际上,不要想要一个简单的

SELECT TestColl.tID, TestMetavalues.*
FROM TestColl, TestMetavalues
WHERE TestColl.tID = TestMetavalues.tID 
  AND TestFullText = 'My favorite thing';

为什么要使用Oracle Text?

答案 2 :(得分:0)

域名索引并非如此。我们正在为源文本中的关键字编制索引,因此我们实际上不会存储"我最喜欢的东西",我们将存储"我的"和#34;最爱"和"事情"等等。

但是您仍然可以通过将索引用作初始过滤机制来获益,例如

SQL> create table t ( x varchar2(1000));

Table created.

SQL>
SQL> insert into t values ('These are my medium stuff');

1 row created.

SQL> insert into t values ('These are stuff I hate');

1 row created.

SQL> insert into t values ('These are other things');

1 row created.

SQL> insert into t values ('These are semi FAVORITE things');

1 row created.

SQL> insert into t select * from t;

4 rows created.

SQL> insert into t select * from t;

8 rows created.

SQL> insert into t select * from t;

16 rows created.

SQL> insert into t select * from t;

32 rows created.

SQL> insert into t select * from t;

64 rows created.

SQL> insert into t select * from t;

128 rows created.

SQL> insert into t select * from t;

256 rows created.

SQL> insert into t select * from t;

512 rows created.

SQL> --
SQL> -- our special rows
SQL> --
SQL> insert into t values ('These are a few of My Favorite Things');

1 row created.

SQL> insert into t values ('Some other of My Favorite Things');

1 row created.

SQL>
SQL> create index ix on t (x )
  2  indextype is ctxsys.context;

Index created.

SQL>
SQL> exec dbms_stats.gather_table_stats('','T')

PL/SQL procedure successfully completed.

SQL>
SQL> set autotrace on explain
SQL> select count(*) from t
  2  where CONTAINS(x,'My Favorite Things') > 0;

  COUNT(*)
----------
       258

1 row selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 2114225437

-------------------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------
|   0 | SELECT STATEMENT |      |     1 |    26 |     1   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE  |      |     1 |    26 |            |          |
|*  2 |   DOMAIN INDEX   | IX   |   130 |  3380 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("CTXSYS"."CONTAINS"("X",'My Favorite Things')>0)

SQL>
SQL> select * from t
  2  where CONTAINS(x,'My Favorite Things') > 0
  3  and x like '%My Favorite Things%';

X
----------------------------------------------------------------------------------------------------
These are a few of My Favorite Things
Some other of My Favorite Things

2 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1339481741

------------------------------------------------------------------------------------
| Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |      |     6 |   156 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS BY INDEX ROWID| T    |     6 |   156 |     3   (0)| 00:00:01 |
|*  2 |   DOMAIN INDEX              | IX   |       |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("X" LIKE '%My Favorite Things%' AND "X" IS NOT NULL)
   2 - access("CTXSYS"."CONTAINS"("X",'My Favorite Things')>0)

SQL>
SQL>

因此,域索引将我们减少到258个候选行,然后额外的LIKE将我们降低到我们想要的2行。