我正在尝试在我的 .NET 应用程序中使用 Apache Solr 作为全文搜索引擎(通过SolrNet)。我的应用程序具有这种数据模式:
class Document
{
public int Id { get; set; };
public string Name { get; set; }
public DateTime CreateDate { get; set;}
public Attach[] Attaches { get; set; }
}
class Attach
{
public int Id { get; set; }
public Document Parent { get; set; }
//files are stored in filesystem, only path stored in database!
public string FilePath { get; set; }
}
现在,我正在尝试索引这些文件(使用 Castle.Windsor):
_container.AddFacility("solr",
new SolrNetFacility("http://localhost:8983/solr"));
var solr = _container.Resolve<ISolrOperations<Document>>();
solr.Delete(SolrQuery.All);
var conn = _container.Resolve<ISolrConnection>();
var docs = from o in Documents
where o.Attaches.Count > 0
select o;
foreach (var doc in docs)
{
foreach (var att in doc.Attaches)
{
try
{
var file = Directory.GetFiles("C:\\Attachments\\" + doc.Id );
foreach (var s in file)
{
var a = File.ReadAllText(s);
conn.Post("/update", a);
}
}
catch (Exception)
{
throw;
}
}
}
solr.Commit();
solr.BuildSpellCheckDictionary();
如代码中所述,我正在搜索文件路径,并直接从磁盘添加文件内容。但是,当我将文件的文本发布到 Solr 时,我收到了错误:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">400</int><int name="QTime">2</int>
</lst>
<lst name="error">
<str name="msg">Unexpected character 'Т' (code 1058 / 0x422) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]</str>
<int name="code">400</int>
</lst>
</response>
我有这个问题:
- 我可以发布以索引纯文本而不是 XML 吗?
- 我必须序列化我的数据对象以索引它们吗?如果是,我必须如何在“附加”类中表示文件?