Last active
August 29, 2015 14:22
-
-
Save lipusz/011159b91a5c5324a941 to your computer and use it in GitHub Desktop.
Test Lucene deleteAll() v3.5.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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