Lucene.net字段包含多发的价值观和谁搜索 [英] Lucene.net Field contains mutiple values and who to search

查看:169
本文介绍了Lucene.net字段包含多发的价值观和谁搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道最好的办法就是对持有多个值的字段搜索什么

Anyone know what the best way is to search on a Field that hold multiple values?

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

比方说,我想搜索具有标签CSHARP的所有文档,谁我能最好地实现这一点?

Let's say I want to search for all documents that has the tag "csharp", who could I best implement this?

推荐答案

我觉得你在找什么是添加多个字段具有相同名称为单个文件

I think what you are looking for is adding multiple fields with the same name to a single Document.

你要做的就是创建一个文件并添加多个标记字段 。它

What you do is create a single Document and add multiple tags Field to it.

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

这篇关于Lucene.net字段包含多发的价值观和谁搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆