I have a simple table with registered directory names inside a full text indexed varchar column.
For example:
- 'c:\Dir1\'
- 'c:\Dir1\Sub1\'
- 'c:\Dir1\Sub2\'
- 'c:\Dir2\'
- 'c:\Dir3\'
I now need to find the closest subdirectory to file paths like:
'c:\Dir1\File.txt' -> 'c:\Dir1\'
'c:\Dir1\Sub2\File.txt' -> 'c:\Dir1\Sub2\'
'c:\Dir2\Sub1\SubSub1\File.txt' -> 'c:\Dir2\'
All my efforts with CONTAINS and NEAR failed.
Can somebody help me how I can solve this with text search?
Best Answer
If you take the path of interest and parse it into it's sub part, and use those to build a search string, you should then be able to use that search string to find matches of all lengths and then pick the best fit. There is an assumption that the folder structure is embedded in other data within the field (otherwise, why use Full Text Indexing), so the manner of finding the best match will depending on your specific context.
Here is some code the demonstrates the suggested path, include a sample way of selecting the best fit.
Notes: 1. To preserve formatting in SO, I changed '\' to '\'. 2. I did not have an environment to test the Full Index Search, so some tweaking may be needed. 3. The selection criteria in the 'best fit' may produce inconsistent results if there are multiple matches with the same score (wobble). If this is of concern, additional ORDER BY criteria should be specified.