Java Code Examples for org.apache.lucene.index.DirectoryReader#indexExists()

The following examples show how to use org.apache.lucene.index.DirectoryReader#indexExists() . These examples are extracted from open source projects. 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 Project: onedev   File: DefaultIndexManager.java    License: MIT License 6 votes vote down vote up
private IndexResult doIndex(Project project, ObjectId commit) {
	try (Directory directory = FSDirectory.open(storageManager.getProjectIndexDir(project.getId()).toPath())) {
		if (DirectoryReader.indexExists(directory)) {
			try (IndexReader reader = DirectoryReader.open(directory)) {
				IndexSearcher searcher = new IndexSearcher(reader);
				if (getIndexVersion().equals(getCommitIndexVersion(searcher, commit)))
					return new IndexResult(0, 0);
				else
					return doIndex(project, commit, directory, searcher);
			}
		} else {
			return doIndex(project, commit, directory, null);
		}
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 2
/**
 * Constructor with the given index directory and callback to notify when the
 * indexes were updated.
 */
public IndexReplicationHandler(Directory indexDir, Callable<Boolean> callback) throws IOException {
  this.callback = callback;
  this.indexDir = indexDir;
  currentRevisionFiles = null;
  currentVersion = null;
  if (DirectoryReader.indexExists(indexDir)) {
    final List<IndexCommit> commits = DirectoryReader.listCommits(indexDir);
    final IndexCommit commit = commits.get(commits.size() - 1);
    currentRevisionFiles = IndexRevision.revisionFiles(commit);
    currentVersion = IndexRevision.revisionVersion(commit);
    final InfoStream infoStream = InfoStream.getDefault();
    if (infoStream.isEnabled(INFO_STREAM_COMPONENT)) {
      infoStream.message(INFO_STREAM_COMPONENT, "constructor(): currentVersion=" + currentVersion
          + " currentRevisionFiles=" + currentRevisionFiles);
      infoStream.message(INFO_STREAM_COMPONENT, "constructor(): commit=" + commit);
    }
  }
}
 
Example 3
/** Create a new instance, loading from a previously built
   *  AnalyzingInfixSuggester directory, if it exists.  This directory must be
   *  private to the infix suggester (i.e., not an external
   *  Lucene index).  Note that {@link #close}
   *  will also close the provided directory.
   *
   *  @param minPrefixChars Minimum number of leading characters
   *     before PrefixQuery is used (default 4).
   *     Prefixes shorter than this are indexed as character
   *     ngrams (increasing index size but making lookups
   *     faster).
   *
   *  @param commitOnBuild Call commit after the index has finished building. This would persist the
   *                       suggester index to disk and future instances of this suggester can use this pre-built dictionary.
   *
   *  @param allTermsRequired All terms in the suggest query must be matched.
   *  @param highlight Highlight suggest query in suggestions.
   *  @param closeIndexWriterOnBuild If true, the IndexWriter will be closed after the index has finished building.
   */
public AnalyzingInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars,
                               boolean commitOnBuild, boolean allTermsRequired, 
                               boolean highlight, boolean closeIndexWriterOnBuild) throws IOException {
                                  
  if (minPrefixChars < 0) {
    throw new IllegalArgumentException("minPrefixChars must be >= 0; got: " + minPrefixChars);
  }

  this.queryAnalyzer = queryAnalyzer;
  this.indexAnalyzer = indexAnalyzer;
  this.dir = dir;
  this.minPrefixChars = minPrefixChars;
  this.commitOnBuild = commitOnBuild;
  this.allTermsRequired = allTermsRequired;
  this.highlight = highlight;
  this.closeIndexWriterOnBuild = closeIndexWriterOnBuild;

  if (DirectoryReader.indexExists(dir)) {
    // Already built; open it:
    searcherMgr = new SearcherManager(dir, null);
  }
}
 
Example 4
private void assertFiles(Set<String> expected, int run, int commit, FastHdfsKeyValueDirectory directory)
    throws IOException {
  Set<String> actual;
  if (DirectoryReader.indexExists(directory)) {
    List<IndexCommit> listCommits = DirectoryReader.listCommits(directory);
    // assertEquals(1, listCommits.size());
    IndexCommit indexCommit = listCommits.get(0);
    actual = new TreeSet<String>(indexCommit.getFileNames());
  } else {
    actual = new TreeSet<String>();
  }

  Set<String> missing = new TreeSet<String>(expected);
  missing.removeAll(actual);
  Set<String> extra = new TreeSet<String>(actual);
  extra.removeAll(expected);
  assertEquals("Pass [" + run + "] Missing Files " + " Extra Files " + extra + "", expected, actual);
}
 
Example 5
Source Project: Stargraph   File: LuceneSearcher.java    License: MIT License 5 votes vote down vote up
private synchronized IndexSearcher getLuceneSearcher() {
    try {
        if (indexReader == null) {
            if (!DirectoryReader.indexExists(directory)) {
                return null;
            }
            indexReader = DirectoryReader.open(directory);
            indexSearcher = new IndexSearcher(indexReader);
        }
        return indexSearcher;
    }
    catch (IOException e) {
        throw new StarGraphException(e);
    }
}
 
Example 6
Source Project: incubator-retired-blur   File: LoadTest.java    License: Apache License 2.0 5 votes vote down vote up
private static void runTest(AccessControlFactory accessControlFactory) throws IOException {
  File file = new File("./src/test/resouces/loadtestindex-" + accessControlFactory.getClass().getName());
  FSDirectory directory = FSDirectory.open(file);
  if (!file.exists() || !DirectoryReader.indexExists(directory)) {
    long s = System.nanoTime();
    createIndex(directory, accessControlFactory);
    long e = System.nanoTime();
    System.out.println("Index Creation Time [" + (e - s) / 1000000.0 + "]");
  }
  DirectoryReader reader = DirectoryReader.open(directory);

  IndexSearcher searcher = new IndexSearcher(reader);

  SecureIndexSearcher secureIndexSearcher1 = new SecureIndexSearcher(reader, accessControlFactory,
      Arrays.asList("nothing"), Arrays.asList("nothing"), new HashSet<String>(), null);

  SecureIndexSearcher secureIndexSearcher2 = new SecureIndexSearcher(reader, accessControlFactory,
      Arrays.asList("r1"), Arrays.asList("nothing"), new HashSet<String>(), null);

  MatchAllDocsQuery query = new MatchAllDocsQuery();
  for (int p = 0; p < 10; p++) {
    hitEnterToContinue();
    runSearch(searcher, query);
    hitEnterToContinue();
    runSearch(secureIndexSearcher1, query);
    hitEnterToContinue();
    runSearch(secureIndexSearcher2, query);
  }
}
 
Example 7
private void postInit() throws IOException {
	this.queryAnalyzer = new StandardAnalyzer();

	// do some initialization for new indices
	if (!DirectoryReader.indexExists(directory)) {
		logger.debug("creating new Lucene index in directory {}", directory);
		IndexWriterConfig indexWriterConfig = getIndexWriterConfig();
		indexWriterConfig.setOpenMode(OpenMode.CREATE);
		IndexWriter writer = new IndexWriter(directory, indexWriterConfig);
		writer.close();
	}
}
 
Example 8
Source Project: clue   File: ClueApplication.java    License: Apache License 2.0 5 votes vote down vote up
public ClueApplication(String idxLocation, boolean interactiveMode) throws Exception{
  ctx = newContext(idxLocation, config, interactiveMode);

  if (!DirectoryReader.indexExists(ctx.getDirectory())){
    System.out.println("lucene index does not exist at: "+idxLocation);
    System.exit(1);
  }
}
 
Example 9
/**
 * Returns the last {@link IndexCommit} found in the {@link Directory}, or
 * {@code null} if there are no commits.
 */
public static IndexCommit getLastCommit(Directory dir) throws IOException {
  try {
    if (DirectoryReader.indexExists(dir)) {
      List<IndexCommit> commits = DirectoryReader.listCommits(dir);
      // listCommits guarantees that we get at least one commit back, or
      // IndexNotFoundException which we handle below
      return commits.get(commits.size() - 1);
    }
  } catch (IndexNotFoundException e) {
    // ignore the exception and return null
  }
  return null;
}
 
Example 10
public IndexReadyCallback(Directory indexDir) throws IOException {
  this.indexDir = indexDir;
  if (DirectoryReader.indexExists(indexDir)) {
    reader = DirectoryReader.open(indexDir);
    lastGeneration = reader.getIndexCommit().getGeneration();
  }
}
 
Example 11
public IndexAndTaxonomyReadyCallback(Directory indexDir, Directory taxoDir) throws IOException {
  this.indexDir = indexDir;
  this.taxoDir = taxoDir;
  config = new FacetsConfig();
  config.setHierarchical("A", true);
  if (DirectoryReader.indexExists(indexDir)) {
    indexReader = DirectoryReader.open(indexDir);
    lastIndexGeneration = indexReader.getIndexCommit().getGeneration();
    taxoReader = new DirectoryTaxonomyReader(taxoDir);
  }
}
 
Example 12
Source Project: lucene-solr   File: SpellChecker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Use a different index as the spell checker index or re-open
 * the existing index if <code>spellIndex</code> is the same value
 * as given in the constructor.
 * @param spellIndexDir the spell directory to use
 * @throws AlreadyClosedException if the Spellchecker is already closed
 * @throws  IOException if spellchecker can not open the directory
 */
// TODO: we should make this final as it is called in the constructor
public void setSpellIndex(Directory spellIndexDir) throws IOException {
  // this could be the same directory as the current spellIndex
  // modifications to the directory should be synchronized 
  synchronized (modifyCurrentIndexLock) {
    ensureOpen();
    if (!DirectoryReader.indexExists(spellIndexDir)) {
        IndexWriter writer = new IndexWriter(spellIndexDir,
          new IndexWriterConfig(null));
        writer.close();
    }
    swapSearcher(spellIndexDir);
  }
}
 
Example 13
@Override
public void close() throws IOException {
  if (isOpen) {
    isOpen = false;
    if (checkIndexOnClose && DirectoryReader.indexExists(this)) {
      TestUtil.checkIndex(this, doSlowChecksOnClose);
    }
  }
  super.close();
}
 
Example 14
private IndexReader createIndexReader(String indexerPath) throws IOException {
	File indexfile = new File(indexerPath);
	indexDir = FSDirectory.open(indexfile.toPath());


	if (!DirectoryReader.indexExists(indexDir)) {
		LOG.log(Level.SEVERE,
				"No Lucene Index Dierctory Found, Invoke indexBuild() First !");
		System.exit(1);
	}

	return DirectoryReader.open(indexDir);
}
 
Example 15
/**
 * Build the gazetteer index line by line
 *
 * @param gazetteerPath
 *            path of the gazetteer file
 * @param indexerPath
 *            path to the created Lucene index directory.
 * @param reverseGeocodingEnabled 
 * @throws IOException
 * @throws RuntimeException
 */
public void buildIndex(String gazetteerPath, String indexerPath, boolean reverseGeocodingEnabled)
		throws IOException {
	File indexfile = new File(indexerPath);
	indexDir = FSDirectory.open(indexfile.toPath());
	if (!DirectoryReader.indexExists(indexDir)) {
		IndexWriterConfig config = new IndexWriterConfig(analyzer);
		indexWriter = new IndexWriter(indexDir, config);
		Logger logger = Logger.getLogger(this.getClass().getName());
		logger.log(Level.WARNING, "Start Building Index for Gazatteer");
		BufferedReader filereader = new BufferedReader(
				new InputStreamReader(new FileInputStream(gazetteerPath),
						"UTF-8"));
		String line;
		int count = 0;
		while ((line = filereader.readLine()) != null) {
			try {
				count += 1;
				if (count % 100000 == 0) {
					logger.log(Level.INFO, "Indexed Row Count: " + count);
				}
				addDoc(indexWriter, line, reverseGeocodingEnabled);

			} catch (RuntimeException re) {
				logger.log(Level.WARNING, "Skipping... Error on line: {0}",
						line);
				re.printStackTrace();
			}
		}
		logger.log(Level.WARNING, "Building Finished");
		filereader.close();
		indexWriter.close();
	}
}
 
Example 16
/**
 * Creates an empty collection to get it up and running
 */
public synchronized void create( boolean _errorOnExists ) throws IOException {
	setDirectory();
	
	if ( directory.listAll().length > 2 ) {
		if ( _errorOnExists ) {
			throw new IOException( "directory not empty; possible collection already present" );
		}else {
			if ( DirectoryReader.indexExists( directory ) ) {
				return;
			}// otherwise an index doesn't exist so allow the creation code to execute
		}
	}

	IndexWriterConfig iwc = new IndexWriterConfig( AnalyzerFactory.get(language) );
	iwc.setOpenMode( OpenMode.CREATE );
	
	indexwriter = new IndexWriter(directory, iwc);
	indexwriter.commit();
	indexwriter.close();
	indexwriter = null;
	
	// throw an openbd.create file in there so we know when it was created
	created	= System.currentTimeMillis();
	File touchFile	= new File( collectionpath, "openbd.created" );
	Writer	fw	= new FileWriter( touchFile );
	fw.close();
}
 
Example 17
private int getDocumentCount(Directory directory) throws IOException {
  if (DirectoryReader.indexExists(directory)) {
    DirectoryReader reader = DirectoryReader.open(directory);
    int maxDoc = reader.maxDoc();
    reader.close();
    return maxDoc;
  }
  return 0;
}
 
Example 18
Source Project: crate   File: Lucene.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean indexExists(final Directory directory) throws IOException {
    return DirectoryReader.indexExists(directory);
}
 
Example 19
public LuceneTranslationMemory(Directory directory, DocumentBuilder documentBuilder, QueryBuilder queryBuilder, Rescorer rescorer, AnalyzerFactory analyzerFactory, int minQuerySize) throws IOException {
    this.indexDirectory = directory;
    this.queryBuilder = queryBuilder;
    this.rescorer = rescorer;
    this.documentBuilder = documentBuilder;
    this.analyzerFactory = analyzerFactory;
    this.shortQueryAnalyzer = analyzerFactory.createShortQueryAnalyzer();
    this.longQueryAnalyzer = analyzerFactory.createLongQueryAnalyzer();
    this.minQuerySize = minQuerySize;

    // Index writer setup
    IndexWriterConfig indexConfig = new IndexWriterConfig(Version.LUCENE_4_10_4, new DelegatingAnalyzerWrapper(PER_FIELD_REUSE_STRATEGY) {
        @Override
        protected Analyzer getWrappedAnalyzer(String fieldName) {
            if (documentBuilder.isHashField(fieldName))
                return analyzerFactory.createHashAnalyzer();
            else
                return analyzerFactory.createContentAnalyzer();
        }
    });

    indexConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    indexConfig.setSimilarity(analyzerFactory.createSimilarity());

    this.indexWriter = new IndexWriter(this.indexDirectory, indexConfig);

    // Ensure index exists
    if (!DirectoryReader.indexExists(directory))
        this.indexWriter.commit();

    // Read channels status
    IndexSearcher searcher = this.getIndexSearcher();

    Query query = this.queryBuilder.getChannels(this.documentBuilder);
    TopDocs docs = searcher.search(query, 1);

    if (docs.scoreDocs.length > 0) {
        Document channelsDocument = searcher.doc(docs.scoreDocs[0].doc);
        this.channels = this.documentBuilder.asChannels(channelsDocument);
    } else {
        this.channels = new HashMap<>();
    }
}
 
Example 20
private DirectoryReader getIndexReader(RAMDirectory directory) throws IOException {
  if (!DirectoryReader.indexExists(directory)) {
    new IndexWriter(directory, _conf.clone()).close();
  }
  return DirectoryReader.open(directory);
}