Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

tsql - FullText search with CONTAINS on multiple columns and predicate - AND

I have a search table with, say, 4 columns of text data to search.

I do something like this:

SELECT * FROM dbo.SearchTable
WHERE CONTAINS((co1, col2, col3, col4), 'term1 AND term2')

It looks like Contains only returns true if term1 and term2 are in the same column. Is there any way to specify that all columns should be included with an AND?

If not, my idea is to JSON all search columns and stick them into one. That way I can full text search them but still easily extract the individual columns in .NET. I'm presuming that the indexer won't have a problem with this and will dispense with the JSON characters and quotes. Is this correct?

Thanks

EDIT

Thinking about the JSON idea, the crawler would also index the property names so I'd have to rename {name}, {details}, {long_details} to something like {x1}, {x2}, {x3} to ensure they'd not be picked in a search. Hopefully if they're so short they wouldn't be indexed anyway.

EDIT2

I can create a Stoplist, based on the system Stoplist and put the property names into that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work:

SELECT * FROM dbo.SearchTable
WHERE CONTAINS((co1, col2, col3, col4), 'term1')
AND CONTAINS((co1, col2, col3, col4), 'term2');

Alternatively, you could add a new computed column with a full text index on it. Add a column like this:

computedCol AS col1 + ' ' + col2 + ' ' + col3 + ' ' + col4

And create the full text index:

CREATE FULLTEXT INDEX ON SearchTable (computedCol LANGUAGE 1033)
KEY INDEX pk_SearchTable_yourPrimaryKeyName

Then you can do this:

SELECT * FROM dbo.SearchTable
WHERE CONTAINS(*, 'term1 AND term2')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...