Showing posts with label containstable. Show all posts
Showing posts with label containstable. Show all posts

Thursday, March 8, 2012

Can CONTAINSTABLE get its search terms from a table?

(SQL Server 2000, SP3)
Hello all!
We've got an issue with a Full Text Search query that is failing because of too many
search terms in the CONTAINSTABLE clause (upwards of 3000 elements separated with " OR ",
I think).
I was wondering if the CONTAINSTABLE clause could somehow be refactored to obtain its
search terms from a temporary table? I certainly don't see any evidence of this in BOL --
but I thought I'd check here.
Thanks for any help you can provide! :-)
John PetersonJohn,
Are you getting a syntax error on the FTS query? If so, what is the syntax
error? If you reduce the number of terms, i.e., reduce the total length of
the search string, does the error go away? There was a bug in the past
builds of SQL Server in regards to the length of the search string, but it
was fixed in an early SP.
Is the containstable query in a stored procedure or is it an ad hoc query?
You could "refactor" the query to use a temp table of terms, but that would
most likely require cursors or a while loop, both could be detrimental to
the SQL FTS query performance... Instead, try something like this and change
contains to containstable and be careful and use the correct number of
single quotes...
use pubs
go
DROP PROCEDURE usp_FTSearchPubsInfo
go
CREATE PROCEDURE usp_FTSearchPubsInfo ( @.vcSearchText varchar(7800))
AS
declare @.s as varchar (8000)
set @.s='select pub_id, pr_info from pub_info where
contains(pr_info,'+''''+@.vcSearchText+''''+')'
exec (@.s)
go
-- Small / Simple example of mutiple parameters...
EXEC usp_FTSearchPubsInfo '("pulp*") or ("waste" and "paper" or
"wastepaper") or
("recycle* paper") or (("paper slurry") and ("paper sludge")) or
("biodegrad* paper") or
("paper" and "dispos*") or (("paper" near "bleach*") or ("paper" near
"chemical*"))'
go
With usp_FTSearchPubsInfo you can add to the length of the search string up
to 8000 bytes (the max varchar size), but I've not tested this on SQL 2000
SP3 to 8000 bytes, so, your 3000 elements *might* exceed this limit... Give
it a try and let us know...
Regards,
John
"John Peterson" <j0hnp@.comcast.net> wrote in message
news:#faBMtBpDHA.2064@.TK2MSFTNGP11.phx.gbl...
> (SQL Server 2000, SP3)
> Hello all!
> We've got an issue with a Full Text Search query that is failing because
of too many
> search terms in the CONTAINSTABLE clause (upwards of 3000 elements
separated with " OR ",
> I think).
> I was wondering if the CONTAINSTABLE clause could somehow be refactored to
obtain its
> search terms from a temporary table? I certainly don't see any evidence
of this in BOL --
> but I thought I'd check here.
> Thanks for any help you can provide! :-)
> John Peterson
>|||Hello John!
I *was* getting a syntax error in the FTS query -- and it would go away when I removed a
bunch of the search terms. This was the error:
Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near ' "Aberdeen" OR "Acton" OR "Acworth" OR "Addison" OR
"Advance" OR "Ajax" OR "Alameda" OR "Albuquerque" OR "Algonquin" OR "Aliquip'.
The query is an ad-hoc query that gets built by an .ASP application and sent to SQL Server
via an ADOConnection/ADORecordset.
Alas, the string that's being sent is like 20K -- so I think that it far exceeds that 8000
character limit. :-(
Fundamentally, I think we need to change our searching component to ensure that people
can't enter in a billion search terms. But, by the same token, I felt that if we could
fix this relatively quickly/easily, that would be good too.
Any other ideas that we might be able to try?
As always, thank you so much for your help! :-)
John Peterson
"John Kane" <jt-kane@.comcast.net> wrote in message
news:%23qAU02BpDHA.2732@.TK2MSFTNGP11.phx.gbl...
> John,
> Are you getting a syntax error on the FTS query? If so, what is the syntax
> error? If you reduce the number of terms, i.e., reduce the total length of
> the search string, does the error go away? There was a bug in the past
> builds of SQL Server in regards to the length of the search string, but it
> was fixed in an early SP.
> Is the containstable query in a stored procedure or is it an ad hoc query?
> You could "refactor" the query to use a temp table of terms, but that would
> most likely require cursors or a while loop, both could be detrimental to
> the SQL FTS query performance... Instead, try something like this and change
> contains to containstable and be careful and use the correct number of
> single quotes...
> use pubs
> go
> DROP PROCEDURE usp_FTSearchPubsInfo
> go
> CREATE PROCEDURE usp_FTSearchPubsInfo ( @.vcSearchText varchar(7800))
> AS
> declare @.s as varchar (8000)
> set @.s='select pub_id, pr_info from pub_info where
> contains(pr_info,'+''''+@.vcSearchText+''''+')'
> exec (@.s)
> go
> -- Small / Simple example of mutiple parameters...
> EXEC usp_FTSearchPubsInfo '("pulp*") or ("waste" and "paper" or
> "wastepaper") or
> ("recycle* paper") or (("paper slurry") and ("paper sludge")) or
> ("biodegrad* paper") or
> ("paper" and "dispos*") or (("paper" near "bleach*") or ("paper" near
> "chemical*"))'
> go
> With usp_FTSearchPubsInfo you can add to the length of the search string up
> to 8000 bytes (the max varchar size), but I've not tested this on SQL 2000
> SP3 to 8000 bytes, so, your 3000 elements *might* exceed this limit... Give
> it a try and let us know...
> Regards,
> John
>
>
> "John Peterson" <j0hnp@.comcast.net> wrote in message
> news:#faBMtBpDHA.2064@.TK2MSFTNGP11.phx.gbl...
> > (SQL Server 2000, SP3)
> >
> > Hello all!
> >
> > We've got an issue with a Full Text Search query that is failing because
> of too many
> > search terms in the CONTAINSTABLE clause (upwards of 3000 elements
> separated with " OR ",
> > I think).
> >
> > I was wondering if the CONTAINSTABLE clause could somehow be refactored to
> obtain its
> > search terms from a temporary table? I certainly don't see any evidence
> of this in BOL --
> > but I thought I'd check here.
> >
> > Thanks for any help you can provide! :-)
> >
> > John Peterson
> >
> >
>|||You're welcome, John
That's the syntax error I suspected you were getting... Yes, I think it
would be a good idea to change your "searching component to ensure that
people can't enter in a billion search terms" as even Google limits the
number of *effective* search terms to 10, even though you can enter as many
search terms as you want. Perhaps adding a "tips" or "help" statement to
your ASP application page might be helpful as well, stating a limit of 10
(or whatever number) of search words are allowed...
Relative to the below stored procedure, and a "quick fix" for this would be
allowing your users to enter a trailing * (asterisk), for example book* - to
find book, books, booking, booked, etc. so that they will not have to enter
all word variations. Note, also add a tip that only a trailing * (asterisk)
is allowed as SQL FTS only supports this syntax...
Regards,
John
"John Peterson" <j0hnp@.comcast.net> wrote in message
news:eUMRMFCpDHA.3612@.TK2MSFTNGP11.phx.gbl...
> Hello John!
> I *was* getting a syntax error in the FTS query -- and it would go away
when I removed a
> bunch of the search terms. This was the error:
> Server: Msg 170, Level 15, State 1, Line 5
> Line 5: Incorrect syntax near ' "Aberdeen" OR "Acton" OR "Acworth" OR
"Addison" OR
> "Advance" OR "Ajax" OR "Alameda" OR "Albuquerque" OR "Algonquin" OR
"Aliquip'.
> The query is an ad-hoc query that gets built by an .ASP application and
sent to SQL Server
> via an ADOConnection/ADORecordset.
> Alas, the string that's being sent is like 20K -- so I think that it far
exceeds that 8000
> character limit. :-(
> Fundamentally, I think we need to change our searching component to ensure
that people
> can't enter in a billion search terms. But, by the same token, I felt
that if we could
> fix this relatively quickly/easily, that would be good too.
> Any other ideas that we might be able to try?
> As always, thank you so much for your help! :-)
> John Peterson
>
> "John Kane" <jt-kane@.comcast.net> wrote in message
> news:%23qAU02BpDHA.2732@.TK2MSFTNGP11.phx.gbl...
> > John,
> > Are you getting a syntax error on the FTS query? If so, what is the
syntax
> > error? If you reduce the number of terms, i.e., reduce the total length
of
> > the search string, does the error go away? There was a bug in the past
> > builds of SQL Server in regards to the length of the search string, but
it
> > was fixed in an early SP.
> >
> > Is the containstable query in a stored procedure or is it an ad hoc
query?
> > You could "refactor" the query to use a temp table of terms, but that
would
> > most likely require cursors or a while loop, both could be detrimental
to
> > the SQL FTS query performance... Instead, try something like this and
change
> > contains to containstable and be careful and use the correct number of
> > single quotes...
> >
> > use pubs
> > go
> > DROP PROCEDURE usp_FTSearchPubsInfo
> > go
> > CREATE PROCEDURE usp_FTSearchPubsInfo ( @.vcSearchText varchar(7800))
> > AS
> > declare @.s as varchar (8000)
> > set @.s='select pub_id, pr_info from pub_info where
> > contains(pr_info,'+''''+@.vcSearchText+''''+')'
> > exec (@.s)
> > go
> >
> > -- Small / Simple example of mutiple parameters...
> > EXEC usp_FTSearchPubsInfo '("pulp*") or ("waste" and "paper" or
> > "wastepaper") or
> > ("recycle* paper") or (("paper slurry") and ("paper sludge")) or
> > ("biodegrad* paper") or
> > ("paper" and "dispos*") or (("paper" near "bleach*") or ("paper" near
> > "chemical*"))'
> > go
> >
> > With usp_FTSearchPubsInfo you can add to the length of the search
string up
> > to 8000 bytes (the max varchar size), but I've not tested this on SQL
2000
> > SP3 to 8000 bytes, so, your 3000 elements *might* exceed this limit...
Give
> > it a try and let us know...
> >
> > Regards,
> > John
> >
> >
> >
> >
> > "John Peterson" <j0hnp@.comcast.net> wrote in message
> > news:#faBMtBpDHA.2064@.TK2MSFTNGP11.phx.gbl...
> > > (SQL Server 2000, SP3)
> > >
> > > Hello all!
> > >
> > > We've got an issue with a Full Text Search query that is failing
because
> > of too many
> > > search terms in the CONTAINSTABLE clause (upwards of 3000 elements
> > separated with " OR ",
> > > I think).
> > >
> > > I was wondering if the CONTAINSTABLE clause could somehow be
refactored to
> > obtain its
> > > search terms from a temporary table? I certainly don't see any
evidence
> > of this in BOL --
> > > but I thought I'd check here.
> > >
> > > Thanks for any help you can provide! :-)
> > >
> > > John Peterson
> > >
> > >
> >
> >
>

Can CONTAINSTABLE be made to match unconditionally?

Say that users searching a Books database are able to search by Author,
Title, and Publisher using three separate text boxes. They can choose to
leave any or all text boxes empty if they do not care to filter by that
particular item.
Say I have a query that looks similar to this:
b.* FROM Books b
INNER JOIN CONTAINSTABLE( Books, Author, @.author) authorRank ON b.BookId =
authorRank.[KEY]
INNER JOIN CONTAINSTABLE( Books, Title, @.title) titleRank ON ( b.BookId =
titleRank.[KEY] )
INNER JOIN CONTAINSTABLE( Books, Publisher, @.publisher) publisherRank ON (
b.BookId = publisherRank.[KEY] )
This works great if the user chooses to enter something in each of the three
text boxes. However, CONTAINSTABLE does not accept an empty string for the
search condition (nor does it accept *, %, or other wildcards except when
searching for a prefix). How can I get the query to unconditionally match
title and publisher if they leave title and publisher blank but type
something for author?
If there is no way to allow any title and publisher to be returned when the
user leaves those text boxes blank, I will need to write a query for if they
type all three text boxes, a query for if they type in none of the text
boxes, a query if they type in just the publisher but not the title or
author, a query... etc. Not a good solution.
How can I achieve the results I want without writing multiple queries?
Thank you.Hi, Greg
Try something like this (untested):
SELECT * FROM Books
WHERE (@.author IS NULL OR BookId IN (
SELECT KEY FROM CONTAINSTABLE(Books, Author, @.author)
)) AND (@.title IS NULL OR BookId IN (
SELECT KEY FROM CONTAINSTABLE(Books, Title, @.title)
)) AND (@.publisher IS NULL OR BookId IN (
SELECT KEY FROM CONTAINSTABLE(Books, Publisher, @.publisher)
))
Razvan|||That doesn't let me use any of the ranking information provided by
CONTAINSTABLE. It may be workable. I may have found some sort of
sp_configure setting that will change the behavior of noise words. I'm not
sure about it yet.
"Razvan Socol" wrote:

> Hi, Greg
> Try something like this (untested):
> SELECT * FROM Books
> WHERE (@.author IS NULL OR BookId IN (
> SELECT KEY FROM CONTAINSTABLE(Books, Author, @.author)
> )) AND (@.title IS NULL OR BookId IN (
> SELECT KEY FROM CONTAINSTABLE(Books, Title, @.title)
> )) AND (@.publisher IS NULL OR BookId IN (
> SELECT KEY FROM CONTAINSTABLE(Books, Publisher, @.publisher)
> ))
> Razvan
>|||> That doesn't let me use any of the ranking information provided by
> CONTAINSTABLE.
In this case, you might want to use something like this (also
untested):
SELECT b.*, x.RANK, y.RANK, z.RANK FROM Books b
LEFT JOIN CONTAINSTABLE(Books, Author, @.author) x ON b.BookID=x.[KEY]
LEFT JOIN CONTAINSTABLE(Books, Title, @.title) y ON b.BookID=y.[KEY]
LEFT JOIN CONTAINSTABLE(Books, Publisher, @.publisher) z ON
b.BookID=z.[KEY]
WHERE (@.author IS NULL OR x.[KEY] IS NOT NULL)
AND (@.title IS NULL OR y.[KEY] IS NOT NULL)
AND (@.publisher IS NULL OR z.[KEY] IS NOT NULL)
Razvan|||If @.author is NULL (or blank), then CONTAINSTABLE will throw an error, which
is pretty much the entire problem here.
"Razvan Socol" wrote:

> In this case, you might want to use something like this (also
> untested):
> SELECT b.*, x.RANK, y.RANK, z.RANK FROM Books b
> LEFT JOIN CONTAINSTABLE(Books, Author, @.author) x ON b.BookID=x.[KEY]
> LEFT JOIN CONTAINSTABLE(Books, Title, @.title) y ON b.BookID=y.[KEY]
> LEFT JOIN CONTAINSTABLE(Books, Publisher, @.publisher) z ON
> b.BookID=z.[KEY]
> WHERE (@.author IS NULL OR x.[KEY] IS NOT NULL)
> AND (@.title IS NULL OR y.[KEY] IS NOT NULL)
> AND (@.publisher IS NULL OR z.[KEY] IS NOT NULL)
> Razvan
>|||> If @.author is NULL (or blank), then CONTAINSTABLE will throw an error [...]
Aha! I told you it was untested... :) I assumed that CONTAINSTABLE will
return an empty resultset when given a NULL search condition.
Obviously, I was wrong.
In this case, we can use a non-existent word instead of NULL, like
this:
SET @.title=ISNULL(@.title,'NotSpecified')
SET @.author=ISNULL(@.author,'NotSpecified')
SET @.publisher=ISNULL(@.publisher,'NotSpecifi
ed')
SELECT b.*, x.RANK, y.RANK, z.RANK FROM Books b
LEFT JOIN CONTAINSTABLE(Books, Author, @.author) x ON b.BookId=x.[KEY]
LEFT JOIN CONTAINSTABLE(Books, Title, @.title) y ON b.BookId=y.[KEY]
LEFT JOIN CONTAINSTABLE(Books, Publisher, @.publisher) z ON
b.BookId=z.[KEY]
WHERE (@.author='NotSpecified' OR x.[KEY] IS NOT NULL)
AND (@.title='NotSpecified' OR y.[KEY] IS NOT NULL)
AND (@.publisher='NotSpecified' OR z.[KEY] IS NOT NULL)
If you think the word "NotSpecified" may appear in a title of a book,
you can change it with another inexisting word.
Razvan