{"id":15003179,"url":"https://github.com/softcircuits/fulltextsearchquery","last_synced_at":"2025-10-30T09:31:28.914Z","repository":{"id":34995908,"uuid":"194898805","full_name":"SoftCircuits/FullTextSearchQuery","owner":"SoftCircuits","description":"Converts simple query to SQL Server full-text-search query.","archived":false,"fork":false,"pushed_at":"2024-12-24T00:18:54.000Z","size":63,"stargazers_count":20,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-02T07:51:14.802Z","etag":null,"topics":["full-text-search","fulltext-search","fulltextsearch","microsoft-sql-server","sql-server"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SoftCircuits.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-02T16:27:56.000Z","updated_at":"2024-12-24T00:18:58.000Z","dependencies_parsed_at":"2023-01-15T11:42:01.405Z","dependency_job_id":null,"html_url":"https://github.com/SoftCircuits/FullTextSearchQuery","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FFullTextSearchQuery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FFullTextSearchQuery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FFullTextSearchQuery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCircuits%2FFullTextSearchQuery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoftCircuits","download_url":"https://codeload.github.com/SoftCircuits/FullTextSearchQuery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238950485,"owners_count":19557533,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["full-text-search","fulltext-search","fulltextsearch","microsoft-sql-server","sql-server"],"created_at":"2024-09-24T18:56:39.081Z","updated_at":"2025-10-30T09:31:28.562Z","avatar_url":"https://github.com/SoftCircuits.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FullTextSearchQuery\n\n[![NuGet version (SoftCircuits.FullTextSearchQuery)](https://img.shields.io/nuget/v/SoftCircuits.FullTextSearchQuery.svg?style=flat-square)](https://www.nuget.org/packages/SoftCircuits.FullTextSearchQuery/)\n\n```\nInstall-Package SoftCircuits.FullTextSearchQuery\n```\n\nFullTextSearchQuery is a .NET class library that converts a user-friendly search term into a valid Microsoft SQL Server full-text-search query. The code attempts to detect and handle all cases where the query would otherwise cause SQL Server to generate an error.\n\n# Introduction\nMicrosoft SQL Server provides a powerful full-text search feature. However, the syntax is rather cryptic, especially for non-programmers. Moreover, there are many conditions that can cause SQL Server to throw up an error if things aren't exactly right.\n\nFullTextSearchQuery converts a user-friendly, Google-like search term to the corresponding full-text search SQL query condition. Its goal is to never throw exceptions on badly formed input. It simply constructs the best valid query it can from the input.\n\n# Examples\nThe following list shows how various input are transformed.\n\n| Input | Output | Description |\n| ---- | ---- | ---- |\n| abc | `FORMSOF(INFLECTIONAL, abc)` | Find inflectional forms of abc.\n| ~abc | `FORMSOF(THESAURUS, abc)` | Find thesaurus variations of abc.\n| \"abc\" | `\"abc\"` | Find exact term abc.\n| +abc | `\"abc\"` | Find exact term abc.\n| \"abc\" near \"def\" | `\"abc\" NEAR \"def\"` | Find exact term abc near exact term def.\n| abc* | `\"abc*\"` | Finds words that start with abc.\n| -abc def | `FORMSOF(INFLECTIONAL, def) AND NOT FORMSOF(INFLECTIONAL, abc)` | Find inflectional forms of def but not inflectional forms of abc. |\n| abc def | `FORMSOF(INFLECTIONAL, abc) AND FORMSOF(INFLECTIONAL, def)` | Find inflectional forms of both abc and def.\n| abc or def | `FORMSOF(INFLECTIONAL, abc) OR FORMSOF(INFLECTIONAL, def)` | Find inflectional forms of either abc or def.\n| \u0026lt;+abc +def\u0026gt; | `\"abc\" NEAR \"def\"` | Find exact term abc near exact term def.\n| abc and (def or ghi) | `FORMSOF(INFLECTIONAL, abc) AND (FORMSOF(INFLECTIONAL, def) OR FORMSOF(INFLECTIONAL, ghi))` | Find inflectional forms of both abc and either def or ghi.\n\n# Preventing SQL Server Errors\nEven after a syntactically correct query has been generated, SQL Server can still generate an error for some queries. For example, in the table above you can see that the ouput for `-abc def` swaps the two subexpressions. This is because `NOT FORMSOF(INFLECTIONAL, abc) AND FORMSOF(INFLECTIONAL, def)` will cause an error. SQL Server does not like the `NOT` at the start. In this example, FullTextSearchQuery will swaps the two subexpressions (on either side of `AND`).\n\nAfter constructing a query, FullTextSearchQuery will check for this and several other error conditions and make corrections as necessary. The following table describes these conditions.\n\n| Term | Action Taken\n| ---- | ----\n| NOT term1 AND term2 | Subexpressions swapped.\n| NOT term1 | Expression discarded.\n| NOT term1 AND NOT term2 | Expression discarded if node is grouped (parenthesized) or is the root node; otherwise, the parent node may contain another subexpression that will make this one valid.\n| term1 OR NOT term2 | Expression discarded.\n| term1 NEAR NOT term2 | NEAR conjunction changed to AND.\n\nFullTextSearchQuery converts all NEAR conjunctions to AND when either subexpression is not an InternalNode with the form TermForms.Literal.\n\n# Usage\nUse the `Transform()` method to convert a search expression to a valid SQL Server full-text search condition. This method takes a user-friendly search query and converts it to a correctly formed full-text search condition that can be passed to SQL Server's `CONTAINS` or `CONTAINSTABLE` functions. If the query contains invalid terms, the code will do what it can to return a valid search condition. If no valid terms were found, this method returns an empty string.\n\n```c#\n// Pass true to add the standard stop words\nFtsQuery ftsQuery = new FtsQuery(true);\nstring searchTerm = ftsQuery.Transform(text);\n```\n\nIn the following SQL query example, `@SearchTerm` is a reference to the string returned from `Transform()`.\n\n```sql\nSELECT select_list\nFROM table AS FT_TBL INNER JOIN\n   CONTAINSTABLE(table, column, @SearchTerm) AS KEY_TBL\n   ON FT_TBL.unique_key_column = KEY_TBL.[KEY];\n```\n\n# Stop Words (Noise Words)\nOne thing to be aware of is SQL Server's handling of stop words. Stop words are words such as *a*, *and*, and *the*. These words are not included in the full-text index. SQL Server does not index these words because they are very common and don't add to the quality of the search. Since these words are not indexed, SQL Server will never find a match for these words. The result is that a search for a stop word will return no results, even though that stop word may appear in an articles!\n\nThe best way to handle this seems to be to exclude these words from the SQL query. Easy Full Text Search allows you to do this by adding stop words to the `StopWords` collection property. Stop words will not be included in the resulting query unless they are quoted, thereby preventing stop words in the query from blocking all results.\n\nThe easiest way to add a standard list of stop words to the `StopWords` collection is to pass `true` to the `FtsQuery` constructor. (To see which words were added, you can simply inspect the `StopWords` collection.) You can modify the `StopWords` collection at any time, as needed.\n\nAlternatively, SQL Server provides an option for preventing the issue described above. The transform noise words option can be used to enable SQL Server to return matches even when the query contains a stop word (noise word). Set this option to 1 to enable noise word transformation. See [transform noise words Server Configuration Option](https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/transform-noise-words-server-configuration-option?view=sql-server-ver15) for more information.\n\nThe following query can be used to get the system stop words from a SQL Server database.\n\n```sql\nSELECT ssw.stopword, slg.name\nFROM sys.fulltext_system_stopwords ssw\nJOIN sys.fulltext_languages slg\nON slg.lcid = ssw.language_id\nWHERE slg.lcid = 1033\n```\n\n# More Information\nFor more information and a discussion of the code, please see my article [Easy Full-Text Search Queries](http://www.blackbeltcoder.com/Articles/data/easy-full-text-search-queries).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftcircuits%2Ffulltextsearchquery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftcircuits%2Ffulltextsearchquery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftcircuits%2Ffulltextsearchquery/lists"}