开发者

Different analyzers for each field

How can I enable different analyzers for each field in a document I'm in开发者_高级运维dexing with Lucene? Example:

        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();

The analyzer is an argument to the IndexWriter, but I want to use StandardAnalyzer for field1 and SimpleAnalyzer for field2, how can I do that? The same applies when searching, of course. The correct analyzer must be applied for each field.


PerFieldAnalyzerWrapper is what you are looking for. The equivalent of this in Lucene.net is here.


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);


Necromancing.
For C#:

Lucene.Net.Util.LuceneVersion version = Lucene.Net.Util.LuceneVersion.LUCENE_48;

Dictionary<string, Lucene.Net.Analysis.Analyzer> fieldAnalyzers = 
    new Dictionary<string, Lucene.Net.Analysis.Analyzer>(System.StringComparer.OrdinalIgnoreCase);

fieldAnalyzers["YourFieldName"] = new Lucene.Net.Analysis.Core.KeywordAnalyzer();

Lucene.Net.Analysis.Miscellaneous.PerFieldAnalyzerWrapper wrapper = 
    new Lucene.Net.Analysis.Miscellaneous.PerFieldAnalyzerWrapper(
        new Lucene.Net.Analysis.Core.KeywordAnalyzer(), fieldAnalyzers);

Lucene.Net.Index.IndexWriterConfig writerConfig = new Lucene.Net.Index.IndexWriterConfig(version, wrapper);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜