首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Oracle数据库中使用CONTAINS子句进行精确短语搜索

在Oracle数据库中使用CONTAINS子句进行精确短语搜索
EN

Stack Overflow用户
提问于 2017-12-20 20:20:21
回答 3查看 789关注 0票数 1

我有下面的查询,我希望返回与短语"My favorite thing“完全匹配的行。

代码语言:javascript
复制
SELECT TestColl.tID, TestMetavalues.*
FROM TestColl, TestMetavalues
WHERE TestColl.tID=TestMetavalues.tID 
AND ( (CONTAINS(TestFullText,'(My favorite thing)') > 0 )) ;

但是上面的查询返回了只有"My favorite thing“和"favorite”的行

表TestColl有一个BLOB列- TestFullText

我希望查询只显示与“我最喜欢的东西”完全相同的那些行。

如何做到这一点?

我尝试了这些解决方案,但没有成功

Expecting exact results when using contains clause in Oracle

search criteria difference between Like vs Contains() in oracle

EN

回答 3

Stack Overflow用户

发布于 2017-12-20 23:04:04

试试这个:

代码语言:javascript
复制
SELECT TestColl.tID, TestMetavalues.*
FROM TestColl, TestMetavalues
WHERE TestColl.tID=TestMetavalues.tID 
AND TestColl.TestFullText LIKE '%My favorite thing%' ;
票数 1
EN

Stack Overflow用户

发布于 2017-12-21 04:50:38

我希望查询只显示那些确切包含“我最喜欢的东西”的行。

如果是这样的话,你不是想要一个简单的

代码语言:javascript
复制
SELECT TestColl.tID, TestMetavalues.*
FROM TestColl, TestMetavalues
WHERE TestColl.tID = TestMetavalues.tID 
  AND TestFullText = 'My favorite thing';

为什么要使用Oracle Text呢?

票数 0
EN

Stack Overflow用户

发布于 2017-12-21 09:42:34

域索引不是这样工作的。我们在源文本中索引关键字,所以我们实际上不会存储“我最喜欢的东西”,而是存储“我的”、“最喜欢的”和“东西”等等。

但是,您仍然可以通过使用索引作为初始过滤机制来获得好处,例如

代码语言:javascript
复制
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行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47905953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档