如何使用和和和来编写包含稳定的搜索条件

时间:2011-12-07 12:27:04

标签: sql full-text-search containstable

目前正在尝试使用containstable编写一个select语句,并且搜索条件让我感到悲伤。

目的是搜索具有3种不同参数类型的表,并相应地对输出进行排名。 (以下是我的代码示例)

  1. 主要搜索条件(必须加权,加权为1)|| testvalue = Accountant
  2. 附加必须(加权.8)|| testvalue = Manager
  3. 很高兴(加权.5)|| testvalue = Excel
  4. SQL:

     SELECT KTBL.Rank as [Ranking], KeySkills
       FROM Applicants INNER JOIN 
          CONTAINSTABLE(Applicants, KeySkills, 
            '(ISABOUT("Accountant" weight(1))) & 
             (ISABOUT("Manager" weight(.8))) | 
             (ISABOUT("Excel") weight(.5)))
         AS KTBL
       ON Applicants.Id = KTBL.[KEY];
    

    上述陈述没有返回任何内容,我怀疑是否有可能以上述方式堆叠条款。

    返回的结果必须同时具有会计和管理员,并且很高兴有Excel

    我有什么建议可以实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

我最终想出了一个我自己的问题的解决方案,我现在很满意。

我最终得到了如下所示的存储过程(有点长的啰嗦),它有3个参数 1.主要搜索键(必须匹配) 2.附加必须有逗号分隔的字符串(必须匹配) 3.另外很好用逗号分隔字符串

CREATE PROCEDURE [dbo].[GetJobSeekers]
(
@KeywordSearch nvarchar(500),
@MustHave nvarchar(500), --Comma separated skills
@NiceToHave nvarchar(500) --Comma separated skills
)
AS
BEGIN

SET NOCOUNT ON;

DECLARE @SQL nvarchar(4000)
DECLARE @SELECT nvarchar(4000)
DECLARE @SEARCH nvarchar(4000)
DECLARE @SEARCH1 nvarchar(4000)
DECLARE @SEARCH2 nvarchar(4000)
DECLARE @WHERE nvarchar(4000)
DECLARE @ORDERBY nvarchar(4000)

/*Used for string split*/
DECLARE @POS int
DECLARE @NEXTPOS int

/*Get Result for primary search key*/
SET @SELECT =   'SELECT ktbl.rank AS [Ranking], jobseekers.*'
SET @SEARCH =   'ISABOUT("'+@KeywordSearch+'" weight(1))'

SET @WHERE  =   ' FROM jobseekers INNER JOIN
                CONTAINSTABLE (jobseekers, *, ''' + @SEARCH + ''')
                AS ktbl On jobseekers.Id = ktbl.[KEY]'

SET @ORDERBY=   'ORDER BY [Ranking] DESC'

/* Get Result set for all additional must have keywords and INNER JOIN With primary Search */
IF @MustHave <> ''

BEGIN
    DECLARE @MustHaveSplitString nvarchar(500)

    SET @POS = 1
    WHILE(@POS <= LEN(@MustHave))
    BEGIN
        SELECT @NEXTPOS = CHARINDEX(N',', @MustHave,  @POS)
        IF (@NEXTPOS = 0 OR @NEXTPOS IS NULL)
              SELECT @NEXTPOS = LEN(@MustHave) + 1
        SELECT @MustHaveSplitString = RTRIM(LTRIM(SUBSTRING(@MustHave, @POS, @NEXTPOS - @POS)))

        SET @SELECT = @SELECT + ', ktbl'+@MustHaveSplitString+'.rank AS [Ranking'+@MustHaveSplitString+']'
        SET @ORDERBY = @ORDERBY + ', [Ranking'+@MustHaveSplitString+'] DESC'
        SET @SEARCH1 = 'ISABOUT("'+@MustHaveSplitString+'" weight(.8))'
        SET @WHERE = @WHERE + ' INNER JOIN CONTAINSTABLE (jobseekers, *, ''' + @SEARCH1 + ''')
                                AS ktbl'+@MustHaveSplitString+' on Jobseekers.Id = ktbl'+@MustHaveSplitString+'.[KEY]'
        SELECT @POS = @NEXTPOS+1    
    END 
END


/*Get result set for all nice to have by stacking them in the isabout searchcondition and LEFT OUTER JOIN with Primary Search + Must have search if its there*/
IF @NiceToHave <> ''
BEGIN
    DECLARE @NiceToHaveSplitString nvarchar(500)        
    SET @SEARCH2 = 'ISABOUT('

    SET @POS = 1
    WHILE(@POS <= LEN(@NiceToHave))
    BEGIN
        SELECT @NEXTPOS = CHARINDEX(N',', @NiceToHave,  @POS)
        IF (@NEXTPOS = 0 OR @NEXTPOS IS NULL)
              SELECT @NEXTPOS = LEN(@NiceToHave) + 1
        SELECT @NiceToHaveSplitString = RTRIM(LTRIM(SUBSTRING(@NiceToHave, @POS, @NEXTPOS - @POS)))

        SET @SEARCH2 = @SEARCH2 + '"'+@NiceToHaveSplitString+'" weight(.5),'            

        SELECT @POS = @NEXTPOS+1    
    END         
    /*Clean last , off the search2 string */
    SET @SEARCH2 = LEFT(@SEARCH2, LEN(@SEARCH2) -1)
    /*Close the isabout in search2 string*/
    SET @SEARCH2 = @SEARCH2 + ')'


    SET @SELECT = @SELECT + ', ktbl2.rank AS [Ranking2]'
    SET @ORDERBY = @ORDERBY + ', [Ranking2] DESC'

    SET @WHERE = @WHERE + ' LEFT JOIN CONTAINSTABLE (jobseekers, *, ''' + @SEARCH2 + ''')
                            AS ktbl2 on Jobseekers.Id =          ktbl2.[KEY]'
END

SET @SQL    =   @SELECT + @WHERE + @ORDERBY

EXEC sp_executesql @SQL
END

存储过程仍然不是100%完成,因为它需要考虑额外的逻辑,但它暂时将作为最终结果的shell。此外,我还没有针对任何大量数据对此进行测试,因此我仍然不确定它的表现如何。

亲切的问候,