2017-05-10 2 views
1
List<SearchResults> Searchresults = new List<SearchResults>(); 

    // Specify the location where the index files are stored 
    string indexFileLocation = @"D:\Lucene.Net\Data\Persons"; 

    Lucene.Net.Store.Directory dir = FSDirectory.Open(indexFileLocation); 
    // specify the search fields, lucene search in multiple fields 
    string[] searchfields = new string[] { "FirstName", "LastName", "DesigName", "CatagoryName" }; 
    IndexSearcher indexSearcher = new IndexSearcher(dir); 
    // Making a boolean query for searching and get the searched hits 
    Query som = QueryMaker(searchString, searchfields); 
    int n = 1000; 
    TopDocs hits = indexSearcher.Search(som,null,n); 



    for (int i = 0; i <hits.TotalHits; i++) 
    { 
     SearchResults result = new SearchResults(); 
     result.FirstName = hits.ScoreDocs.GetValue(i).ToString(); 

     result.FirstName = hits.Doc.GetField("FirstName").StringValue(); 
     result.LastName = hits.Doc(i).GetField("LastName").StringValue(); 
     result.DesigName = hits.Doc(i).GetField("DesigName").StringValue(); 
     result.Addres = hits.Doc(i).GetField("Addres").StringValue(); 
     result.CatagoryName = hits.Doc(i).GetField("CatagoryName").StringValue(); 
     Searchresults.Add(result); 
    } 

j'ai champs de table premier nom nom .... Comment puis-je traiter frapper pour obtenir les valeurs du résultat de la recherchecomment traiter hits sur Lucene 3,03

j'ai une erreur qui dit TopDocs ne contient pas de définition pour le document

Répondre

1

S'appuyer sur le compilateur. Il n'existe aucune propriété ou méthode appelée Doc dans la classe TopDocs. En ScoreDocs propriété de TopDocs classe vous avez la liste des résultats avec le numéro de document et le score. Vous devez utiliser ce numéro de document pour obtenir le document réel. Après cela, utilisez la méthode Doc qui est en IndexSearcher pour rechercher un document avec ce numéro. Et puis vous pouvez obtenir des données de champ stockées à partir de ce document.

Vous pouvez traiter de tels résultats:

foreach (var scoreDoc in hits.ScoreDocs) 
{ 
    var result = new SearchResults(); 
    var doc = indexSearcher.Doc(scoreDoc.Doc); 
    result.FirstName = doc.GetField("FirstName").StringValue; 
    result.LastName = doc.GetField("LastName").StringValue; 
    result.DesigName = doc.GetField("DesigName").StringValue; 
    result.Addres = doc.GetField("Addres").StringValue; 
    result.CategoryName = doc.GetField("CategoryName").StringValue; 
    Searchresults.Add(result); 
} 

Ou dans plus LINQ façon:

var searchResults = 
    indexSearcher 
    .Search(som, null, n) 
    .ScoreDocs 
    .Select(scoreDoc => indexSearcher.Doc(scoreDoc)) 
    .Select(doc => 
    { 
     var result = new SearchResults(); 
     result.FirstName = doc.GetField("FirstName").StringValue; 
     result.LastName = doc.GetField("LastName").StringValue; 
     result.DesigName = doc.GetField("DesigName").StringValue; 
     result.Addres = doc.GetField("Addres").StringValue; 
     result.CategoryName = doc.GetField("CategoryName").StringValue; 
     return result; 
    }) 
    .ToList(); 
0

Séparation de la méthode hits vous permettra d'effacer la correspondance documents et à l'avenir si vous voulez highlight l'apparié documents puis vous pouvez facilement intégrer la méthode lucene.net highlighter dans getMatchedHits.

List<SearchResults> Searchresults = new List<SearchResults>(); 

// Specify the location where the index files are stored 
string indexFileLocation = @"D:\Lucene.Net\Data\Persons"; 

Lucene.Net.Store.Directory dir = FSDirectory.Open(indexFileLocation); 
     // specify the search fields, lucene search in multiple fields 
string[] searchfields = new string[] { "FirstName", "LastName", "DesigName", "CatagoryName" }; 
IndexSearcher indexSearcher = new IndexSearcher(dir); 
// Making a boolean query for searching and get the searched hits 
Query som = QueryMaker(searchString, searchfields); 
int n = 1000; 
var hits = indexSearcher.Search(som,null,n).ScoreDocs; 

Searchresults = getMatchedHits(hits,indexSearcher); 

getMatchedHits code de la méthode:

public static List<SearchResults> getMatchedHits(ScoreDoc[] hits, IndexSearcher searcher) 
{ 
    List<SearchResults> list = new List<SearchResults>(); 
    SearchResults obj; 
    try 
    { 
     for (int i = 0; i < hits.Count(); i++) 
     { 
      // get the document from index 
      Document doc = searcher.Doc(hits[i].Doc); 
      string strFirstName = doc.Get("FirstName"); 
      string strLastName = doc.Get("LastName"); 
      string strDesigName = doc.Get("DesigName"); 
      string strAddres = doc.Get("Addres"); 
      string strCategoryName = doc.Get("CategoryName"); 
      obj = new SearchResults(); 
      obj.FirstName = strFirstName; 
      obj.LastName = strLastName; 
      obj.DesigName= strDesigName; 
      obj.Addres = strAddres; 
      obj.CategoryName = strCategoryName; 
      list.Add(obj); 
     } 
     return list; 
    } 
    catch (Exception ex) 
    { 
     return null; // or throw exception 
    } 
} 

Hope it helps!