Skip to content

Commit 9341d6f

Browse files
committed
BAEL-1160 Introduction to Apache Lucene
1 parent 996bee2 commit 9341d6f

4 files changed

Lines changed: 139 additions & 0 deletions

File tree

‎lucene/pom.xml‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>parent-modules</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>lucene</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
<name>lucene</name>
12+
<description>An Apache Lucene demo application</description>
13+
14+
<dependencies>
15+
16+
<dependency>
17+
<groupId>org.apache.lucene</groupId>
18+
<artifactId>lucene-core</artifactId>
19+
<version>7.1.0</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.apache.lucene</groupId>
24+
<artifactId>lucene-queryparser</artifactId>
25+
<version>7.1.0</version>
26+
</dependency>
27+
<!-- https://mvnrepository.com/artifact/junit/junit -->
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.12</version>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
</dependencies>
36+
37+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.lucene;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import org.apache.lucene.analysis.standard.StandardAnalyzer;
8+
import org.apache.lucene.document.Document;
9+
import org.apache.lucene.document.Field;
10+
import org.apache.lucene.document.TextField;
11+
import org.apache.lucene.index.DirectoryReader;
12+
import org.apache.lucene.index.IndexReader;
13+
import org.apache.lucene.index.IndexWriter;
14+
import org.apache.lucene.index.IndexWriterConfig;
15+
import org.apache.lucene.queryparser.classic.ParseException;
16+
import org.apache.lucene.queryparser.classic.QueryParser;
17+
import org.apache.lucene.search.IndexSearcher;
18+
import org.apache.lucene.search.Query;
19+
import org.apache.lucene.search.ScoreDoc;
20+
import org.apache.lucene.search.TopDocs;
21+
import org.apache.lucene.store.Directory;
22+
23+
public class InMemoryLuceneIndex {
24+
25+
private Directory memoryIndex;
26+
private StandardAnalyzer analyzer;
27+
28+
public InMemoryLuceneIndex(Directory memoryIndex, StandardAnalyzer analyzer) {
29+
super();
30+
this.memoryIndex = memoryIndex;
31+
this.analyzer = analyzer;
32+
}
33+
34+
/**
35+
*
36+
* @param title
37+
* @param body
38+
*/
39+
public void indexDocument(String title, String body) {
40+
41+
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
42+
try {
43+
IndexWriter writter = new IndexWriter(memoryIndex, indexWriterConfig);
44+
Document document = new Document();
45+
46+
document.add(new TextField("title", title, Field.Store.YES));
47+
document.add(new TextField("body", body, Field.Store.YES));
48+
49+
writter.addDocument(document);
50+
writter.close();
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
public List<Document> searchIndex(String inField, String queryString) {
57+
try {
58+
Query query = new QueryParser(inField, analyzer).parse(queryString);
59+
60+
IndexReader indexReader = DirectoryReader.open(memoryIndex);
61+
IndexSearcher searcher = new IndexSearcher(indexReader);
62+
TopDocs topDocs = searcher.search(query, 10);
63+
List<Document> documents = new ArrayList<>();
64+
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
65+
documents.add(searcher.doc(scoreDoc.doc));
66+
}
67+
68+
return documents;
69+
} catch (IOException | ParseException e) {
70+
e.printStackTrace();
71+
}
72+
return null;
73+
74+
}
75+
76+
}
77+
78+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.lucene;
2+
3+
import java.util.List;
4+
5+
import org.apache.lucene.analysis.standard.StandardAnalyzer;
6+
import org.apache.lucene.document.Document;
7+
import org.apache.lucene.store.RAMDirectory;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
public class LuceneInMemorySearchTest {
12+
13+
@Test
14+
public void givenSearchQueryWhenFetchedDocumentThenCorrect() {
15+
InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
16+
inMemoryLuceneIndex.indexDocument("Hello world", "Some hello world ");
17+
18+
List<Document> documents = inMemoryLuceneIndex.searchIndex("body", "world");
19+
20+
Assert.assertEquals("Hello world", documents.get(0).get("title"));
21+
}
22+
23+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
<module>saas</module>
261261
<module>deeplearning4j</module>
262262
<module>spring-boot-admin</module>
263+
<module>lucene</module>
263264
</modules>
264265

265266
<dependencies>

0 commit comments

Comments
 (0)