Search in sources :

Example 66 with Directory

use of org.apache.lucene.store.Directory 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 67 with Directory

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

the class NearTest method createIndex.

public static void createIndex() throws Exception {
    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_32, new AnsjAnalysis());
    Directory directory = FSDirectory.open(new File("c:/index"));
    IndexWriter writer = new IndexWriter(directory, conf);
    String str = "文化人;文化人谈文化";
    String[] values = str.split(";");
    for (String value : values) {
        Document doc = new Document();
        Field field = new Field("test", value, Store.YES, Index.ANALYZED_NO_NORMS, TermVector.WITH_POSITIONS_OFFSETS);
        //			field.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
        doc.add(field);
        writer.addDocument(doc);
        writer.commit();
    }
    writer.close();
}
Also used : Field(org.apache.lucene.document.Field) IndexWriter(org.apache.lucene.index.IndexWriter) Document(org.apache.lucene.document.Document) File(java.io.File) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 68 with Directory

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

the class SimpleUniquenessVerifierTest method initLuceneResources.

@Before
public void initLuceneResources() throws Exception {
    dirFactory = new DirectoryFactory.InMemoryDirectoryFactory();
    Directory dir = dirFactory.open(testDir.directory("test"));
    writer = new IndexWriter(dir, IndexWriterConfigs.standard());
    searcherManager = new SearcherManager(writer, true, new SearcherFactory());
}
Also used : SearcherFactory(org.apache.lucene.search.SearcherFactory) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryFactory(org.neo4j.kernel.api.impl.index.storage.DirectoryFactory) SearcherManager(org.apache.lucene.search.SearcherManager) Directory(org.apache.lucene.store.Directory) TestDirectory(org.neo4j.test.rule.TestDirectory) Before(org.junit.Before)

Example 69 with Directory

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

the class PartitionedIndexStorageTest method prepareFolderRemovesFromLucene.

@Test
public void prepareFolderRemovesFromLucene() throws IOException {
    File folder = createRandomFolder(testDir.graphDbDir());
    Directory dir = createRandomLuceneDir(folder);
    assertFalse(ArrayUtil.isEmpty(dir.listAll()));
    storage.prepareFolder(folder);
    assertTrue(fs.fileExists(folder));
    assertTrue(ArrayUtil.isEmpty(dir.listAll()));
}
Also used : File(java.io.File) Directory(org.apache.lucene.store.Directory) TestDirectory(org.neo4j.test.rule.TestDirectory) Test(org.junit.Test)

Example 70 with Directory

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

the class PartitionedIndexStorageTest method openIndexDirectoriesForEmptyIndex.

@Test
public void openIndexDirectoriesForEmptyIndex() throws IOException {
    File indexFolder = storage.getIndexFolder();
    Map<File, Directory> directories = storage.openIndexDirectories();
    assertTrue(directories.isEmpty());
}
Also used : File(java.io.File) Directory(org.apache.lucene.store.Directory) TestDirectory(org.neo4j.test.rule.TestDirectory) Test(org.junit.Test)

Aggregations

Directory (org.apache.lucene.store.Directory)2194 Document (org.apache.lucene.document.Document)1374 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)816 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)669 IndexReader (org.apache.lucene.index.IndexReader)590 IndexSearcher (org.apache.lucene.search.IndexSearcher)383 BytesRef (org.apache.lucene.util.BytesRef)376 RAMDirectory (org.apache.lucene.store.RAMDirectory)360 Term (org.apache.lucene.index.Term)325 StringField (org.apache.lucene.document.StringField)313 IndexWriter (org.apache.lucene.index.IndexWriter)312 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)274 TextField (org.apache.lucene.document.TextField)259 Field (org.apache.lucene.document.Field)257 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)257 Test (org.junit.Test)237 FSDirectory (org.apache.lucene.store.FSDirectory)221 Analyzer (org.apache.lucene.analysis.Analyzer)193 DirectoryReader (org.apache.lucene.index.DirectoryReader)193 TopDocs (org.apache.lucene.search.TopDocs)174