Question

Essayant actuellement d'écrire une instruction sélectionnée avec un containstable Et la condition de recherche me donne du chagrin.

Le but est de rechercher un tableau avec 3 types de paramètres différents et de classer la sortie en conséquence. (Ci-dessous est un exemple de mon code)

  1. Condition de recherche clé (incontournable et pondéré à 1) || testvalue = Accountant
  2. Incontournable supplémentaire (pondéré .8) || testvalue = Manager
  3. Agréable à avoir (pondéré .5) || testvalue = Excel

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];

L'énoncé ci-dessus ne renvoie rien et je doute qu'il soit possible d'empiler les termes Isabout de la manière ci-dessus.

Les résultats renvoyés doivent avoir à la fois un comptable et un gestionnaire et ce serait bien d'avoir Excel

Des suggestions à avoir pour pouvoir y parvenir?

Était-ce utile?

La solution

J'ai finalement trouvé une solution à ma propre question dont je suis satisfait pour le moment.

Je me suis retrouvé avec la procédure stockée comme indiqué ci-dessous ici (un peu long) qui prend 3 paramètres 1. Clé de recherche principale (doit correspondre) 2. Doit avoir une chaîne séparée par des virgules (doit correspondre) 3. Nice supplémentaire pour que les virgules soient séparées chaîne de caractères

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

La procédure stockée n'est toujours pas complète à 100% car elle doit prendre en considération la logique supplémentaire, mais elle agira pour le moment comme un shell pour le résultat final. Je n'ai pas encore testé cela contre une quantité substantielle de données, donc je ne sais toujours pas à quel point il fonctionnera.

Sincères amitiés,

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top