나는 하나의 Document
에 같은 이름의 여러 필드를 추가하는 것으로 생각합니다.
하나의 Document
을 만들고 여러 개의 태그 Field
을 추가하십시오.
RAMDirectory ramDir = new RAMDirectory();
IndexWriter writer = new IndexWriter(ramDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
Document doc = new Document();
Field tags = null;
string [] articleTags = new string[] {"C#", "WPF", "Lucene" };
foreach (string tag in articleTags)
{
// adds a field with same name multiple times to the same document
tags = new Field("tags", tag, Field.Store.YES, Field.Index.NOT_ANALYZED);
doc.Add(tags);
}
writer.AddDocument(doc);
writer.Commit();
// search
IndexReader reader = writer.GetReader();
IndexSearcher searcher = new IndexSearcher(reader);
// use an analyzer that treats the tags field as a Keyword (Not Analyzed)
PerFieldAnalyzerWrapper aw = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
aw.AddAnalyzer("tags", new KeywordAnalyzer());
QueryParser qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "tags", aw);
Query q = qp.Parse("+WPF +Lucene");
TopDocs docs = searcher.Search(q, null, 100);
Console.WriteLine(docs.totalHits); // 1 hit
q = qp.Parse("+WCF +Lucene");
docs = searcher.Search(q, null, 100);
Console.WriteLine(docs.totalHits); // 0 hit
답장을 보내고 싶습니다. 원하는 것을 수행하는 것 같습니다. 그러나 추가 태그로 기존 문서를 업데이트하는 방법은 무엇입니까? Cheerz – Martijn
lucene에서는 문서를 "업데이트"하지 않고 삭제 한 다음 색인에 다시 추가합니다. IndexWriter에는 UpdateDocument() 메서드가 있습니다.이 메서드는 단순히 문서를 삭제 한 다음 다시 인덱스에 추가하는 편리한 메서드입니다. –