Lucene is a full-text index and search engine written with Java, its the foundation of various search engine products like Solr and ElasticSearch. Sometime we may need to explore its API for learning it. This post create a starter Gradle project with Lucene support in Eclipse.

Create Gradle project in Eclipse

Select File -> New -> Project... and set Sample project as Java Quickstart

 lucene gradle eclipse create project

The standard project contains some sample files

We don't need these file, delete org.gralde package and create new package for example com.makble.lucenetest.

Add Lucene dependency

Add this line to sample gradle build script

First, the basic part of Lucene, if we need to try out other API of Lucene we will add it when need them, one the best practices is set the version as variable

 
ext.luceneVersion= "4.0.0"
 

And then add this line to dependencies closure block:

 
    compile "org.apache.lucene:lucene-core:${luceneVersion}"
 

Now add a class TestLucene.java, lets create a basic unit in Lucene , a term

 
package com.makble.lucenetest;
 
import org.apache.lucene.index.Term;
 
public class TestLucene {
    public static void main (String [] args ) {
        Term t = new Term( "field" , "TermText");
        System.out.println (t.field());
        System.out.println(t.text());
    }
}
 
 

A term is just a pair, the two elements are field and text.