Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Lucene 4 Cookbook
  • Toc
  • feedback
Lucene 4 Cookbook

Lucene 4 Cookbook

By : Edwood Ng, Vineeth Mohan
3.2 (5)
close
Lucene 4 Cookbook

Lucene 4 Cookbook

3.2 (5)
By: Edwood Ng, Vineeth Mohan

Overview of this book

This book is for software developers who are new to Lucene and who want to explore the more advanced topics to build a search engine. Knowledge of Java is necessary to follow the code samples. You will learn core concepts, best practices, and also advanced features, in order to build an effective search application.
Table of Contents (11 chapters)
close
10
Index

Creating fields

We have learned that indexing information in Lucene requires the creation of document objects. A Lucene document contains one or more field where each one represents a single data point about the document. A field can be a title, description, article ID, and so on. In this section, we will show you the basic structure and how to create a field.

A Lucene field has three attributes:

  • Name
  • Type
  • Value

Name and value are self-explanatory. You can think of a name as a column name in a table, and value as a value in one of the records where record itself is a document. Type determines how the field is treated. You can set FieldType to control whether to store value, to index it or even tokenize text. A Lucene field can hold the following:

  • String
  • Reader or preanalyzed TokenStream
  • Binary(byte[])
  • Numeric value

How to do it...

This code snippet shows you how to create a simple TextField:

  Document doc = new Document();
  String text = "Lucene is an Information Retrieval library written in Java.";
  doc.add(new TextField("fieldname", text, Field.Store.YES));

How It Works

In this scenario, we create a document object, initialize a text, and add a field by creating a TextField object. We also configure the field to store a value so it can be retrieved during a search.

A Lucene document is a collection of field objects. A field is the name of the value pairs, which you may add to the document. A field is created by simply instantiating one of the Field classes. Field can be inserted into a document via the add method.

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete