每个领域的不同分析仪

时间:2010-05-16 09:00:02

标签: lucene lucene.net

如何为我正在使用Lucene编制索引的文档中的每个字段启用不同的分析器?例如:

        RAMDirectory dir = new RAMDirectory();
        IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.UNLIMITED);
        Document doc = new Document();
        Field field1 = new Field("field1", someText1, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
        Field field2 = new Field("field2", someText2, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
        doc.Add(field1);
        doc.Add(field2);
        iw.AddDocument(doc);
        iw.Commit();

分析器是IndexWriter的参数,但是我想对field1使用StandardAnalyzer而对field2使用SimpleAnalyzer,我该怎么做?当然,搜索时同样适用。必须为每个字段应用正确的分析仪。

2 个答案:

答案 0 :(得分:26)

PerFieldAnalyzerWrapper正是您要找的。在Lucene.net中相当于here

答案 1 :(得分:8)

Map<String, Analyzer> analyzerMap = new HashMap<String, Analyzer>();
analyzerMap.put(fieldone, new IKAnalyzer4PinYin(false, IKAnalyzer4PinYin.PINYIN));
analyzerMap.put(fieldtwo, new IKAnalyzer4PinYin(false, KAnalyzer4PinYin.PINYIN_SHOUZIMU));
PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new IKAnalyzer4PinYin(false), analyzerMap);

IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_40 , wrapper);