簡體   English   中英

Lucene.net字段包含多個值以及要搜索的人

[英]Lucene.net Field contains mutiple values and who to search

有人知道最好的方法是在具有多個值的字段上進行搜索嗎?

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

假設我要搜索所有帶有標簽“ csharp”的文檔,誰能最好地實現這一點?

我認為您正在尋找的是將多個具有相同名稱的字段添加到單個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM