11

我无法使用 Lucene.NET 2.0.0.4 搜索确切的短语

例如,我正在搜索“范围属性设置变量”(包括引号)但没有收到匹配项,我已经确认 100% 的短语存在。

谁能建议我哪里出错了?Lucene.NET 甚至支持吗?像往常一样,API 文档并没有太大帮助,我读过的一些 CodeProject 文章也没有特别提到这一点。

使用以下代码创建索引:

Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory("Index", true);

Analyzer analyzer = new Lucene.Net.Analysis.SimpleAnalyzer();

IndexWriter indexWriter = new Lucene.Net.Index.IndexWriter(dir, analyzer,true);

//create a document, add in a single field
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();

Lucene.Net.Documents.Field fldContent = new Lucene.Net.Documents.Field(
    "content", File.ReadAllText(@"Documents\100.txt"),
    Lucene.Net.Documents.Field.Store.YES,
    Lucene.Net.Documents.Field.Index.TOKENIZED);

doc.Add(fldContent);

//write the document to the index
indexWriter.AddDocument(doc);

然后我使用以下方法搜索一个短语:

//state the file location of the index
Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory("Index", false);

//create an index searcher that will perform the search
IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir);

QueryParser qp = new QueryParser("content", new SimpleAnalyzer());

// txtSearch.Text  Contains a phrase such as "this is a phrase" 
Query q=qp.Parse(txtSearch.Text);  


//execute the query
Lucene.Net.Search.Hits hits = searcher.Search(q);

目标文档约为 7 MB 纯文本。

我已经看到了这个先前的问题,但是我不想要一个邻近搜索,只是一个精确的短语搜索。

4

2 回答 2

14

Shashikant Kore 的回答是正确的,您需要启用长期职位...

但是,我建议不要将文档的文本存储在字段中,除非您绝对需要它在搜索结果中返回给您……将存储设置为“否”可能有助于减少索引的大小。

Lucene.Net.Documents.Field fldContent = 
    new Lucene.Net.Documents.Field("content", 
        File.ReadAllText(@"Documents\100.txt"),
    Lucene.Net.Documents.Field.Store.NO,
    Lucene.Net.Documents.Field.Index.TOKENIZED, 
    Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS);
于 2009-06-24T20:21:41.143 回答
13

您尚未启用长期职位。如下创建字段应该可以解决您的问题。

Lucene.Net.Documents.Field fldContent = 
    new Lucene.Net.Documents.Field("content", 
        File.ReadAllText(@"Documents\100.txt"),
    Lucene.Net.Documents.Field.Store.YES,
    Lucene.Net.Documents.Field.Index.TOKENIZED, 
    Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS);
于 2009-05-12T05:18:43.740 回答