SQL Server Full Text Search Leading Wildcard - sql-server

After taking a look at this SO question and doing my own research, it appears that you cannot have a leading wildcard while using full text search.
So in the most simple example, if I have a Table with 1 column like below:
TABLE1
coin
coinage
undercoin
select COLUMN1 from TABLE1 where COLUMN1 LIKE '%coin%'
Would get me the results I want.
How can I get the exact same results with FULL TEXT SEARCH enabled on the column?
The following two queries return the exact same data, which is not exactly what I want.
SELECT COLUMN1 FROM TABLE1 WHERE CONTAINS(COLUMN1, '"coin*"')
SELECT COLUMN1 FROM TABLE1 WHERE CONTAINS(COLUMN1, '"*coin*"')

Full text search works on finding words or stems of words. Thus, it does not find the word "coin" anywhere in "undercoin". What you seek is the ability search suffixes using full text searches and it does not do this natively. There are some hacky workarounds like creating a reverse index and searching on "nioc".

Related

Finding matched words using SQL Server Full Text Search FREETEXT function

I'm trying to figure out where the matches were found when using FREETEXT so I can extract the paragraph in which they appear.
I can do this using CONTAINS by searching the the exact phrase in the document. However, because FREETEXT uses a more "fuzzy" approach and uses synonyms, I have no idea what it matched on.
For example, assume I have a column that includes the text "...this will help marginalized communities..."
select #CONTAINS(some_column, '"marginalized groups"')
will return the example column above. But, I can't find the paragraph is appears in because I'm looking for one search term, but FREETEXT is smart enough to find similar terms.
Is there any way to find what the actual match was?

SQL Contains() with a single word does not return all expected rows

I am running a simple SQL contains search and the result does not include all expected rows.
All I need is a search that works in the same way as LIKE %word%.
SELECT *
FROM [MyTable] where contains(Name, N'walmart')
By running this select, the rows returned seem to only contains name with "walmart" with either space before or after or some kind of other delimiter like a period ("walmart.com"). No problem here.
But one row was not returned and its value is "mywalmart". Why does this row fail to return with the contains search? If I use name LIKE '%walmart%' it works just fine.
What do I need to fix the contains search to make it work?
Contains doesn't work the way you think, for your purposes LIKE is best.
See the docs on this:
CONTAINS can search for:
A word or phrase.
The prefix of a word or phrase.
A word near another word.
A word inflectionally generated from another (for example, the word drive is the inflectional stem of drives, drove, driving, and driven).
A word that is a synonym of another word using a thesaurus (for example, the word "metal" can have synonyms such as "aluminum" and "steel").

How to search for similar words in SQL Server

I am using CONTAINS and FREETEXT on SQL query to search for text in big text fields.
What I noticed that the search returns result when the exact word match, but what if I want to search for similar words?
For example, when I type Carlo, it did not display anything if what I have is Carlos (with an S)
Below is a simple query similar to the one I use:
SELECT P.*
FROM MyTable AS P
WHERE(CONTAINS(P.*, 'Carlo') OR freetext(P.*, 'Carlo'))
How can I make the search bring similar words to Carlo such as Carlos, Carla, etc... without affecting the performance?
Try this
SELECT P.*
FROM MyTable AS P
WHERE CONTAINS(P.*, 'FORMSOF(INFLECTIONAL, "Carlo")')
For reference you can check documentation

Matching words in close proximity

I have a table in SQL 2012 that I'm performing a full text search on.
One of the records has, as a part of a larger string, the text 'Trying out your system'.
The problem is that, if I search for two words in the target string which are too close together, I don't get a match.
select * from mytable where contains(*,'trying') -- match
select * from mytable where contains(*,'trying and out') -- no match
select * from mytable where contains(*,'trying and your') -- no match
select * from mytable where contains(*,'trying and system') -- match
I'm aware that I can search for an exact string by enclosing the search pattern in double quotes, however that's not really what I'm after.
Any suggestions how I can make all of the above search terms match?
Thanks.
This sounds like an issue with stopwords (common words like "the", "your", etc. that are usually filtered out of the full text index, thus you cannot search on them).
To prevent this from happening, you can modify your full text index so that it does not use a stoplist (in other words, every single word will be indexed and thus searchable).
ALTER FULLTEXT INDEX ON MyTable SET STOPLIST = OFF
Be sure to rebuild the full text catalog afterwards.
But only do this if you really need the ability to search on common words. Typically this is not necessary. Also, doing so may slow down your full text searches.

how to do prefix and suffix search on full text indexed table

How to query a full text table like "like" operator? Suffix search is not fetching the result... i am using contains and searching in all the columns.Can you please help , how to do suffix search on full text indexed table
You can perform a prefix search using the wildcard operator:
SELECT Description, ProductDescriptionID
FROM Production.ProductDescription
WHERE CONTAINS (Description, ' "top*" ' );
(Source)
Suffix searches are not supported. You have to do a hack like keep a copy of the reversed text as shown here.

Resources