Search in sources :

Example 1 with AnsjIndexAnalysis

use of org.ansj.lucene4.AnsjIndexAnalysis in project ansj_seg by NLPchina.

the class IndexTest method indexTest.

@Test
public void indexTest() throws CorruptIndexException, LockObtainFailedException, IOException, ParseException {
    MyStaticValue.DIC.put(MyStaticValue.DIC_DEFAULT, "../../library/default.dic");
    HashSet<String> hs = new HashSet<String>();
    hs.add("的");
    Analyzer analyzer = new AnsjIndexAnalysis(hs, false);
    Directory directory = null;
    IndexWriter iwriter = null;
    String text = "季德胜蛇药片 10片*6板 ";
    UserDefineLibrary.insertWord("蛇药片", "n", 1000);
    IndexWriterConfig ic = new IndexWriterConfig(Version.LUCENE_44, analyzer);
    // 建立内存索引对象
    directory = new RAMDirectory();
    iwriter = new IndexWriter(directory, ic);
    addContent(iwriter, text);
    iwriter.commit();
    iwriter.close();
    System.out.println("索引建立完毕");
    Analyzer queryAnalyzer = new AnsjAnalysis(hs, false);
    ;
    System.out.println("index ok to search!");
    search(queryAnalyzer, directory, "\"季德胜蛇药片\"");
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) Analyzer(org.apache.lucene.analysis.Analyzer) AnsjIndexAnalysis(org.ansj.lucene4.AnsjIndexAnalysis) RAMDirectory(org.apache.lucene.store.RAMDirectory) AnsjAnalysis(org.ansj.lucene4.AnsjAnalysis) HashSet(java.util.HashSet) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Aggregations

HashSet (java.util.HashSet)1 AnsjAnalysis (org.ansj.lucene4.AnsjAnalysis)1 AnsjIndexAnalysis (org.ansj.lucene4.AnsjIndexAnalysis)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 Directory (org.apache.lucene.store.Directory)1 RAMDirectory (org.apache.lucene.store.RAMDirectory)1 Test (org.junit.Test)1