views:

54

answers:

3

Is it possible to get the information why/how given row returned by FTS query was matched (or which substring caused row to match)? For example, consider simpliest table with id and text columns, with FTS index on the later one.

SELECT * FROM Example
WHERE CONTAINS(text, 'FORMSOF(INFLECTIONAL, jump)');

This examplary query could return, say row {1, 'Jumping Jack'}.

Now, is it possible to somehow get information that this very row was matched because of 'Jumping' word? It doesn't even have to be exact information, more of a which substring caused row to match.

Why I'm asking - I got C# app that builds up those queries basing on user input (keywords to search for), and I need the very basic information why/how row was matched back, to use further in C# code.

If it's not possible, any alternatives?


EDIT in regards of Mike Burton's and LesterDove's replies:

The above example was trivial for obvious reasons and your solutions are ok having that in mind, however FTS queries might return results where regex or simple string matching (eg. LIKE) won't cut in. Consider:

Search for bind returns bound (past form).
Search for extraordinary returns amazing (synonym).

Both valid matches.

I've been looking for solutions to this problem and found this: NHunspell. However, I already got FTS & valid results using SQL Server, duplicating similar mechanism (building extra indexes, storing additional words/thezaurus files etc) doesn't look good.

Lester's answer however gave me some ideas that perhaps I could indeed split the original string to temporary table, and run the original FTS query on this split result. As it might work for my case (where DB is fairly small and queries are not very complex), in general case this approach might be out of question.

+1  A: 

One simple post-processing method would be to generate an equivalent Regular Expression for each WHERE clause article and use it to discover after the fact how the found data matches the specified pattern.

Mike Burton
+2  A: 

1/ Use a SPLIT function (many variations can be Googled) on your original substring, which will dump the individual substrings into a temp table of some sort, with one row per substring snippet.

2/ EDIT: You need to use CROSS APPLY to join to a table valued function:

SELECT * FROM Example E CROSS APPLY Split(E.text, ' ') AS S 
WHERE CONTAINS(E.text, 'FORMSOF(INFLECTIONAL, jump)') AND S.String LIKE '%jump%';

*NOTE: You need to forage for your own user-defined Split function. I used this one and applied the first commenter's edit to allow for the space character as a delimiter.

So, E is your Example table. You're still FT searching on the text field for the word 'jump'. And now you're "joining" to a table comprised of the individual substring values of your text field. Finally, you're matching that against the word 'jump' by using LIKE or Instr.

LesterDove
+1  A: 

You can get SQL to tell you how it interpreted your query, including how it transformed your input.

SELECT occurrence, special_term, display_term, expansion_type, source_term 
FROM sys.dm_fts_parser('FORMSOF(INFLECTIONAL, bind)', 1033, 0, 0)

returns

occurrence  special_term    display_term    expansion_type  source_term
1            Exact Match     binds           2          bind
1            Exact Match     binding         2          bind
1            Exact Match     bound           2          bind
1            Exact Match     bind            0          bind

This isn't precisely what you asked for, but it's a start. You could search your results for anything in the display_term column and probably figure out why it matched.

breischl