Search in sources :

Example 26 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project ansj_seg by NLPchina.

the class PhraseTest method main.

public static void main(String[] args) throws IOException, ParseException {
    DicLibrary.insert(DicLibrary.DEFAULT, "上网人");
    DicLibrary.insert(DicLibrary.DEFAULT, "网人");
    AnsjAnalyzer ansjAnalyzer = new AnsjAnalyzer(AnsjAnalyzer.TYPE.index_ansj);
    TokenStream tokenStream = ansjAnalyzer.tokenStream("上网人员测试", "test");
    while (tokenStream.incrementToken()) {
        System.out.println(tokenStream.getAttribute(CharTermAttribute.class));
    }
    IndexWriterConfig config = new IndexWriterConfig(ansjAnalyzer);
    IndexWriter writer = new IndexWriter(new RAMDirectory(), config);
    Document doc = new Document();
    doc.add(new TextField("test", "上网人员测试", Field.Store.YES));
    writer.addDocument(doc);
    writer.commit();
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer));
    System.out.println(searcher.count(new TermQuery(new Term("test", "网人"))));
    Query q = new QueryParser("test", new AnsjAnalyzer(AnsjAnalyzer.TYPE.index_ansj)).parse("\"上网人\"");
    System.out.println(q);
    System.out.println(searcher.count(q));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) AnsjAnalyzer(org.ansj.lucene6.AnsjAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) TextField(org.apache.lucene.document.TextField) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 27 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory 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)

Example 28 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project languagetool by languagetool-org.

the class PatternRuleQueryBuilderTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    language = new English();
    directory = new RAMDirectory();
    /*File indexPath = new File("/tmp/lucene");
    if (indexPath.exists()) {
      FileUtils.deleteDirectory(indexPath);
    }
    directory = FSDirectory.open(indexPath);*/
    Analyzer analyzer = Indexer.getAnalyzer(language);
    IndexWriterConfig config = Indexer.getIndexWriterConfig(analyzer);
    try (IndexWriter writer = new IndexWriter(directory, config)) {
        addDocument(writer, "How do you thin about this wonderful idea?");
        addDocument(writer, "The are several grammar checkers for English, E.G. LanguageTool 123.");
    }
    reader = DirectoryReader.open(directory);
    searcher = newSearcher(reader);
}
Also used : English(org.languagetool.language.English) IndexWriter(org.apache.lucene.index.IndexWriter) Analyzer(org.apache.lucene.analysis.Analyzer) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 29 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project languagetool by languagetool-org.

the class IndexerSearcherTest method createIndex.

private void createIndex(String content) throws IOException {
    directory = new RAMDirectory();
    //directory = FSDirectory.open(new File("/tmp/lucenetest"));  // for debugging
    Indexer.run(content, directory, new English());
    errorSearcher = new Searcher(directory);
}
Also used : English(org.languagetool.language.English) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 30 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project neo4j by neo4j.

the class LuceneSchemaIndexPopulatorTest method before.

@Before
public void before() throws Exception {
    directory = new RAMDirectory();
    DirectoryFactory directoryFactory = new DirectoryFactory.Single(new DirectoryFactory.UncloseableDirectory(directory));
    provider = new LuceneSchemaIndexProvider(fs.get(), directoryFactory, testDir.directory("folder"), NullLogProvider.getInstance(), Config.empty(), OperationalMode.single);
    indexStoreView = mock(IndexStoreView.class);
    IndexSamplingConfig samplingConfig = new IndexSamplingConfig(Config.empty());
    indexPopulator = provider.getPopulator(indexId, index, samplingConfig);
    indexPopulator.create();
    indexPopulator.configureSampling(true);
}
Also used : IndexSamplingConfig(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig) DirectoryFactory(org.neo4j.kernel.api.impl.index.storage.DirectoryFactory) IndexStoreView(org.neo4j.kernel.impl.api.index.IndexStoreView) RAMDirectory(org.apache.lucene.store.RAMDirectory) Before(org.junit.Before)

Aggregations

RAMDirectory (org.apache.lucene.store.RAMDirectory)183 Directory (org.apache.lucene.store.Directory)101 IndexWriter (org.apache.lucene.index.IndexWriter)82 Document (org.apache.lucene.document.Document)75 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)73 IndexSearcher (org.apache.lucene.search.IndexSearcher)43 IndexReader (org.apache.lucene.index.IndexReader)41 Test (org.junit.Test)35 TextField (org.apache.lucene.document.TextField)33 Field (org.apache.lucene.document.Field)29 Term (org.apache.lucene.index.Term)25 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)21 Before (org.junit.Before)21 IOException (java.io.IOException)19 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)19 Analyzer (org.apache.lucene.analysis.Analyzer)18 TopDocs (org.apache.lucene.search.TopDocs)16 DirectoryReader (org.apache.lucene.index.DirectoryReader)15 FilterDirectory (org.apache.lucene.store.FilterDirectory)15 FieldType (org.apache.lucene.document.FieldType)13