Question

I am using MS SQL server 2012, and I have two tables:

1) Keyword_table that contains list of keywords, like:

keyword, nvarchar(50)
-------------------
web
book
AI
work
...

2) art_table that contains IDs and list of articles, like:

artID,  int         art,  nvarchar(MAX)
---------------------------------------
1                  The Web is a system of ...
2                  AI is the intelligence exhibited by machines or software ...
3                  The Web includes Web 1.0, Web 2.0 and Web 3.0
4                  The work done by ...
....

I want to search each keyword in the keyword_table to get the number of articles that contain that keyword, for example, based on the data above the result should be like this:

keyword            No of articles
----------------------------------
web                2
book               0
AI                 1
work               1

The art_table has few millions records, and it has full text index on art column, and I know that I can use contains to search for a simple term (single word/phrase) like this:

select *
from art_table
where contains (art, 'web')

And to count the number of the articles that contains web:

select count(*)
from art_table
where contains (art, 'web')

BUT how to do it for searching the ALL keywords in keyword_table?

Thanks in advance!

Was it helpful?

Solution 2

In case someone come across with the same need, here is a suggested answer:

DECLARE @kword nvarchar(200);
DECLARE keywordCursor CURSOR FOR(SELECT keyword FROM keyword_table);
OPEN keywordCursor;
FETCH NEXT FROM keywordCursor INTO @kword;
WHILE @@FETCH_STATUS=0
BEGIN
    INSERT INTO keyword_occ(keyword,occ)
    VALUES(@kword,(SELECT count(*) FROM art_table WHERE CONTAINS(art, @kword)));
    FETCH NEXT FROM keywordCursor INTO @kword;
END;
CLOSE keywordCursor;
DEALLOCATE keywordCursor;
GO

Where keyword_occ is a table (keyword nvarchar(200), occ int) to save the required result.

I wish if I could avoid WHILE loop, however, this script works well and fast with millions indexed records!

OTHER TIPS

Try something like this:

select a.* from art_table a
inner join keyword_table b
on a.art like '%' + b.keyword + '%'

That should give you a match based on the keyword and return all records that has the keyword somewhere in it.

[Edit]

Perhaps try this?

select a.* from art_table a
inner join keyword_table b
on contains(a.art,  b.keyword)

Note I haven't syntax tested this :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top