Last active
May 6, 2016 17:01
-
-
Save lipusz/97bdde801ef91af1344eb822a11555a6 to your computer and use it in GitHub Desktop.
Sample (Eclipse-based) project to play with Lucene. Index, search.
This file contains hidden or 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/lucene-analyzers-common-4.3.1.jar"/> | |
<classpathentry kind="lib" path="lib/lucene-core-4.3.1.jar" sourcepath="/lucene-solr"/> | |
<classpathentry kind="lib" path="lib/lucene-queryparser-4.3.1.jar"/> | |
<classpathentry kind="output" path="bin"/> | |
</classpath> |
This file contains hidden or 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"?> | |
<projectDescription> | |
<name>test-lucene</name> | |
<comment></comment> | |
<projects> | |
</projects> | |
<buildSpec> | |
<buildCommand> | |
<name>org.eclipse.jdt.core.javabuilder</name> | |
<arguments> | |
</arguments> | |
</buildCommand> | |
</buildSpec> | |
<natures> | |
<nature>org.eclipse.jdt.core.javanature</nature> | |
</natures> | |
</projectDescription> |
This file contains hidden or 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 java.util.ArrayList; | |
import java.util.List; | |
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.TextField; | |
import org.apache.lucene.index.DirectoryReader; | |
import org.apache.lucene.index.Fields; | |
import org.apache.lucene.index.IndexReader; | |
import org.apache.lucene.index.IndexWriter; | |
import org.apache.lucene.index.IndexWriterConfig; | |
import org.apache.lucene.index.MultiFields; | |
import org.apache.lucene.index.Terms; | |
import org.apache.lucene.index.TermsEnum; | |
import org.apache.lucene.queryparser.classic.QueryParser; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.search.TopDocs; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.FSDirectory; | |
import org.apache.lucene.util.BytesRef; | |
import org.apache.lucene.util.Version; | |
/** | |
* Requires lucene-4.3.1 or later. | |
*<p> | |
* Dependencies:<br> | |
*- lucene-analyzers-common<br> | |
*- lucene-core<br> | |
*- lucene-queryparser<br> | |
*<p> | |
* @author Tibor Lipusz | |
*<br> | |
* Lead Software Engineer, Platform TS, EMEA | |
*<br> | |
* Search & Security SME, Subscription Services & Release, Global | |
*<br> | |
* LSV Project Lead | |
*/ | |
public class TestLuceneV431 { | |
public static void main(String[] args) throws Exception { | |
try { | |
initIndex(); | |
// -- Index docs -- | |
Analyzer analyzer = new StandardAnalyzer(_VERSION); | |
String fieldName = "test"; | |
Document document = new Document(); | |
Field field = | |
new TextField( | |
fieldName, "null", Field.Store.YES); | |
document.add(field); | |
addDocument(document, analyzer); | |
// Echo terms | |
System.out.println( | |
"Indexed terms <" + fieldName + ">: " + | |
getIndexedTerms(fieldName)); | |
// Search | |
String keyword = "null"; | |
int searchCount = search(fieldName, keyword, analyzer); | |
System.out.println( | |
"Number of results searching for: " + keyword + " = " + | |
searchCount); | |
} | |
finally { | |
closeIndex(true); | |
} | |
} | |
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(boolean deleteAll) throws Exception { | |
if (_indexWriter == null) { | |
return; | |
} | |
if (deleteAll) { | |
_indexWriter.deleteAll(); | |
} | |
_indexWriter.close(); | |
} | |
protected static void initIndex() throws Exception { | |
_indexDir = FSDirectory.open(new File("index")); | |
IndexWriterConfig config = | |
new IndexWriterConfig( | |
Version.LUCENE_43, new StandardAnalyzer(_VERSION)); | |
_indexWriter = new IndexWriter(_indexDir, config); | |
} | |
protected static List<String> getIndexedTerms( | |
String lookUpField) | |
throws Exception { | |
checkIndex(); | |
IndexReader indexReader = DirectoryReader.open(_indexDir); | |
Fields fields = MultiFields.getFields(indexReader); | |
List<String> termStrings = new ArrayList<String>(); | |
for (String field : fields) { | |
if (field.equals(lookUpField)) { | |
Terms terms = fields.terms(field); | |
TermsEnum termsEnum = terms.iterator(null); | |
while (true) { | |
BytesRef byteRef = termsEnum.next(); | |
if (byteRef == null) { | |
break; | |
} | |
termStrings.add(byteRef.utf8ToString()); | |
} | |
} | |
} | |
return termStrings; | |
} | |
protected static int search( | |
String field, String keyword, Analyzer analyzer) | |
throws Exception { | |
checkIndex(); | |
IndexReader indexReader = DirectoryReader.open(_indexDir); | |
IndexSearcher indexSearcher = new IndexSearcher(indexReader); | |
QueryParser queryParser = new QueryParser(_VERSION, field, analyzer); | |
Query query = queryParser.parse(keyword); | |
// Print query | |
System.out.println("Query: " + query); | |
TopDocs topDocs = indexSearcher.search(query, 100); | |
return topDocs.totalHits; | |
} | |
private static Directory _indexDir; | |
private static IndexWriter _indexWriter; | |
private static final Version _VERSION = Version.LUCENE_43; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment