I am currently working on a project where I want to search for employees through only one input search query. For this I use SQL FTS.
The table layout looks like this:
Employee table
EmployeeId, Firstname, Lastname
Data examples
1, John, Miller
2, Chuck, Norris
Address table
AddressId, EmployeeId, CityId, Street, StreetNumber
Data examples
1, 1, 1, Avenue, 12
2, 2, 2, Wimbledon Rd, 12
City table
CityId, Name, ZipCode
Data examples
1, Hamburg, 22335
2, London, 12345
So now I have received the following search query:
- John Hamburg: means John And Hamburg and must return 1 record.
- John London: means John and London and should return 0 entries since there is no John in London.
- Norris Wimbledon: means Norris and Wimbledon and must return 1 record.
Now the problem is that using CONTAINSTABLE only allows you to search one table at a time. Thus, the use of "John AND Hamburg" in Employee The full text catalog returns 0 records, since "Hamburg" is in the address table.
Therefore, at present, I can only use βORβ instead of βAND,β for example:
SELECT (keyTblSp.RANK * 3) AS [Rank], sp.* FROM Employee sp INNER JOIN CONTAINSTABLE(Employee, *, 'John OR Hamburg', 1000) AS keyTblSp ON sp.EmployeeId = keyTblSp.[KEY] UNION ALL SELECT (keyTbl.RANK * 2) AS [Rank], sp.* FROM Employee sp LEFT OUTER JOIN [Address] addr ON addr.EmployeeId = sp.EmployeeId INNER JOIN CONTAINSTABLE([Address], *, 'John OR Hamburg', 1000) AS keyTbl ON addr.AddressId = keyTbl.[KEY] UNION ALL SELECT (keyTbl.RANK * 2) AS [Rank], sp.* FROM Employee sp LEFT OUTER JOIN [Address] addr ON addr.EmployeeId = sp.EmployeeId LEFT OUTER JOIN [City] cty ON cty.CityId = addr.CityId INNER JOIN CONTAINSTABLE([City], *, 'John OR Hamburg', 1000) AS keyTbl ON cty.CityId = keyTbl.[KEY]
This leads to the fact that not only John, who lives in Hamburg, but every person named John and every person who lives in Hamburg returns. One solution I could think of is to somehow compute a column in the Employee table that contains all the necessary values ββfor a full-text search.
Employee table
EmployeeId, Firstname, Lastname, FulltextColumn
Data examples
1 | John | Miller | John Miller Avenue 12 Hamburg 22335
So I could do
SELECT (keyTbl.RANK) AS [Rank], sp.* FROM Employee sp INNER JOIN CONTAINSTABLE([Employee], FulltextColumn, 'John AND Hamburg', 1000) AS keyTbl ON sp.EmployeeId = keyTbl.[KEY]
Is it possible? Any other ideas?