2010-05-16 27 views
19

¿Cómo puedo habilitar diferentes analizadores para cada campo en un documento que estoy indexando con Lucene? Ejemplo:Diferentes analizadores para cada campo

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

El analizador es un argumento a la IndexWriter, pero quiero utilizar StandardAnalyzer para campo1 y campo2 para SimpleAnalyzer, ¿cómo puedo hacer eso? Lo mismo aplica cuando se busca, por supuesto. El analizador correcto debe aplicarse para cada campo.

Respuesta

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