Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Ricerca in più campi di due termini non vicini

    Ciao,

    come dal titolo ho un problema su come fare la ricerca su più campi di una frase, ma le parole non devono essere necessariamente vicine.

    Attualmente uso questa query per eseguire la ricerca di un unica parola, ma se diventano due mi cerca quel pezzo di frase non le due parole indistintamente.

    codice:
    strSQL = "SELECT * FROM genesi WHERE titolo REGEXP '(^|[^a-z])"&query&"($|[^a-z])' OR nomecomune REGEXP '(^|[^a-z])"&query&"($|[^a-z])' OR nomelatino REGEXP '(^|[^a-z])"&query&"($|[^a-z])' OR Testo REGEXP '(^|[^a-z])"&query&"($|[^a-z])' ORDER BY nomecomune ,nomelatino,titolo LIMIT "&inizio&","&numeroperpagina&" "
    Come posso fare a far cercare al motore di ricerca i record contenenti entrambe le parole in uno dei 4 i campi?

    Grazie Mauro

  2. #2
    Utente di HTML.it L'avatar di viki1967
    Registrato dal
    Feb 2003
    Messaggi
    1,757
    Devi utilizzare la sintassi LIKE, cerca sul forum se n'è già discusso.
    A S P : tutto il resto è noia...
    L I N U X : forse mi "converto"...

  3. #3
    allora, ti arriva la stringa dal form con le parole da cercare..esempio:

    codice:
    str_ricerca=trim(request.form("keys"))
    
    parole=split(str_ricerca," ")
    per ogni campo di ricerca devi usare in or tutte le parole:

    in pratica ti devi fare un ciclo for su parole in maniera da stamparti una query del tipo:

    codice:
    select * from tabella where (titolo=parola1 and titolo=parola2) or (campo2=parola1 and campo2=parola2) or ....
    probabilmente usando le regexp basterebbe:
    codice:
    REGEXP '(^|[^a-z])"& parola1 &"($|[^a-z])" & parola2 & "($|[^a-z])'"
    però così imponi che parola1 sia prima di parola2....boh prova un pò

  4. #4
    Utente di HTML.it L'avatar di viki1967
    Registrato dal
    Feb 2003
    Messaggi
    1,757
    E tu parli poi di soluzioni creative ?

    codice:
    <%
    	QueryString = Request.QueryString( "QueryString" )
    	QueryWords  = Split( QueryString )
    	strIndent   = ""
    	
    	' Reindirizzo in caso di ricerca fallita
    	If QueryString = "" Then
    		Response.Redirect( "default.asp" )
    	End If
    	
    	Session.timeout = 2
    	If IsObject(Session("sitesearch_conn")) Then
    	    Set conn = Session("sitesearch_conn")
    	Else
    	    Set conn = Server.CreateObject("ADODB.Connection")
    	    conn.open "sitesearch","",""
    	    Set Session("sitesearch_conn") = conn
    	End If
    
    	' Setup SQL
    	sql = "SELECT * FROM [CampoRicerca] WHERE"   
    
    
    	' Cerco nel campo descrizione
    	sql = sql & " ( [Description] LIKE '%" & QueryWords( 0 ) & "%'"			' Prima
    	For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
    		If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
    			If uCase( QueryWords( i-1 ) ) = "OR" Then
    				sql = sql & " OR [Description] LIKE '%" & QueryWords( i ) & "%'"
    			Else
    				sql = sql & " AND [Description] LIKE '%" & QueryWords( i ) & "%'"
    			End If
    		End If
    	Next
    
    	' Cerco nel campo keywords
    	sql = sql & " ) OR ( [Keywords] LIKE '%" & QueryWords( 0 ) & "%'"
    	For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
    		If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
    			If uCase( QueryWords( i-1 ) ) = "OR" Then
    				sql = sql & " OR [Keywords] LIKE '%" & QueryWords( i ) & "%'"
    			Else
    				sql = sql & " AND [Keywords] LIKE '%" & QueryWords( i ) & "%'"
    			End If
    		End If
    	Next
    
    
    	' Cerco nel campo titolo
    	sql = sql & " ) OR ( [Title] LIKE '%" & QueryWords( 0 ) & "%'"
    	For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
    		If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
    			If uCase( QueryWords( i-1 ) ) = "OR" Then
    				sql = sql & " OR [Title] LIKE '%" & QueryWords( i ) & "%'"
    			Else
    				sql = sql & " AND [Title] LIKE '%" & QueryWords( i ) & "%'"
    			End If
    		End If
    	Next
    
    
    	' Cerco nel campo summary
    	sql = sql & " ) OR ( [Summary] LIKE '%" & QueryWords( 0 ) & "%'"
    	For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
    		If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
    			If uCase( QueryWords( i-1 ) ) = "OR" Then
    				sql = sql & " OR [Summary] LIKE '%" & QueryWords( i ) & "%'"
    			Else
    				sql = sql & " AND [Summary] LIKE '%" & QueryWords( i ) & "%'"
    			End If
    		End If
    	Next
    
    	sql = sql & " )"
    
    
        ' Inizializzo il recordset ed eseguo la query
        Set rs = Server.CreateObject("ADODB.Recordset")
        rs.Open sql, conn, 3, 3	
        
        ' Stampo la stringa
        Response.Write "
     QueryString:  " & QueryString
        
        ' Stampo le parole
        Response.Write "
     Query Words:  "
    	For i = LBound( QueryWords ) to UBound( QueryWords )
    		Response.Write "
    " & strIndent & i & ": " & QueryWords( i )
    	Next
    
        ' Stampo la stringa sql
        Response.Write "
     SQL String :  " & sql
    	
    	' Stampo i risultati
        Response.Write "
     Results:  <UL>"
    	On Error Resume Next
    	rs.MoveFirst
    	Do While Not rs.eof
    		Response.Write "
    " & "" & rs.Fields("Title") & " - " 
    		Response.Write rs.Fields("Description") & "
    "
    		Response.Write "<FONT SIZE=2>URL: " & rs.Fields("URL") & "</FONT>"
    		Response.Write "<HR SIZE=1 WIDTH=200 ALIGN=LEFT>"
    		rs.MoveNext
    	Loop
    	Response.Write "[/list]"
        
    %>
    A S P : tutto il resto è noia...
    L I N U X : forse mi "converto"...

  5. #5
    questa non è una soluzione creativa, è solo una soluzioneONAONA perchè lunga da scrivere

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.