Merge pull request #2999 from dhruba619/master
BAEL-1160 Introduction to Apache Lucene
This commit is contained in:
commit
d076823051
|
@ -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>
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue