npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

full-text-search-query

v2.0.0

Published

full-text-search-query is a JavaScript library to help convert a user-friendly, Google-like search term into a valid Microsoft SQL Server full text search query.

Downloads

5

Readme

full-text-search-query

Based on FullTextSearchQuery by Jonathan Wood. Converted to JavaScript by Ronald M. Clifford.

npm install full-text-search-query

full-text-search-query is a JavaScript 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.

Introduction

Microsoft 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.

This library 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.

Examples

The following list shows how various input are transformed.

| Input | Output | Description | | ---- | ---- | ---- | | abc | FORMSOF(INFLECTIONAL, abc) | Find inflectional forms of abc. | ~abc | FORMSOF(THESAURUS, abc) | Find thesaurus variations of abc. | "abc" | "abc" | Find exact term abc. | +abc | "abc" | Find exact term abc. | "abc" near "def" | "abc" NEAR "def" | Find exact term abc near exact term def. | abc* | "abc*" | Finds words that start with abc. | -abc def | FORMSOF(INFLECTIONAL, def) AND NOT FORMSOF(INFLECTIONAL, abc) | Find inflectional forms of def but not inflectional forms of abc. | | abc def | FORMSOF(INFLECTIONAL, abc) AND FORMSOF(INFLECTIONAL, def) | Find inflectional forms of both abc and def. | abc or def | FORMSOF(INFLECTIONAL, abc) OR FORMSOF(INFLECTIONAL, def) | Find inflectional forms of either abc or def. | <+abc +def> | "abc" NEAR "def" | Find exact term abc near exact term def. | 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.

Preventing SQL Server Errors

Even 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, the library will swap the two subexpressions (on either side of AND).

After constructing a query, the library will check for this and several other error conditions and make corrections as necessary. The following table describes these conditions.

| Term | Action Taken | ---- | ---- | NOT term1 AND term2 | Subexpressions swapped. | NOT term1 | Expression discarded. | 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. | term1 OR NOT term2 | Expression discarded. | term1 NEAR NOT term2 | NEAR conjunction changed to AND.

The library converts all NEAR conjunctions to AND when either subexpression is not an InternalNode with the form TermForms.Literal.

Usage

Use 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.

const FtsQuery = require("full-text-search-query");
const ftsQuery = new FtsQuery(true);
const searchTerm = ftsQuery.transform(friendlyQuery);

In the following SQL query example, @SearchTerm is a reference to the string returned from transform().

SELECT select_list
FROM table AS FT_TBL INNER JOIN
   CONTAINSTABLE(table, column, @SearchTerm) AS KEY_TBL
   ON FT_TBL.unique_key_column = KEY_TBL.[KEY];

Stop Words (Noise Words)

One 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 really 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 your articles.

The best way to handle this seems to be to exclude these words from the SQL query. This library 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.

Alternatively, 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.

The following query can be used to get the system stop words from a SQL Server database.

SELECT ssw.stopword, slg.name
FROM sys.fulltext_system_stopwords ssw
JOIN sys.fulltext_languages slg
ON slg.lcid = ssw.language_id
WHERE slg.lcid = 1033

More Information

For more information and a discussion of the original C# library's code, please see Jonathan Wood's article Easy Full-Text Search Queries.