【问题标题】:Storing List of Strings in Lucene.NET在 Lucene.NET 中存储字符串列表
【发布时间】:2016-06-03 19:20:12
【问题描述】:

我正在开发一个依赖于 Lucene.NET 的项目。到目前为止,我已经有了一个具有简单名称/值属性的类(如 int ID { get; set; })。但是,我现在需要在我的索引中添加一个新属性。该属性是一种列表。到目前为止,我已经像这样更新了我的索引......

MyResult result = GetResult();
using (IndexWriter indexWriter = Initialize())
{
  var document = new Document();
  document.Add(new Field("ID", result.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZE));
  indexWriter.AddDocument(document); 
}

现在,MyResult 有一个表示列表的属性。我如何把它放在我的索引中?我需要将它添加到我的索引中的原因是我可以稍后将其取回。

【问题讨论】:

  • 您是否考虑过使用存储无模式、非结构化文档而不仅仅是键值对的东西?这将解决您的问题(一些示例,RavenDB、elasticsearch、MongoDB)。否则,您必须为包含数组信息和嵌套属性信息的键生成一个表示法(很简单,但是一个 PITA 并且如上所述,已经有一些东西可以做到这一点)。
  • 您的清单包含什么?它需要可搜索吗?
  • 列表不需要是可搜索的。

标签: c# lucene.net


【解决方案1】:

您可以将列表中的每个值添加为具有相同名称的新字段(lucene 支持),然后将这些值读回字符串列表:

MyResult result = GetResult();
using (IndexWriter indexWriter = Initialize())
{
    var document = new Document();
    document.Add(new Field("ID", result.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZE));

    foreach (string item in result.MyList)
    {
         document.Add(new Field("mylist", item, Field.Store.YES, Field.Index.NO));
    }

    indexWriter.AddDocument(document);
}

以下是从搜索结果中提取值的方法:

MyResult result = GetResult();
result.MyList = new List<string>();

foreach (IFieldable field in doc.GetFields())
{
    if (field.Name == "ID")
    {
        result.ID = int.Parse(field.StringValue);
    }
    else if (field.Name == "myList")
    {
        result.MyList.Add(field.StringValue);
    }
}

【讨论】:

  • +1,最好的方法。但是该字段应该使用 Field.Index.NO 创建,因为询问者指定它不需要可搜索。
猜你喜欢
  • 1970-01-01
  • 2016-10-18
  • 2014-09-11
  • 1970-01-01
  • 2017-12-29
  • 2022-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多