简体   繁体   English

在C#中使用Lucene查询

[英]Querying with Lucene in C#

I'm new to Lucene. 我是Lucene的新手。 I have some business objects that are stored in a database. 我有一些存储在数据库中的业务对象。 When I retrieve them, I index them with Lucene.NET as shown here: 检索它们时,我使用Lucene.NET为它们建立索引,如下所示:

List<MyEntity> myResults = GetResultsFromDb();
using (var indexService = new IndexService(indexWriter))
{
  indexService.IndexEntities(myResults, p =>
  {
    var document = new Document();
    document.Add(new Field("ID", p.ID.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));
    document.Add(new Field("Name", p.Name, Field.Store.NO, Field.Index.NOT_ANALYZED));
    document.Add(new Field("Description", p.Description, Field.Store.YES, Field.Index.ANALYZED));
    document.Add(new Field("IsActive", p.IsActive.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));
    return document;
  });
}

I'm now trying to search for business objects with a similar Name. 我现在正在尝试搜索名称相似的业务对象。 In an attempt to perform that search, I'm using the following: 为了执行该搜索,我使用了以下内容:

  var indexSearcher = new DirectoryIndexSearcher(new   DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index")), true);
  using (var searchService = new SearchService(indexSearcher))
  {
    TermQuery termQuery = new TermQuery(new Term("Name", GetNameToSearchWith()));                    

    SearchResult<MyEntity> searchResults = searchService.SearchIndex<CourseSearchResult>(termQuery, new MyEntityDefinition());
    foreach (CourseSearchResult searchResult in searchResults.Results)
    {
      filteredResults.Results.Add(searchResult);
      if (filteredResults.Count() >= 25)
        break;
    }
  }

When I execute my query, I retrieve results. 执行查询时,我会检索结果。 The problem is, the only value that is populated is the "Name" field. 问题是,唯一填充的值是“名称”字段。 In the case of MyEntity, ID is a Guid, Description is a String, and IsActive is a nullable bool. 对于MyEntity,ID是Guid,Description是String,IsActive是可为null的布尔值。 When I execute my query, the ID is always an empty Guid, the IsActive flag is always null and Description is always an empty string. 当我执行查询时,ID始终为空的Guid,IsActive标志始终为空,Description始终为空字符串。

What am I doing wrong? 我究竟做错了什么?

Actually you tell Lucene in the document not to store those data in the document. 实际上,您告诉文档中的Lucene不要将那些数据存储在文档中。 you should change Field.Store.NO to Field.Store.YES for all the fields you want to retrieve from documents during a search. 您应该将要在搜索过程中从文档中检索的所有字段的Field.Store.NO更改为Field.Store.YES。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM