2016-04-08 31 views
5

Sono abituato a utilizzare Lucene per la ricerca di testo completo e funzionava perfettamente, ma il mio database principale era SQL. Non mi piaceva l'idea di avere due meccanismi per il database e ho deciso di utilizzare la ricerca full text SQL. Le cose funzionano alla grande, ma ci sono ancora alcune cose che non ho capito. Diciamo che ho la seguente tabella:SQL full text Priorità dei risultati di ricerca

enter image description here

Tutti i campi sono indicizzati per la ricerca full text.

Ora voglio eseguire la ricerca full text su questa tabella con il testo "Isaac". E vorrei che la riga 5 fosse in cima e il resto dei risultati fosse al di sotto di quello. Posso impostare una priorità su un campo in modo che tutto ciò che si trova su quel campo rimanga in cima al risultato? Fondamentalmente mi piacerebbe dare la priorità al campo. Nel mio tavolo reale ho 6 campi.

+0

che tipo di DB stai usando? –

+0

@ Mr.P Sto usando MS SQL – Dilshod

risposta

1

Per quanto ne so MSSQL non supporta il confronto tra stringhe somiglianza .. si deve scrivere il proprio set di funzioni

-- get percentage diff 
CREATE FUNCTION [dbo].[GetPercentageOfTwoStringMatching] 
(
    @string1 NVARCHAR(100) 
    ,@string2 NVARCHAR(100) 
) 
RETURNS INT 
AS 
BEGIN 

    DECLARE @levenShteinNumber INT 

    DECLARE @string1Length INT = LEN(@string1) 
    , @string2Length INT = LEN(@string2) 
    DECLARE @maxLengthNumber INT = CASE WHEN @string1Length > @string2Length THEN @string1Length ELSE @string2Length END 

    SELECT @levenShteinNumber = [dbo].[LEVENSHTEIN] ( @string1 ,@string2) 

    DECLARE @percentageOfBadCharacters INT = @levenShteinNumber * 100/@maxLengthNumber 

    DECLARE @percentageOfGoodCharacters INT = 100 - @percentageOfBadCharacters 

    -- Return the result of the function 
    RETURN @percentageOfGoodCharacters 

END 

-- get diff of strings 
CREATE FUNCTION [dbo].[LEVENSHTEIN](@left VARCHAR(100), 
            @right VARCHAR(100)) 
returns INT 
AS 
    BEGIN 
     DECLARE @difference INT, 
       @lenRight  INT, 
       @lenLeft  INT, 
       @leftIndex  INT, 
       @rightIndex INT, 
       @left_char  CHAR(1), 
       @right_char CHAR(1), 
       @compareLength INT 

     SET @lenLeft = LEN(@left) 
     SET @lenRight = LEN(@right) 
     SET @difference = 0 

     IF @lenLeft = 0 
     BEGIN 
      SET @difference = @lenRight 

      GOTO done 
     END 

     IF @lenRight = 0 
     BEGIN 
      SET @difference = @lenLeft 

      GOTO done 
     END 

     GOTO comparison 

     COMPARISON: 

     IF (@lenLeft >= @lenRight) 
     SET @compareLength = @lenLeft 
     ELSE 
     SET @compareLength = @lenRight 

     SET @rightIndex = 1 
     SET @leftIndex = 1 

     WHILE @leftIndex <= @compareLength 
     BEGIN 
      SET @left_char = substring(@left, @leftIndex, 1) 
      SET @right_char = substring(@right, @rightIndex, 1) 

      IF @left_char <> @right_char 
       BEGIN -- Would an insertion make them re-align? 
        IF(@left_char = substring(@right, @rightIndex + 1, 1)) 
        SET @rightIndex = @rightIndex + 1 
        -- Would an deletion make them re-align? 
        ELSE IF(substring(@left, @leftIndex + 1, 1) = @right_char) 
        SET @leftIndex = @leftIndex + 1 

        SET @difference = @difference + 1 
       END 

      SET @leftIndex = @leftIndex + 1 
      SET @rightIndex = @rightIndex + 1 
     END 

     GOTO done 

     DONE: 

     RETURN @difference 
    END 

e poi si aggiungere questo al tuo ordine da

SELECT * 
FROM [dbo].[some_table] 
ORDER BY [dbo].[GetPercentageOfTwoStringMatching](col1 ,col2) DESC 

adattarlo alle proprie DTB ma questo dovrebbe funzionare per voi


oppure è possibile impostare semplice CA SE WHEN condizione per creare la colonna dell'ordine