Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaciasluque committed Mar 11, 2023
2 parents 6864624 + 22f73b8 commit 78705a9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 55 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "maven" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
17 changes: 17 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Java CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: "17"
distribution: "temurin"
- name: Build with Maven
run: mvn --batch-mode --update-snapshots clean package
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Created by .ignore support plugin (hsz.mobi)
.vscode

### Maven template
target/
pom.xml.tag
Expand All @@ -24,4 +25,4 @@ buildNumber.properties
.idea/vcs.xml
HelloLucene.iml
src/main/main.iml
.idea/dictionaries/
.idea/dictionaries/
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
hello-lucene
============

Mavenized version of Kelvin Tan's example (http://www.lucenetutorial.com/lucene-in-5-minutes.html)
Mavenized version of Kelvin Tan's example <http://www.lucenetutorial.com/lucene-in-5-minutes.html>

Use the following command to run the program:
```
$ mvn package -q

```bash
mvn --quiet clean package
```
93 changes: 54 additions & 39 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,64 +1,79 @@
<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>
<groupId>me.juanmacias</groupId>
<artifactId>HelloLucene</artifactId>
<version>0.0.2-SNAPSHOT</version>
<name>Hello Lucene</name>
<description>Mavenized version of Kelvin Tan's example</description>
<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>
<groupId>me.juanmacias</groupId>
<artifactId>HelloLucene</artifactId>
<version>0.0.3-SNAPSHOT</version>
<name>Hello Lucene</name>
<description>Mavenized version of Kelvin Tan's example</description>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>ex</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>me.juanmacias.HelloLucene</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>ex</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>me.juanmacias.HelloLucene</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<excludes>
<exclude>org.apache.commons:commons-collections4</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.1.0</version>
<version>9.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>7.1.0</version>
<version>9.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-test-framework</artifactId>
<version>7.1.0</version>
<version>9.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>7.1.0</version>
<version>8.11.2</version>
</dependency>
<dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.1.0</version>
<version>9.5.0</version>
</dependency>
</dependencies>

Expand Down
19 changes: 8 additions & 11 deletions src/main/java/me/juanmacias/HelloLucene.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
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.TopScoreDocCollector;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.ByteBuffersDirectory;

import java.io.IOException;

public class HelloLucene {

public static void main(String[] args) throws IOException {
// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
// The same analyzer should be used for indexing and searching
StandardAnalyzer analyzer = new StandardAnalyzer();

// 1. create the index
Directory index = new RAMDirectory();

Directory index = new ByteBuffersDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);

IndexWriter w = new IndexWriter(index, config);
Expand All @@ -45,8 +44,7 @@ public static void main(String[] args) throws IOException {
// when no field is explicitly specified in the query.
Query query = null;
try {
String[] fields = {"title"};
query = new MultiFieldQueryParser(fields, analyzer).parse(queryString);
query = new QueryParser("title", analyzer).parse(queryString);
} catch (org.apache.lucene.queryparser.classic.ParseException e) {
e.printStackTrace();
}
Expand All @@ -55,9 +53,8 @@ public static void main(String[] args) throws IOException {
int hitsPerPage = 10;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
searcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
TopDocs docs = searcher.search(query, hitsPerPage);
ScoreDoc[] hits = docs.scoreDocs;

// 4. display results
System.out.println("Found " + hits.length + " hits.");
Expand Down

0 comments on commit 78705a9

Please sign in to comment.