Lucene 4 Cookbook
By Edwood Ng and Vineeth Mohan
()
About this ebook
- Make the most of Lucene by understanding its philosophy and leveraging the data searching capability of your application
- Explore techniques to design scalable and succinct search applications
- Packed with clear, step-by-step recipes to walk you through the capabilities of Lucene
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.
Related to Lucene 4 Cookbook
Related ebooks
D Cookbook Rating: 0 out of 5 stars0 ratingsAdministrating Solr Rating: 0 out of 5 stars0 ratingsjQuery Mobile Web Development Essentials - Third Edition Rating: 0 out of 5 stars0 ratingsjQuery UI 1.7: The User Interface Library for jQuery Rating: 0 out of 5 stars0 ratingsMicrosoft .NET Framework 4.5 Quickstart Cookbook Rating: 0 out of 5 stars0 ratingsLearning Apache Mahout Classification Rating: 0 out of 5 stars0 ratingsSolr Cookbook - Third Edition Rating: 0 out of 5 stars0 ratingsMastering MeteorJS Application Development Rating: 4 out of 5 stars4/5HTML5 Graphing and Data Visualization Cookbook Rating: 0 out of 5 stars0 ratingsPhpStorm Cookbook Rating: 0 out of 5 stars0 ratingsApache Solr Search Patterns Rating: 0 out of 5 stars0 ratingsAkka Cookbook Rating: 2 out of 5 stars2/5Jasmine Cookbook Rating: 5 out of 5 stars5/5MySQL Admin Cookbook LITE: Configuration, Server Monitoring, Managing Users Rating: 4 out of 5 stars4/5Java Data Science Cookbook Rating: 0 out of 5 stars0 ratingsNatural Language Processing with Java and LingPipe Cookbook Rating: 0 out of 5 stars0 ratingsA Primer on Statistical Distributions Rating: 0 out of 5 stars0 ratingsScalatra in Action Rating: 0 out of 5 stars0 ratingsSQL and NoSQL Interview Questions: Your essential guide to acing SQL and NoSQL job interviews (English Edition) Rating: 0 out of 5 stars0 ratingsTika in Action Rating: 0 out of 5 stars0 ratingsSolr in Action Rating: 3 out of 5 stars3/5RxJava for Android Developers Rating: 0 out of 5 stars0 ratingsLearning Underscore.js Rating: 0 out of 5 stars0 ratingsSpark GraphX in Action Rating: 0 out of 5 stars0 ratingsStart Concurrent: An Introduction to Problem Solving in Java with a Focus on Concurrency, 2014 Rating: 0 out of 5 stars0 ratingsThe Best Javascript Rating: 0 out of 5 stars0 ratingsDevOps in Python: Infrastructure as Python Rating: 0 out of 5 stars0 ratingsFlex on Java Rating: 0 out of 5 stars0 ratingsMastering Apache Cassandra - Second Edition Rating: 0 out of 5 stars0 ratingsAngularJS in Action Rating: 0 out of 5 stars0 ratings
Databases For You
SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Practical Data Analysis Rating: 4 out of 5 stars4/5Excel 2021 Rating: 4 out of 5 stars4/5Access 2019 For Dummies Rating: 0 out of 5 stars0 ratingsLearn SQL in 24 Hours Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL Server: Tips and Tricks - 2 Rating: 4 out of 5 stars4/5CompTIA DataSys+ Study Guide: Exam DS0-001 Rating: 0 out of 5 stars0 ratingsJAVA for Beginner's Crash Course: Java for Beginners Guide to Program Java, jQuery, & Java Programming Rating: 4 out of 5 stars4/5Data Science Using Python and R Rating: 0 out of 5 stars0 ratingsSQL Clearly Explained Rating: 5 out of 5 stars5/5Business Intelligence Strategy and Big Data Analytics: A General Management Perspective Rating: 5 out of 5 stars5/5MATLAB Machine Learning Recipes: A Problem-Solution Approach Rating: 0 out of 5 stars0 ratingsGo in Action Rating: 5 out of 5 stars5/5Learn SQL Server Administration in a Month of Lunches Rating: 3 out of 5 stars3/5Codeless Data Structures and Algorithms: Learn DSA Without Writing a Single Line of Code Rating: 0 out of 5 stars0 ratingsData Governance: How to Design, Deploy and Sustain an Effective Data Governance Program Rating: 4 out of 5 stars4/5COBOL Basic Training Using VSAM, IMS and DB2 Rating: 5 out of 5 stars5/5Serverless Architectures on AWS, Second Edition Rating: 5 out of 5 stars5/5Data Science Strategy For Dummies Rating: 0 out of 5 stars0 ratingsPython Projects for Everyone Rating: 0 out of 5 stars0 ratingsBlockchain For Dummies Rating: 5 out of 5 stars5/5Data Stewardship: An Actionable Guide to Effective Data Management and Data Governance Rating: 4 out of 5 stars4/5Learning ArcGIS Geodatabases Rating: 5 out of 5 stars5/5SQL: Practical Guide for Developers Rating: 2 out of 5 stars2/5Relational Database Design and Implementation Rating: 5 out of 5 stars5/5Artificial Intelligence for Fashion: How AI is Revolutionizing the Fashion Industry Rating: 0 out of 5 stars0 ratingsThe Visual Imperative: Creating a Visual Culture of Data Discovery Rating: 4 out of 5 stars4/5Developing High Quality Data Models Rating: 0 out of 5 stars0 ratings
Reviews for Lucene 4 Cookbook
0 ratings0 reviews
Book preview
Lucene 4 Cookbook - Edwood Ng
Table of Contents
Lucene 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introducing Lucene
Introduction
How Lucene works
Why is Lucene so popular?
Some Lucene implementations
Installing Lucene
How to do it...
How it works…
Setting up a simple Java Lucene project
Getting ready
How to do it...
How it works...
Obtaining an IndexWriter
How to do it…
How it works…
Creating an analyzer
Getting ready
How to do it...
How it works…
Creating fields
How to do it...
How It Works
Creating and writing documents to an index
How to do it...
How it works…
Deleting documents
How to do it...
How it works…
Obtaining an IndexSearcher
How to do it...
How it works…
Creating queries with the Lucene QueryParser
How to do it...
How it works…
Performing a search
How to do it...
How it works…
Enumerating results
How to do it...
How it works…
2. Analyzing Your Text
Introduction
Obtaining a common analyzer
Getting ready
How to do it...
How it works...
There's more…
Obtaining a TokenStream
Getting ready
How to do it...
How it works…
Obtaining TokenAttribute values
Getting ready
How to do it…
How it works…
Using PositionIncrementAttribute
Getting ready
How to do it...
How it works…
Using PerFieldAnalyzerWrapper
Getting ready
How to do it…
How it works…
Defining custom TokenFilters
How to do it…
How it works…
Defining custom analyzers
How to do it…
How it works…
Defining custom tokenizers
How to do it…
How it works…
Defining custom attributes
How to do it…
How it works…
3. Indexing Your Data
Introduction
Obtaining an IndexWriter
How to do it...
How it works...
Creating a StringField
How to do it...
How it works...
Creating a TextField
How to do it...
How it works...
Creating a numeric field
How to do it...
How it works...
Creating a DocValue Field
How to do it...
How it works...
Transactional commits and index versioning
How to do it...
How it works...
Reusing field and document objects per thread
How to do It...
How it works...
Delving into field norms
How to do it...
How it works...
Changing similarity implementation used during indexing
Getting ready
How to do it…
4. Searching Your Indexes
Introduction
Obtaining IndexReaders
How to do it...
How it works...
Un-inverting single-valued fields in memory with FieldCache
How to do it...
How it works...
TermVectors
How to do it...
How it works...
IndexSearcher
How to do it...
How it works...
Constructing queries
How to do it...
How it works...
Specifying sort logic
How to do it...
How it works...
Forming a search result
How to do it...
How it works...
Pagination
How to do it...
How it works...
Using Collectors
How to do it...
How it works...
Sorting with custom FieldComparator
How to do it...
How it works...
5. Near Real-time Searching
Introduction
Using the DirectoryReader to open index in Near Real-Time
How to do it...
How it works...
Using the SearcherManager to refresh IndexSearcher
How to do it...
How it works...
Generational indexing with TrackingIndexWriter
How to do it…
How it works...
Maintaining search sessions with SearcherLifetimeManager
How to do it…
How it works…
Performance tuning: latency and throughput
How to do it…
How it works…
6. Querying and Filtering Data
Introduction
Performing advanced filtering
How to do it...
How it works…
Creating a custom filter
How to do it...
Searching with QueryParser
How to do it..
Wildcard search
Term range search
Autogenerated phrase query
Date resolution
Default operator
Enable position increments
Fuzzy query
Lowercase expanded term
Phrase slop
TermQuery and TermRangeQuery
How to do it..
BooleanQuery
How to do it..
How it works…
PrefixQuery and WildcardQuery
How to do it...
How it works…
PhraseQuery and MultiPhraseQuery
How to do it...
How it works…
FuzzyQuery
How to do it...
How it works…
NumericRangeQuery
How to do it…
How it works…
DisjunctionMaxQuery
How to do it…
How it works…
RegexpQuery
How to do it…
SpanQuery
How to do it...
How it works…
CustomScoreQuery
How to do it…
How it works…
7. Flexible Scoring
Introduction
Overriding similarity
How to do it…
How it works…
There's more…
The BM25 model
The language model
The divergence from randomness model
The information-based Model
Implementing the BM25 model
How to do It…
How it works…
Implementing the language model
How to do it…
Implementing the divergence from randomness model
How to do It…
How it works…
Implementing the information-based model
How to do It…
How it works…
8. Introducing Elasticsearch
Introduction
Getting Elasticsearch
Getting ready
How to do it...
How it works...
There's more…
Creating a new index
How to do it…
How it works…
Predefine field mappings
How to do it...
How it works....
Adding a document
How to do it...
How it works...
Deleting a document
How to do it…
How it works…
Updating a document
How to do it…
How it works…
Performing bulk indexing
How to do it…
How it works…
Searching the index
How to do it...
How it works...
There's more…
Scaling Elasticsearch
How to do it…
How it works…
There's more…
9. Extending Lucene with Modules
Introduction
Exploring spatial search
Getting ready…
How to do it…
How it works…
There's more…
Implementing joins
Getting ready…
How to do it…
Performing faceting
Getting ready…
How to do it…
How it works…
Implementing grouping
Getting ready…
How to do it…
Employing autosuggest
Getting ready…
How to do it…
Implementing highlighting
Getting ready…
How to do it…
How it works…
Index
Lucene 4 Cookbook
Lucene 4 Cookbook
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2015
Production reference: 1220615
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-228-5
www.packtpub.com
Credits
Authors
Edwood Ng
Vineeth Mohan
Reviewers
Anders Lybecker
Sujit Pal
Commissioning Editor
James K
Acquisition Editor
Usha Iyer
Content Development Editor
Vaibhav Pawar
Technical Editor
Naveenkumar Jain
Copy Editors
Trishya Hajare
Aditya Nair
Project Coordinator
Kranti Berde
Proofreader
Safis Editing
Indexer
Priya Sane
Graphics
Sheetal Aute
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
About the Authors
Edwood Ng is a technologist with over a decade of experience in building scalable solutions from proprietary implementations to client-facing web-based applications. Currently, he's the director of DevOps at Wellframe, leading infrastructure and DevOps operations.
His background in search engine began at Endeca Technologies in 2004, where he was a technical consultant helping numerous clients to architect and implement faceted search solutions. After Endeca, he drew on his knowledge and began designing and building Lucene-based solutions. His first Lucene implementation that went to production was the search engine behind http://UpDown.com. From there on, he continued to create search applications using Lucene extensively to deliver robust and scalable systems for his clients. Edwood is a supporter of an open source software. He has also contributed to the plugin sfI18NGettextPluralPlugin to the Symphony project.
Vineeth Mohan is a data scientist whose major expertise lies in search analytics and data science. His career started at Yahoo! India, working with a team facilitated by a real-time feed processing platform based on Hadooop across all verticals in Yahoo!. He was also in the team that migrated Right Media technologies to Yahoo! stack. He then moved to Algotree, where he worked across many data science projects such as technical pattern detection in TimeSeries. Later, he designed a real-time news analysis platform based on natural language processing and machine learning approaches. He was also the architect of the search solution for this. Vineeth is passionate about Big Data and data science and has extensive knowledge and experience in Elasticsearch/Solr/Lucene and algorithm design.
About the Reviewers
Sujit Pal currently works in information retrieval and natural language processing in the healthcare industry. He uses a combination of his company's manually curated medical taxonomy and a variety of open-source tools such as Lucene, Hadoop, Mahout, Akka, OpenNLP, and so on, to build the company's semantic search platform. His main areas of interest are search, distributed processing, NLP, and machine learning. He believes in lifelong learning and blogs about his experiences at http://sujitpal.blogspot.com.
He works for Healthline Networks, Inc., which is a start-up in the consumer healthcare space.
It has been a pleasure to review this book. Special thanks to the author and publishing team for making this process so enjoyable.
Anders Lybecker is a technical evangelist for Microsoft and has more than 10 years of experience as a solutions architect, working with multi-billion dollar revenue systems within the travel, finance, and banking industries. A special interest in search technologies and especially Lucene has led to more than a dozen implementations with Lucene, Solr, and ElasticSearch.
Anders has presented at numerous conferences and holds a degree in software engineering, specializing in software development. He blogs about software development including search technologies at http://www.lybecker.com/blog/.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Preface
Apache Lucene 4 is the backbone of many search engine implementations, including big names such as LinkedIn, Twitter, and IBM. It also serves as a foundation for other open source projects such as Solr and Elasticsearch. We will dive into all the features that Lucene offers and show you easy-to-understand recipes to get you started using Lucene.
What this book covers
Chapter 1, Introducing Lucene, introduces you to the core components of Lucene and gives you the know-how to set up Lucene on your own.
Chapter 2, Analyzing Your Text, explores a key feature of Lucene called Analyzers. We will show you how text analyzing works and how to customize it to suit your needs.
Chapter 3, Indexing Your Data, looks into the data injection process in Lucene and reviews the core concepts, such as norms and similarity.
Chapter 4, Searching Your Indexes, will cover the core search components such as FieldCache and TermVectors, and will give you all the knowledge you need to build an effective search engine.
Chapter 5, Near Real-time Searching, will show you how near real-time search is achieved via various methods and their trade-offs, so you can make educated decisions when designing your search application.
Chapter 6, Querying and Filtering Data, gives you a glimpse of the various querying and filtering features that have been proven to build successful search engines.
Chapter 7, Flexible Scoring, takes a technical dive into the mechanics of scoring, how scores are determined, how it affects ranking positions and what you can do to customize it.
Chapter 8, Introducing Elasticsearch, gives an introduction to a Lucene-based open source search solution that gives you everything you need to build a search application in no time.
Chapter 9, Extending Lucene with Modules, explores the additional features, such as spatial search and faceting, that extend Lucene functionalities beyond just text search.
What you need for this book
Lucene is built in Java so all you need is a Java-supported operating system with JDK 1.6 installed. It's preferable to use Oracle JDK; more information can be found here: http://www.oracle.com/technetwork/java/javase/downloads/index.html. Lucene's project page can be found at http://lucene.apache.org/.
Lucene is a library, so there are no installation steps other than copying the library files. One way to include Lucene into your project is through a Maven repository. Here is a sample Maven dependency description to include in the lucene-core library.
Who this book is for
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.
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it…, How it works…, There's more…, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
Getting ready
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
How to do it…
This section contains the steps required to follow the recipe.
How it works…
This section usually consists of a detailed explanation of what happened in the previous section.
There's more…
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: we should let Lucene decide which implementation to use by calling FSDirectory.open (File path).
A block of code is set as follows:
indexWriter.deleteDocuments(new Term(id
, 1
));"));
indexWriter.close();
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
Reader reader = new StringReader(Text to be passed
);
Analyzer analyzer = new SimpleAnalyzer();
TokenStream tokenStream = analyzer.tokenStream(myField
, reader);
Any command-line input or output is written as follows:
curl -XDELETE 'http://localhost:9200/news/article/1'
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: The Download button will take you to all available Apache mirrors where you can download Lucene.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <feedback@packtpub.com>, and mention the book's title in the subject of your