2012-06-20 2 views
0

값이 여러 개인 필드를 검색하는 가장 좋은 방법은 무엇입니까?Lucene.net 필드에 여러 값과 검색 대상이 포함되어 있습니다.

string tagString = ""; 
foreach(var tag in tags) 
{ 
    tagString = tagString += ":" + tag; 
} 
doc.Field(new Field("Tags", tagString, Field.Store.YES, Field.Index.Analyzed); 

"csharp"라는 태그가있는 모든 문서를 검색하고 싶습니다. 누가 가장 잘 구현할 수 있습니까?

답변

4

나는 하나의 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 
+0

답장을 보내고 싶습니다. 원하는 것을 수행하는 것 같습니다. 그러나 추가 태그로 기존 문서를 업데이트하는 방법은 무엇입니까? Cheerz – Martijn

+0

lucene에서는 문서를 "업데이트"하지 않고 삭제 한 다음 색인에 다시 추가합니다. IndexWriter에는 UpdateDocument() 메서드가 있습니다.이 메서드는 단순히 문서를 삭제 한 다음 다시 인덱스에 추가하는 편리한 메서드입니다. –

관련 문제