如何处理lucene 3.03上的命中

时间:2017-05-10 09:07:12

标签: c# lucene.net

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);
    }

我有表字段名字姓氏....如何处理命中以从搜索结果中获取值

  

我有一个错误,说TopDocs不包含doc

的定义

2 个答案:

答案 0 :(得分:1)

倾向于编译器。 Doc类中没有名为TopDocs的属性或方法。在ScoreDocs类的TopDocs属性中,您有包含文档编号和分数的匹配列表。您需要使用此文档编号来获取实际文档。之后使用Doc中的方法IndexSearcher来查询具有此编号的文档。然后,您可以从该文档中获取存储的现场数据。

您可以像这样处理结果:

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);
}

或更多LINQ方式:

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();

答案 1 :(得分:0)

hits方法的分离将让您清除匹配的documents,如果您想要highlight匹配的documents,您可以轻松嵌入{{1}在lucene.net highlighter方法中。

getMatchedHits

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

希望它有帮助!