org.apache.lucene.index.IndexWriterConfig.OpenMode Java Examples

The following examples show how to use org.apache.lucene.index.IndexWriterConfig.OpenMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LuceneSearchService.java    From ApiManager with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean delete(SearchDto searchDto) throws IOException {
	IndexWriter writer = null;
	try {
		IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
		conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
		writer = new IndexWriter(FSDirectory.open(Paths.get(settingCache.get(ISetting.S_LUCENE_DIR).getValue())), conf);
		writer.deleteDocuments(new Term(ID, searchDto.getId()));
	} catch (Exception e) {
		e.printStackTrace();
		stringCache.add(IConst.C_CACHE_ERROR_TIP, "Lucene删除异常,请联系管理员查看日志,错误信息:" + e.getMessage());
	} finally {
		if (writer != null) {
			writer.close();
		}
	}
	return true;
}
 
Example #2
Source File: WordFrequencyStore.java    From SourcererCC with GNU General Public License v3.0 6 votes vote down vote up
public void prepareIndex() throws IOException {
    File globalWFMDIr = new File(Util.GTPM_INDEX_DIR);
    if (!globalWFMDIr.exists()) {
        Util.createDirs(Util.GTPM_INDEX_DIR);
    }
    KeywordAnalyzer keywordAnalyzer = new KeywordAnalyzer();
    IndexWriterConfig wfmIndexWriterConfig = new IndexWriterConfig(Version.LUCENE_46, keywordAnalyzer);
    wfmIndexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    wfmIndexWriterConfig.setRAMBufferSizeMB(1024);

    logger.info("PREPARE INDEX");
    try {
        wfmIndexWriter = new IndexWriter(FSDirectory.open(new File(Util.GTPM_INDEX_DIR)), wfmIndexWriterConfig);
        wfmIndexWriter.commit();
        wfmIndexer = new DocumentMaker(wfmIndexWriter);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: TestBackwardsCompatibility.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testUpgradeWithNRTReader() throws Exception {
  for (String name : oldNames) {
    Directory dir = newDirectory(oldIndexDirs.get(name));

    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random()))
                                         .setOpenMode(OpenMode.APPEND));
    writer.addDocument(new Document());
    DirectoryReader r = DirectoryReader.open(writer);
    writer.commit();
    r.close();
    writer.forceMerge(1);
    writer.commit();
    writer.rollback();
    SegmentInfos.readLatestCommit(dir);
    dir.close();
  }
}
 
Example #4
Source File: LucenePerUserWaveViewHandlerImpl.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
@Inject
public LucenePerUserWaveViewHandlerImpl(IndexDirectory directory,
                                        ReadableWaveletDataProvider waveletProvider,
                                        @Named(CoreSettingsNames.WAVE_SERVER_DOMAIN) String domain,
                                        @IndexExecutor Executor executor) {
  this.waveletProvider = waveletProvider;
  this.executor = executor;
  analyzer = new StandardAnalyzer(LUCENE_VERSION);
  try {
    IndexWriterConfig indexConfig = new IndexWriterConfig(LUCENE_VERSION, analyzer);
    indexConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    indexWriter = new IndexWriter(directory.getDirectory(), indexConfig);
    nrtManager = new NRTManager(indexWriter, new WaveSearchWarmer(domain));
  } catch (IOException ex) {
    throw new IndexException(ex);
  }

  nrtManagerReopenThread = new NRTManagerReopenThread(nrtManager, MAX_STALE_SEC, MIN_STALE_SEC);
  nrtManagerReopenThread.start();
}
 
Example #5
Source File: TestScorerPerf.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void createRandomTerms(int nDocs, int nTerms, double power, Directory dir) throws Exception {
  int[] freq = new int[nTerms];
  Term[] terms = new Term[nTerms];
  for (int i=0; i<nTerms; i++) {
    int f = (nTerms+1)-i;  // make first terms less frequent
    freq[i] = (int)Math.ceil(Math.pow(f,power));
    terms[i] = new Term("f",Character.toString((char)('A'+i)));
  }

  IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE));
  for (int i=0; i<nDocs; i++) {
    Document d = new Document();
    for (int j=0; j<nTerms; j++) {
      if (random().nextInt(freq[j]) == 0) {
        d.add(newStringField("f", terms[j].text(), Field.Store.NO));
        //System.out.println(d);
      }
    }
    iw.addDocument(d);
  }
  iw.forceMerge(1);
  iw.close();
}
 
Example #6
Source File: TestIndexWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCommitData() throws Exception {
  Directory dir = newDirectory();
  IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(null));
  writer.setLiveCommitData(new HashMap<String,String>() {{
    put("key", "value");
  }}.entrySet());
  assertEquals("value", getLiveCommitData(writer).get("key"));
  writer.close();

  // validate that it's also visible when opening a new IndexWriter
  writer = new IndexWriter(dir, newIndexWriterConfig(null)
                                  .setOpenMode(OpenMode.APPEND));
  assertEquals("value", getLiveCommitData(writer).get("key"));
  writer.close();

  dir.close();
}
 
Example #7
Source File: Txt2PubmedIdIndexer.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(UimaContext context)
        throws ResourceInitializationException {
    super.initialize(context);
    try {
        // create writer
        Directory dir;
        dir = FSDirectory.open(new File(INDEX_PATH));
        Analyzer analyzer = getAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(
                Version.LUCENE_41, analyzer);
        iwc.setOpenMode(OpenMode.CREATE);
        indexWriter = new IndexWriter(dir, iwc);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: TestBackwardsCompatibility.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void changeIndexNoAdds(Random random, Directory dir) throws IOException {
  // make sure searching sees right # hits
  DirectoryReader reader = DirectoryReader.open(dir);
  IndexSearcher searcher = newSearcher(reader);
  ScoreDoc[] hits = searcher.search(new TermQuery(new Term("content", "aaa")), 1000).scoreDocs;
  assertEquals("wrong number of hits", 34, hits.length);
  Document d = searcher.doc(hits[0].doc);
  assertEquals("wrong first document", "0", d.get("id"));
  reader.close();

  // fully merge
  IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random))
                                              .setOpenMode(OpenMode.APPEND));
  writer.forceMerge(1);
  writer.close();

  reader = DirectoryReader.open(dir);
  searcher = newSearcher(reader);
  hits = searcher.search(new TermQuery(new Term("content", "aaa")), 1000).scoreDocs;
  assertEquals("wrong number of hits", 34, hits.length);
  doTestHits(hits, 34, searcher.getIndexReader());
  reader.close();
}
 
Example #9
Source File: TestIndexWriterOnOldIndex.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testOpenModeAndCreatedVersion() throws IOException {
  assumeTrue("Reenable when 8.0 is released", false);
  InputStream resource = getClass().getResourceAsStream("index.single-empty-doc.8.0.0.zip");
  assertNotNull(resource);
  Path path = createTempDir();
  TestUtil.unzip(resource, path);
  Directory dir = newFSDirectory(path);
  for (OpenMode openMode : OpenMode.values()) {
    Directory tmpDir = newDirectory(dir);
    assertEquals(7 /** 7.0.0 */, SegmentInfos.readLatestCommit(tmpDir).getIndexCreatedVersionMajor());
    IndexWriter w = new IndexWriter(tmpDir, newIndexWriterConfig().setOpenMode(openMode));
    w.commit();
    w.close();
    switch (openMode) {
      case CREATE:
        assertEquals(Version.LATEST.major, SegmentInfos.readLatestCommit(tmpDir).getIndexCreatedVersionMajor());
        break;
      default:
        assertEquals(7 /** 7.0.0 */, SegmentInfos.readLatestCommit(tmpDir).getIndexCreatedVersionMajor());
    }
    tmpDir.close();
  }
  dir.close();
}
 
Example #10
Source File: TestPerFieldPostingsFormat2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testMergeUnusedPerFieldCodec() throws IOException {
  Directory dir = newDirectory();
  IndexWriterConfig iwconf = newIndexWriterConfig(new MockAnalyzer(random()))
                               .setOpenMode(OpenMode.CREATE).setCodec(new MockCodec());
  IndexWriter writer = newWriter(dir, iwconf);
  addDocs(writer, 10);
  writer.commit();
  addDocs3(writer, 10);
  writer.commit();
  addDocs2(writer, 10);
  writer.commit();
  assertEquals(30, writer.getDocStats().maxDoc);
  TestUtil.checkIndex(dir);
  writer.forceMerge(1);
  assertEquals(30, writer.getDocStats().maxDoc);
  writer.close();
  dir.close();
}
 
Example #11
Source File: TestDirectoryReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testIsCurrent() throws Exception {
  Directory d = newDirectory();
  IndexWriter writer = new IndexWriter(d, newIndexWriterConfig(new MockAnalyzer(random())));
  addDocumentWithFields(writer);
  writer.close();
  // set up reader:
  DirectoryReader reader = DirectoryReader.open(d);
  assertTrue(reader.isCurrent());
  // modify index by adding another document:
  writer = new IndexWriter(d, newIndexWriterConfig(new MockAnalyzer(random()))
                                .setOpenMode(OpenMode.APPEND));
  addDocumentWithFields(writer);
  writer.close();
  assertFalse(reader.isCurrent());
  // re-create index:
  writer = new IndexWriter(d, newIndexWriterConfig(new MockAnalyzer(random()))
                                .setOpenMode(OpenMode.CREATE));
  addDocumentWithFields(writer);
  writer.close();
  assertFalse(reader.isCurrent());
  reader.close();
  d.close();
}
 
Example #12
Source File: BaseLockFactoryTestCase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testStressLocks() throws Exception {
  Path tempPath = createTempDir();
  assumeFalse("cannot handle buggy Files.delete", TestUtil.hasWindowsFS(tempPath));

  Directory dir = getDirectory(tempPath);

  // First create a 1 doc index:
  IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE));
  addDoc(w);
  w.close();
  
  int numIterations = atLeast(20);
  WriterThread writer = new WriterThread(numIterations, dir);
  SearcherThread searcher = new SearcherThread(numIterations, dir);
  writer.start();
  searcher.start();

  writer.join();
  searcher.join();

  assertTrue("IndexWriter hit unexpected exceptions", !writer.hitException);
  assertTrue("IndexSearcher hit unexpected exceptions", !searcher.hitException);
  
  dir.close();
}
 
Example #13
Source File: TestIndexWriterMerging.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void fillIndex(Random random, Directory dir, int start, int numDocs) throws IOException {

    IndexWriter writer = new IndexWriter(
        dir,
        newIndexWriterConfig(new MockAnalyzer(random))
            .setOpenMode(OpenMode.CREATE)
            .setMaxBufferedDocs(2)
            .setMergePolicy(newLogMergePolicy(2))
    );

    for (int i = start; i < (start + numDocs); i++)
    {
      Document temp = new Document();
      temp.add(newStringField("count", (""+i), Field.Store.YES));

      writer.addDocument(temp);
    }
    writer.close();
  }
 
Example #14
Source File: PersistentSnapshotDeletionPolicy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * {@link PersistentSnapshotDeletionPolicy} wraps another
 * {@link IndexDeletionPolicy} to enable flexible snapshotting.
 * 
 * @param primary
 *          the {@link IndexDeletionPolicy} that is used on non-snapshotted
 *          commits. Snapshotted commits, by definition, are not deleted until
 *          explicitly released via {@link #release}.
 * @param dir
 *          the {@link Directory} which will be used to persist the snapshots
 *          information.
 * @param mode
 *          specifies whether a new index should be created, deleting all
 *          existing snapshots information (immediately), or open an existing
 *          index, initializing the class with the snapshots information.
 */
public PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary,
    Directory dir, OpenMode mode) throws IOException {
  super(primary);

  this.dir = dir;

  if (mode == OpenMode.CREATE) {
    clearPriorSnapshots();
  }

  loadPriorSnapshots();

  if (mode == OpenMode.APPEND && nextWriteGen == 0) {
    throw new IllegalStateException("no snapshots stored in this directory");
  }
}
 
Example #15
Source File: TestDirectoryTaxonomyWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommit() throws Exception {
  // Verifies that nothing is committed to the underlying Directory, if
  // commit() wasn't called.
  Directory dir = newDirectory();
  DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
  assertFalse(DirectoryReader.indexExists(dir));
  ltw.commit(); // first commit, so that an index will be created
  ltw.addCategory(new FacetLabel("a"));
  
  IndexReader r = DirectoryReader.open(dir);
  assertEquals("No categories should have been committed to the underlying directory", 1, r.numDocs());
  r.close();
  ltw.close();
  dir.close();
}
 
Example #16
Source File: TestAddIndexes.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWithPendingDeletes() throws IOException {
  // main directory
  Directory dir = newDirectory();
  // auxiliary directory
  Directory aux = newDirectory();

  setUpDirs(dir, aux);
  IndexWriter writer = newWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));
  writer.addIndexes(aux);

  // Adds 10 docs, then replaces them with another 10
  // docs, so 10 pending deletes:
  for (int i = 0; i < 20; i++) {
    Document doc = new Document();
    doc.add(newStringField("id", "" + (i % 10), Field.Store.NO));
    doc.add(newTextField("content", "bbb " + i, Field.Store.NO));
    doc.add(new IntPoint("doc", i));
    doc.add(new IntPoint("doc2d", i, i));
    doc.add(new NumericDocValuesField("dv", i));
    writer.updateDocument(new Term("id", "" + (i%10)), doc);
  }
  // Deletes one of the 10 added docs, leaving 9:
  PhraseQuery q = new PhraseQuery("content", "bbb", "14");
  writer.deleteDocuments(q);

  writer.forceMerge(1);
  writer.commit();

  verifyNumDocs(dir, 1039);
  verifyTermDocs(dir, new Term("content", "aaa"), 1030);
  verifyTermDocs(dir, new Term("content", "bbb"), 9);

  writer.close();
  dir.close();
  aux.close();
}
 
Example #17
Source File: LuceneUtil.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected Directory createLuceneDirectory(JasperPrint jasperPrint) throws IOException, JRException {
	Long start = System.currentTimeMillis();
	Directory dir = new RAMDirectory();
	Analyzer analyzer = getConfiguredAnalyzer();
	IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

	iwc.setOpenMode(OpenMode.CREATE);
	writer = new IndexWriter(dir, iwc);

	List<JRPrintPage> pages = jasperPrint.getPages();
	if (pages != null && pages.size() > 0) {
		if (log.isDebugEnabled()) {
			log.debug("there are " + pages.size() + " pages to be indexed");
		}
		for (int i = 0, ps = pages.size(); i < ps; i++) {
			if (log.isDebugEnabled()) {
				log.debug("indexing page: " + i);
			}
			indexPage(pages.get(i), i);
		}
	}

	writer.close();

	if (log.isDebugEnabled()) {
		log.debug("index creation took: " + (System.currentTimeMillis() - start) + " ms");
	}

	return dir;
}
 
Example #18
Source File: TestTermRangeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void initializeIndex(String[] values, Analyzer analyzer) throws IOException {
  IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(analyzer).setOpenMode(OpenMode.CREATE));
  for (int i = 0; i < values.length; i++) {
    insertDoc(writer, values[i]);
  }
  writer.close();
}
 
Example #19
Source File: TestAddIndexes.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testNoTailSegments() throws IOException {
  // main directory
  Directory dir = newDirectory();
  // auxiliary directory
  Directory aux = newDirectory();

  setUpDirs(dir, aux);

  IndexWriter writer = newWriter(
      dir,
      newIndexWriterConfig(new MockAnalyzer(random())).
          setOpenMode(OpenMode.APPEND).
          setMaxBufferedDocs(10).
          setMergePolicy(newLogMergePolicy(4))
  );
  addDocs(writer, 10);

  writer.addIndexes(aux);
  assertEquals(1040, writer.getDocStats().maxDoc);
  assertEquals(1000, writer.maxDoc(0));
  writer.close();

  // make sure the index is correct
  verifyNumDocs(dir, 1040);
  dir.close();
  aux.close();
}
 
Example #20
Source File: TestDirectoryReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFilesOpenClose() throws IOException {
  // Create initial data set
  Path dirFile = createTempDir("TestIndexReader.testFilesOpenClose");
  Directory dir = newFSDirectory(dirFile);
  
  IndexWriter writer  = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
  addDoc(writer, "test");
  writer.close();
  dir.close();

  // Try to erase the data - this ensures that the writer closed all files
  IOUtils.rm(dirFile);
  dir = newFSDirectory(dirFile);

  // Now create the data set again, just as before
  writer  = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random()))
                            .setOpenMode(OpenMode.CREATE));
  addDoc(writer, "test");
  writer.close();
  dir.close();

  // Now open existing directory and test that reader closes all files
  dir = newFSDirectory(dirFile);
  DirectoryReader reader1 = DirectoryReader.open(dir);
  reader1.close();
  dir.close();

  // The following will fail if reader did not close
  // all files
  IOUtils.rm(dirFile);
}
 
Example #21
Source File: TestIndexWriterLockRelease.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testIndexWriterLockRelease() throws IOException {
  Directory dir = newFSDirectory(createTempDir("testLockRelease"));
  try {
    new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));
  } catch (FileNotFoundException | NoSuchFileException e) {
    try {
      new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));
    } catch (FileNotFoundException | NoSuchFileException e1) {
    }
  } finally {
    dir.close();
  }
}
 
Example #22
Source File: TestPersistentSnapshotDeletionPolicy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSnapshotReleaseByGeneration() throws Exception {
  Directory dir = newDirectory();
  IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy(dir)));
  PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();
  prepareIndexAndSnapshots(psdp, writer, 1);
  writer.close();

  psdp.release(snapshots.get(0).getGeneration());

  psdp = new PersistentSnapshotDeletionPolicy(
      new KeepOnlyLastCommitDeletionPolicy(), dir, OpenMode.APPEND);
  assertEquals("Should have no snapshots !", 0, psdp.getSnapshotCount());
  dir.close();
}
 
Example #23
Source File: TestPerFieldPostingsFormat2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testStressPerFieldCodec() throws IOException {
  Directory dir = newDirectory(random());
  final int docsPerRound = 97;
  int numRounds = atLeast(1);
  for (int i = 0; i < numRounds; i++) {
    int num = TestUtil.nextInt(random(), 30, 60);
    IndexWriterConfig config = newIndexWriterConfig(random(),
        new MockAnalyzer(random()));
    config.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = newWriter(dir, config);
    for (int j = 0; j < docsPerRound; j++) {
      final Document doc = new Document();
      for (int k = 0; k < num; k++) {
        FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
        customType.setTokenized(random().nextBoolean());
        customType.setOmitNorms(random().nextBoolean());
        Field field = newField("" + k, TestUtil
            .randomRealisticUnicodeString(random(), 128), customType);
        doc.add(field);
      }
      writer.addDocument(doc);
    }
    if (random().nextBoolean()) {
      writer.forceMerge(1);
    }
    writer.commit();
    assertEquals((i + 1) * docsPerRound, writer.getDocStats().maxDoc);
    writer.close();
  }
  dir.close();
}
 
Example #24
Source File: Indexer.java    From gerbil with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Indexer create(String indexDirPath) {
    Directory indexDirectory = null;
    try {
        indexDirectory = FSDirectory.open(new File(indexDirPath).toPath());
        IndexWriterConfig config = new IndexWriterConfig();
        config.setOpenMode(OpenMode.CREATE);
        IndexWriter indexWriter = new IndexWriter(indexDirectory, config);
        return new Indexer(indexDirectory, indexWriter);
    } catch (IOException e) {
        LOGGER.error("Exception while trying to create index writer for entity checking. Returning null.", e);
        IOUtils.closeQuietly(indexDirectory);
        return null;
    }
}
 
Example #25
Source File: TestDirectoryReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void addDoc(Random random, Directory ramDir1, String s, boolean create) throws IOException {
  IndexWriter iw = new IndexWriter(ramDir1, newIndexWriterConfig(new MockAnalyzer(random))
                                              .setOpenMode(create ? OpenMode.CREATE : OpenMode.APPEND));
  Document doc = new Document();
  doc.add(newTextField("body", s, Field.Store.NO));
  iw.addDocument(doc);
  iw.close();
}
 
Example #26
Source File: TestDirectoryTaxonomyWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testReaderFreshness() throws Exception {
  // ensures that the internal index reader is always kept fresh. Previously,
  // this simple scenario failed, if the cache just evicted the category that
  // is being added.
  Directory dir = newDirectory();
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE, NO_OP_CACHE);
  int o1 = taxoWriter.addCategory(new FacetLabel("a"));
  int o2 = taxoWriter.addCategory(new FacetLabel("a"));
  assertTrue("ordinal for same category that is added twice should be the same !", o1 == o2);
  taxoWriter.close();
  dir.close();
}
 
Example #27
Source File: TestTermdocPerf.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void addDocs(final Random random, Directory dir, final int ndocs, String field, final String val, final int maxTF, final float percentDocs) throws IOException {

    Analyzer analyzer = new Analyzer() {
      @Override
      public TokenStreamComponents createComponents(String fieldName) {
        return new TokenStreamComponents(new RepeatingTokenizer(val, random, percentDocs, maxTF));
      }
    };

    Document doc = new Document();
    
    doc.add(newStringField(field, val, Field.Store.NO));
    IndexWriter writer = new IndexWriter(
        dir,
        newIndexWriterConfig(analyzer)
          .setOpenMode(OpenMode.CREATE)
          .setMaxBufferedDocs(100)
          .setMergePolicy(newLogMergePolicy(100))
    );

    for (int i=0; i<ndocs; i++) {
      writer.addDocument(doc);
    }

    writer.forceMerge(1);
    writer.close();
  }
 
Example #28
Source File: TestTryDelete.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static IndexWriter getWriter (Directory directory)
  throws IOException
{
  MergePolicy policy = new LogByteSizeMergePolicy();
  IndexWriterConfig conf = new IndexWriterConfig(new MockAnalyzer(random()));
  conf.setMergePolicy(policy);
  conf.setOpenMode(OpenMode.CREATE_OR_APPEND);

  IndexWriter writer = new IndexWriter(directory, conf);

  return writer;
}
 
Example #29
Source File: SolrSnapshotMetaDataManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * A constructor.
 *
 * @param dir The directory where the snapshot meta-data is stored.
 * @param mode CREATE If previous meta-data should be erased.
 *             APPEND If previous meta-data should be read and updated.
 *             CREATE_OR_APPEND Creates a new meta-data structure if one does not exist
 *                              Updates the existing structure if one exists.
 * @throws IOException in case of errors.
 */
public SolrSnapshotMetaDataManager(SolrCore solrCore, Directory dir, OpenMode mode) throws IOException {
  this.solrCore = solrCore;
  this.dir = dir;

  if (mode == OpenMode.CREATE) {
    deleteSnapshotMetadataFiles();
  }

  loadFromSnapshotMetadataFile();

  if (mode == OpenMode.APPEND && nextWriteGen == 0) {
    throw new IllegalStateException("no snapshots stored in this directory");
  }
}
 
Example #30
Source File: TestMultipleIndexFields.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefault() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  // create and open an index writer
  RandomIndexWriter iw = new RandomIndexWriter(random(), indexDir, newIndexWriterConfig(
      new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
  // create and open a taxonomy writer
  TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
  FacetsConfig config = getConfig();

  seedIndex(tw, iw, config);

  IndexReader ir = iw.getReader();
  tw.commit();

  // prepare index reader and taxonomy.
  TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);

  // prepare searcher to search against
  IndexSearcher searcher = newSearcher(ir);

  FacetsCollector sfc = performSearch(tr, ir, searcher);

  // Obtain facets results and hand-test them
  assertCorrectResults(getTaxonomyFacetCounts(tr, config, sfc));

  assertOrdinalsExist("$facets", ir);

  iw.close();
  IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}