gpt4 book ai didi

c# - 使用 lucene.net 搜索 "mvc2"时没有命中

转载 作者:行者123 更新时间:2023-11-30 16:27:12 25 4
gpt4 key购买 nike

我正在使用 lucene.net 进行索引和搜索,我的代码遇到的唯一问题是在搜索“mvc2”时它没有找到任何匹配项(它似乎适用于我搜索的所有其他词),我尝试了不同的分析器(请参阅分析器的评论)和较旧的 lucene 代码,这是我的索引和搜索代码,如果有人能告诉我我哪里出错了,我将不胜感激,谢谢。

////Indexing code
public void DoIndexing(string CvContent)
{
//state the file location of the index
const string indexFileLocation = @"C:\RecruitmentIndexer\IndexedCVs";

//if directory does not exist, create it, and create new index for it.
//if directory does exist, do not create directory, do not create new //index(add field to previous index).
bool creatNewDirectory; //to pass into lucene GetDirectory
bool createNewIndex; //to pass into lucene indexWriter
if (!Directory.Exists(indexFileLocation))
{
creatNewDirectory = true;
createNewIndex = true;
}
else
{
creatNewDirectory = false;
createNewIndex = false;
}


Lucene.Net.Store.Directory dir =
Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, creatNewDirectory);//creates if true

//create an analyzer to process the text
Lucene.Net.Analysis.Analyzer analyzer = new
Lucene.Net.Analysis.SimpleAnalyzer(); //this analyzer gets all //hits exept mvc2
//Lucene.Net.Analysis.Standard.StandardAnalyzer(); //this leaves out sql once //and mvc2 once

//create the index writer with the directory and analyzer defined.
Lucene.Net.Index.IndexWriter indexWriter = new
Lucene.Net.Index.IndexWriter(dir, analyzer,
/*true to create a new index*/ createNewIndex);

//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",
CvContent,//"This is some text to search by indexing",
Lucene.Net.Documents.Field.Store.YES,
Lucene.Net.Documents.Field.Index.ANALYZED,
Lucene.Net.Documents.Field.TermVector.YES);

doc.Add(fldContent);

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

//optimize and close the writer
indexWriter.Optimize();
indexWriter.Close();

}
////search code
private void button2_Click(object sender, EventArgs e)
{

string SearchString = textBox1.Text;

///after creating an index, search
//state the file location of the index
const string indexFileLocation = @"C:\RecruitmentIndexer\IndexedCVs";

Lucene.Net.Store.Directory dir =
Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, false);

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

SearchString = SearchString.Trim();
SearchString = QueryParser.Escape(SearchString);


//build a query object
Lucene.Net.Index.Term searchTerm =
new Lucene.Net.Index.Term("content", SearchString);
Lucene.Net.Search.Query query = new Lucene.Net.Search.TermQuery(searchTerm);

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

label1.Text = hits.Length().ToString();

//iterate over the results.
for (int i = 0; i < hits.Length(); i++)
{
Lucene.Net.Documents.Document docMatch = hits.Doc(i);

MessageBox.Show(docMatch.Get("content"));

}

}

最佳答案

我相信 StandardAnalyzer 实际上从“mvc2”中删除了“2”,留下索引词只有“mvc”。不过,我不确定 SimpleAnalyzer。您可以尝试使用 WhitespaceAnalyzer,我相信它不会去除数字。

您还应该像处理索引一样处理搜索输入。 TermQuery 是“相同”匹配,这意味着如果您尝试搜索“mvc2”,而索引中的实际字符串总是显示“mvc”,那么您将找不到匹配项。

除非我使用 QueryParser,否则我还没有找到真正使用分析器的方法,即使那样我也总是得到奇怪的结果。

您可以尝试这样做,以便以与索引文档相同的方式“标记化”您的搜索字符串,并对所有术语进行 bool AND 搜索:

    // We use a boolean query to combine all prefix queries
var analyzer = new SimpleAnalyzer();
var query = new BooleanQuery();

using ( var reader = new StringReader( queryTerms ) )
{
// This is what we need to do in order to get the terms one by one, kind of messy but seemed to be the only way
var tokenStream = analyzer.TokenStream( "why_do_I_need_this", reader );
var termAttribute = tokenStream.GetAttribute( typeof( TermAttribute ) ) as TermAttribute;

// This will return false when all tokens has been processed.
while ( tokenStream.IncrementToken() )
{
var token = termAttribute.Term();
query.Add( new PrefixQuery( new Term( KEYWORDS_FIELD_NAME, token ) ), BooleanClause.Occur.MUST );
}

// I don't know if this is necessary, but can't hurt
tokenStream.Close();
}

如果您只需要完全匹配,您可以将 PrefixQuery 替换为 TermQuery(PrefixQuery 将匹配任何以“search*”开头的内容)

关于c# - 使用 lucene.net 搜索 "mvc2"时没有命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8103445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com