3

在我的情况下,我需要搜索 C#、.Net、C++..etc 等关键字,其中标准分析器会去除特殊字符,所以我使用了空白分析器,它对我不起作用。索引时:

public void Indexing(DataSet ds)
{
        string indexFileLocation = @"D:\Lucene.Net\Data";
        Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, true);
        IndexWriter indexWriter = new IndexWriter(dir, new WhitespaceAnalyzer(), Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
        if (ds.Tables[0] != null)
        {
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
               {
                    //Create the Document object
                    Document doc = new Document();

                    foreach (DataColumn dc in dt.Columns)
                    {
                        string check = dc.ToString();

                        if (check.Equals("Skill_Summary"))
                        {
                            doc.Add(new Field(dc.ColumnName, dr[dc.ColumnName].ToString(), Field.Store.YES, Field.Index.ANALYZED));
                        }
                        if (check.Equals("Title"))
                        {
                            doc.Add(new Field(dc.ColumnName, dr[dc.ColumnName].ToString(), Field.Store.YES, Field.Index.ANALYZED));
                        }
                    }
                    // Write the Document to the catalog
                    indexWriter.AddDocument(doc);
                }
            }
        }
        // Close the writer
        indexWriter.Close();
    }

和搜索领域,如:

string[] searchfields = new string[] { "Skill_Summary", "Title" };
var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, searchfields, new WhitespaceAnalyzer());
string searchText = "C#";

//Split the search string into separate search terms by word
string[] terms = searchText.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
foreach (string term in terms)
{
    finalQuery.Add(parser.Parse(term.Replace("*", "") + "*"), BooleanClause.Occur.MUST);
}
hits = searcher.Search(finalQuery);

在我的情况下,如何使用 Whitespaceanalyzer 和 LowerCase 过滤器构建自己的分析器?

4

1 回答 1

8

在我的情况下,如何使用 Whitespaceanalyzer 和 LowerCase 过滤器构建自己的分析器?

public class CaseInsensitiveWhitespaceAnalyzer : Analyzer
{
    /// <summary>
    /// </summary>
    public override TokenStream TokenStream(string fieldName, TextReader reader)
    {
        TokenStream t = null;
        t = new WhitespaceTokenizer(reader);
        t = new LowerCaseFilter(t);

        return t;
    }
}

PS:当你使用通配符(?*)时,查询解析器不使用任何分析器,只是你的术语的小写形式(取决于 QueryParser.LowercaseExpandedTerms 的值)

于 2013-07-27T18:28:34.083 回答