Skip to content

Instantly share code, notes, and snippets.

@lipusz
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lipusz/011159b91a5c5324a941 to your computer and use it in GitHub Desktop.
Save lipusz/011159b91a5c5324a941 to your computer and use it in GitHub Desktop.
Test Lucene deleteAll() v3.5.0
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/commons-io.jar"/>
<classpathentry kind="lib" path="lib/lucene-core-3.5.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.test;
import java.io.File;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
/**
* Uses lucene-3.5.0.
*<p>
* Dependencies: See the .classpath file
*<p>
* @author Tibor Lipusz, Senior Software Engineer @ TS | Liferay, Inc.
*/
public class TestLuceneV350 {
public static void main(String[] args) throws Exception {
try {
initIndex();
// -- Index docs --
Analyzer analyzer = new StandardAnalyzer(_VERSION);
Document document = new Document();
Field field = new Field(
"field", "value", Field.Store.YES, Index.ANALYZED);
document.add(field);
addDocument(document, analyzer);
document = new Document();
field = new Field(
"field", "value2", Field.Store.YES, Index.ANALYZED);
document.add(field);
addDocument(document, analyzer);
}
finally {
closeIndex();
}
}
protected static void addDocument(
Document document, Analyzer analyzer)
throws Exception {
checkIndex();
_indexWriter.addDocument(document, analyzer);
_indexWriter.commit();
}
protected static void checkIndex() throws Exception {
if (_indexWriter == null) {
initIndex();
}
}
protected static void closeIndex() throws Exception {
if (_indexWriter == null) {
return;
}
_indexWriter.deleteAll();
_indexWriter.close();
}
protected static void initIndex() throws Exception {
_indexDirectory = FSDirectory.open(new File("index"));
IndexWriterConfig config =
new IndexWriterConfig(
_VERSION, new StandardAnalyzer(_VERSION));
_indexWriter = new IndexWriter(_indexDirectory, config);
}
private static Directory _indexDirectory;
private static IndexWriter _indexWriter;
private static final Version _VERSION = Version.LUCENE_35;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment