2009-09-11 6 views
0

J'utilise le service d'index IIS pour disposer d'une fonction de recherche sur mon site Web. J'ai utilisé le code ci-dessous pour créer la requête pour obtenir des résultatsLimitation des résultats dans l'interrogation du catalogue d'index IIS

string query = String.Format(@"SELECT Rank, VPath, DocTitle, Filename, Characterization, Write FROM SCOPE('DEEP TRAVERSAL OF ""{0}""') WHERE NOT CONTAINS(VPath, '""_vti_"" OR "".pdf"" OR "".config"" OR "".js"" OR "".txt""')", "/"); 

     // Conditionally construct the rest of the WHERE clause. 
     string type = "any";//this.cboQueryType.SelectedItem.Value.ToLower(); 
     // string fmt = @" AND (filename NOT LIKE '%.doc') AND (filename NOT LIKE '%.txt') AND (filename NOT LIKE '%.js') AND (filename NOT LIKE '%.pdf') AND (filename NOT LIKE '%.ppt') AND (CONTAINS('{0}') OR CONTAINS(DocTitle, '{0}'))"; 
     string fmt = @" AND (filename NOT LIKE '%.doc') AND (filename NOT LIKE '%.aspx') AND (filename NOT LIKE '%.xml') AND (filename NOT LIKE '%.txt') AND (CONTAINS('{0}') OR CONTAINS(DocTitle, '{0}'))"; 

     // Get the query string and remove all semi-colons, which should stop 
     // attempt to run malicious SQL code. 

     if (type == "all" || type == "any" || type == "boolean") 
     { 
      string[] words = text.Split(' '); 
      int len = words.Length; 
      for (int i = 0; i < len; i++) 
      { 
       string word = words[i]; 
       if (type == "boolean") 
        if (String.Compare(word, "and", true) == 0 || 
         String.Compare(word, "or", true) == 0 || 
         String.Compare(word, "not", true) == 0 || 
         String.Compare(word, "near", true) == 0) 
         continue; 

       words[i] = String.Format(@"""{0}""", word); 
       if (i < len - 1) 
       { 
        if (type == "all") words[i] += " AND"; 
        else if (type == "any") words[i] += " OR"; 
       } 
      } 

      query += String.Format(fmt, String.Join(" ", words)); 
     } 
     else if (type == "exact") 
     { 
      query += String.Format(fmt, text); 
     } 
     else if (type == "natural") 
     { 
      query += String.Format(" AND FREETEXT('{0}')", text); 
     } 

     // Sort the results. 
     query += String.Format(" ORDER BY {0} {1}","Rank","DESC"); 


     //Trace.Write("Query", query); 
     return query; 

Cela fonctionne bien pour me.Now je veux réduire le nombre de résultats .Comment faire? Je cherche quelque chose comme SELECT top 15 du client. Je n'ai besoin que de 10 enregistrements pour mon résultat de recherche. Des pensées ????

Répondre

0

Vous pouvez utiliser la propriété MaxRecords.

Questions connexes