Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Other Technologies / Full-Text Search / November 2007

Tip: Looking for answers? Try searching our database.

Contains clause with only NOT keywords

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Kotuby - 07 Nov 2007 13:34 GMT
Hello everyone,

I posted this on sqlserver.programming and it was recommended I try this
group.

I am designing a search screen that searches for keywords in Text fields as
well as searching other related tables with fields like Date ranges and
other lookup code fields. One of our users asked why they can't use a
Date-Range search in conjunction with keywords NOT found in the free text. I
have read that it is not possible to do with Contains.

For example, a standard keyword search might create this Contains clause:
contains((desciption),'("cat" & "dog") and ("horse") &! "cow" &! "bull"')

The users just want to use the &! "cow" &! "bull" part of the Contains query
along with other more standard Where criteria, for example "and OrderDate >
'10/10 2006' ".

I have tried to pass "noise" words for the first part of the Contains, but
they are ignored.

I also tried separating out the NOT keywords into a series of " and not
description like 'bull%' " type filters, but the performance becomes
intolerably slow.

Is there any way to get around this problem?  Maybe some crafty trickery?

Thanks to all...
Hilary Cotter - 09 Nov 2007 12:31 GMT
You have to parse your query so that it looks like this:

select * from John where  contains(*,'("cat" AND "dog" AND "horse") AND NOT
( "cow" AND "bull")')

I have upper cased the boolean operators for clarity.

For your date query it would look like this

select * from John where  contains(*,'("cat" AND "dog" AND "horse") AND NOT
( "cow" AND "bull")')
where orderdate>'2007-01-01'

You cannot search on a date string and hope for it to be interpreted as a
date and do inequality operations on it.  So I could not do something like
this

select * from John where  contains(*,'("cat" AND "dog" AND "horse") AND NOT
( "cow" AND "bull") and OrderDate>'2007-01-01')

as sql FTS can only interpret the date string as a string and only do not
equal or equal operations against it.

Signature

RelevantNoise.com - dedicated to mining blogs for business intelligence.

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html

Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com

> Hello everyone,
>
[quoted text clipped - 28 lines]
>
> Thanks to all...
ML - 09 Nov 2007 13:52 GMT
FYI - the original thread can be found here
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.sqlserver.
programming&mid=b965bf2b-ce05-4ad2-baee-47205465946b


As I understand it, he OP was trying to find out how to combine a negative
FTI search (using CONTAINS) with additional restrictions in the WHERE caluse.
I suggested building the condition using NOT(CONTAINS()).


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
Hilary Cotter - 09 Nov 2007 15:38 GMT
thanks ML - that is an interesting approach. That should work, but it would
be expensive if the results set was large.

Signature

RelevantNoise.com - dedicated to mining blogs for business intelligence.

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html

Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com

> FYI - the original thread can be found here:
> http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.sqlserver.
programming&mid=b965bf2b-ce05-4ad2-baee-47205465946b

[quoted text clipped - 9 lines]
> Matija Lah, SQL Server MVP
> http://milambda.blogspot.com/
John Kotuby - 09 Nov 2007 14:16 GMT
Thanks Hilary...

I guess I confused the issue by mentioning the Date comparison in my post.
The crux of the matter was whether I could use JUST exclusions in the
Contains clause.  For example...

contains((description),'(!"cow" &! "bull")')

The idea being that any record is acceptable, as long as the 'description'
field does NOT contain either "cow" OR "bull"

In your syntax I guess that would translate into
contains((description), 'NOT ( "cow" AND "bull")').

Someone in the SQLServer.Programming group suggested:

where NOT(contains((description),'"cow" OR "bull"'))
   and <the rest of the conditions>

That seems to work well, and now I use it whenever the program sees that
there were no keywords entered into any of the text boxes for  each of the
conditions "With ALL of the Words:" or "With at Least ONE of the Words:",
and the user has placed entries only in "With NONE of the Words:".

My code now becomes (when only exclusions are entered):

Where NOT (contains((description),'("cow" | "bull")') OR
contains((long_name),'("cow" | "bull")') )
AND ( list_date > '10/01/2007')

Note here that the date comparison is completely separated from the Contains
clause, as you have indicated.

However, I have noticed that you use the * in your contains example, whereas
I am creating a "contains" for each Full Text Indexed field being queried.

Does this mean I can consolidate the query I am using with * instead of
querying each field separately? In my Text Catalog, only the 2 fields
"description" and "long_name" are indexed.

Thanks again...

> You have to parse your query so that it looks like this:
>
[quoted text clipped - 52 lines]
>>
>> Thanks to all...
Hilary Cotter - 09 Nov 2007 15:35 GMT
No, you can't do this. Basically this is like saying, search on every word
in the dictionary and return hits to every document and then subtract the
ones which contain cow or bull.

Signature

RelevantNoise.com - dedicated to mining blogs for business intelligence.

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html

Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com

> Thanks Hilary...
>
[quoted text clipped - 98 lines]
>>>
>>> Thanks to all...
John Kotuby - 09 Nov 2007 18:48 GMT
Thanks Hilary,

But just a little clarification please...

When you say

"No, you can't do this. "

Did you mean my code example as suggested by ML (and which is currently
written and operating in test mode)?

Or were you refering to my question of using the * in the syntax.

Sorry to bother you again, but I will be putting the code into production
soon...

> No, you can't do this. Basically this is like saying, search on every word
> in the dictionary and return hits to every document and then subtract the
[quoted text clipped - 103 lines]
>>>>
>>>> Thanks to all...
Hilary Cotter - 12 Nov 2007 15:54 GMT
No, MLs code should work. I was referring the the wildcard usage.

Signature

RelevantNoise.com - dedicated to mining blogs for business intelligence.

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html

Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com

> Thanks Hilary,
>
[quoted text clipped - 121 lines]
>>>>>
>>>>> Thanks to all...
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.