Merge pull request #2999 from dhruba619/master

BAEL-1160 Introduction to Apache Lucene
This commit is contained in:
Carsten Gräf 2017-11-14 15:42:33 +01:00 committed by GitHub
commit d076823051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 139 additions and 0 deletions

37
lucene/pom.xml Normal file
View File

@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>lucene</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lucene</name>
<description>An Apache Lucene demo application</description>
<dependencies>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,78 @@
package com.baeldung.lucene;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
public class InMemoryLuceneIndex {
private Directory memoryIndex;
private StandardAnalyzer analyzer;
public InMemoryLuceneIndex(Directory memoryIndex, StandardAnalyzer analyzer) {
super();
this.memoryIndex = memoryIndex;
this.analyzer = analyzer;
}
/**
*
* @param title
* @param body
*/
public void indexDocument(String title, String body) {
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
try {
IndexWriter writter = new IndexWriter(memoryIndex, indexWriterConfig);
Document document = new Document();
document.add(new TextField("title", title, Field.Store.YES));
document.add(new TextField("body", body, Field.Store.YES));
writter.addDocument(document);
writter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public List<Document> searchIndex(String inField, String queryString) {
try {
Query query = new QueryParser(inField, analyzer).parse(queryString);
IndexReader indexReader = DirectoryReader.open(memoryIndex);
IndexSearcher searcher = new IndexSearcher(indexReader);
TopDocs topDocs = searcher.search(query, 10);
List<Document> documents = new ArrayList<>();
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
documents.add(searcher.doc(scoreDoc.doc));
}
return documents;
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.lucene;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Assert;
import org.junit.Test;
public class LuceneInMemorySearchTest {
@Test
public void givenSearchQueryWhenFetchedDocumentThenCorrect() {
InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
inMemoryLuceneIndex.indexDocument("Hello world", "Some hello world ");
List<Document> documents = inMemoryLuceneIndex.searchIndex("body", "world");
Assert.assertEquals("Hello world", documents.get(0).get("title"));
}
}

View File

@ -261,6 +261,7 @@
<module>saas</module>
<module>deeplearning4j</module>
<module>spring-boot-admin</module>
<module>lucene</module>
</modules>
<dependencies>