Lucene Searcher 仅返回一个匹配结果

发布于 2024-11-18 07:40:53 字数 694 浏览 0 评论 0原文

我的搜索文本为“ma”,我有两个 lucene 文档,其中的文本为 ma。但作为回报我只得到一份文件。

下面是代码:

//adding deocument 
document.Add(new Field("Text",text,Field.Store.YES, Field.Index.TOKENIZED));

//search logic :

IndexReader reader = IndexReader.Open(GetFileInfo(indexName));
//create an index searcher that will perform the search
IndexSearcher searcher = new IndexSearcher(reader);

//List of ID
List<string> searchResultID = new List<string>();

//build a query object
QueryParser parser = new QueryParser("Text", analyzer);
parser.SetAllowLeadingWildcard(true);
Query query = parser.Parse(searchText);
//execute the query
Hits hits = searcher.Search(query);

My search text goes as "ma" and i have two lucene document which have ma as the text in it. But in return i only get one document.

Below is the code :

//adding deocument 
document.Add(new Field("Text",text,Field.Store.YES, Field.Index.TOKENIZED));

//search logic :

IndexReader reader = IndexReader.Open(GetFileInfo(indexName));
//create an index searcher that will perform the search
IndexSearcher searcher = new IndexSearcher(reader);

//List of ID
List<string> searchResultID = new List<string>();

//build a query object
QueryParser parser = new QueryParser("Text", analyzer);
parser.SetAllowLeadingWildcard(true);
Query query = parser.Parse(searchText);
//execute the query
Hits hits = searcher.Search(query);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2024-11-25 07:40:53

也许您可以使用luke。它是一个有用的诊断工具,可以显示现有 Lucene 索引的内容并执行其他有趣的操作。我自己没有使用过它,所以我不确定,但我认为它可能会帮助你调试这个问题。祝你好运!

Maybe you could use luke. It's a useful diagnostic tool that can display the contents of an existing Lucene index and do other interesting stuff. I haven't used it myself, so I'm not sure, but I think it might help you in debugging this issue. Good luck!

梦初启 2024-11-25 07:40:53

我能够解决我的问题:

索引编写器只能创建一次。如果不创建新的索引编写器,您可以检查索引是否存在。例如:

//IndexWriter 构造函数的最后一个参数 bool 表示您是否要创建一个 newIndexWriter

IndexWriter writer = new IndexWriter(GetFileInfo(indexName), analyzer, true);

在添加新文档时,您必须检查索引是否存在,如果存在,则只需将 bool 参数传递为对 IndexWriter 构造函数设置 false:

IndexWriter  writer = new IndexWriter(GetFileInfo(indexName), analyzer, false);
 writer.AddDocument(CreateDocument(Id, text, dateTime));
 writer.Optimize();
 writer.Close();

I was able to solve my issue :

Index Writer must be created only once.You can check whether the index exits or not if not you create an new IndexWriter . for eg :

//The last parameter bool of an IndexWriter Contructor which says that you want to create an newIndexWriter or not

IndexWriter writer = new IndexWriter(GetFileInfo(indexName), analyzer, true);

On adding the new Document you must perform an check whether index exists or not , if it exists , then just pass bool param as false to the IndexWriter constructor:

IndexWriter  writer = new IndexWriter(GetFileInfo(indexName), analyzer, false);
 writer.AddDocument(CreateDocument(Id, text, dateTime));
 writer.Optimize();
 writer.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文