How hard is it to incorporate full text search with SQL Server?

前端 未结 5 899
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 09:19

I am building a C#/ASP.NET app with an SQL backend. I am on deadline and finishing up my pages, out of left field one of my designers incorporated a full text search on one

5条回答
  •  天涯浪人
    2020-12-29 10:12

    First off, you need to enabled Full text Searching indexing on the production servers, so if thats not in scope, your not going to want to go with this.

    However, if that's already ready to go, full text searching is relatively simple.

    T-SQL has 4 predicates used for full text search:

    • FREETEXT
    • FREETEXTTABLE
    • CONTAINS
    • CONTAINSTABLE

    FREETEXT is the simplest, and can be done like this:

    SELECT UserName
    FROM Tbl_Users
    WHERE FREETEXT (UserName, 'bob' )
    
    Results:
    
    JimBob
    Little Bobby Tables
    

    FREETEXTTABLE works the same as FreeTEXT, except it returns the results as a table.

    The real power of T-SQL's full text search comes from the CONTAINS (and CONTAINSTABLE) predicate...This one is huge, so I'll just paste its usage in:

    CONTAINS
        ( { column | * } , '< contains_search_condition >' 
        ) 
    
    < contains_search_condition > ::= 
            { < simple_term > 
            | < prefix_term > 
            | < generation_term > 
            | < proximity_term > 
            | < weighted_term > 
            } 
            | { ( < contains_search_condition > ) 
            { AND | AND NOT | OR } < contains_search_condition > [ ...n ] 
            } 
    
    < simple_term > ::= 
        word | " phrase "
    
    < prefix term > ::= 
        { "word * " | "phrase * " }
    
    < generation_term > ::= 
        FORMSOF ( INFLECTIONAL , < simple_term > [ ,...n ] ) 
    
    < proximity_term > ::= 
        { < simple_term > | < prefix_term > } 
        { { NEAR | ~ } { < simple_term > | < prefix_term > } } [ ...n ] 
    
    < weighted_term > ::= 
        ISABOUT 
            ( { { 
                    < simple_term > 
                    | < prefix_term > 
                    | < generation_term > 
                    | < proximity_term > 
                    } 
                [ WEIGHT ( weight_value ) ] 
                } [ ,...n ] 
            ) 
    

    This means you can write queries such as:

    SELECT UserName
    FROM Tbl_Users
    WHERE CONTAINS(UserName, '"little*" NEAR tables')
    
    Results:
    
    Little Bobby Tables
    

    Good luck :)

提交回复
热议问题