48
Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Embed Size (px)

Citation preview

Page 1: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Lucene Boot Camp I

Grant IngersollLucid Imagination

Nov. 3, 2008 New Orleans, LA

Page 2: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Intro

• My Background• Goals for Tutorial

– Understand Lucene core capabilities

– Real examples, real code, real data

• Ask Questions!!!!!

Page 3: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Schedule• Day I

– Concepts– Indexing– Searching– Analysis– Lucene contrib: highlighter, spell checking, etc.

• Day II– In-depth Indexing/Searching

• Performance, Internals

– Terms and Term Vectors– Class Project– Q & A

Page 4: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

4

Resources

• Slides at – http://www.lucenebootcamp.com/boot-camp-slides/

• Lucene Java– http://lucene.apache.org/java– http://lucene.apache.org/java/2_4_0/

– http://lucene.apache.org/java/2_4_0/api/index.html

• Luke:– http://www.getopt.org/luke

Page 5: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

5

What is Search?

• Given a user’s information need (query), find documents relevant to the need– Very Subjective!

• Information Retrieval– Interdisciplinary– Comp. Sci, Math/Statistics, Library Sci., Linguistics, AI…

Page 6: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

6

Search Use Cases• Web– Google, Y!, etc.

• Enterprise– Intranet, Content Repositories, email, etc.

• eCommerce/DB/CMS– Online Stores, websites, etc.

• Other– QA, Federated

• Yours? Why do you need Search?

Page 7: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

7

Your Content And You

• Only you know your content!– Key Features

•Title, body, price, margin, etc.

– Important Terms– Synonyms/Jargon– Structures (tables, lists, etc.)– Importance– Priorities

Page 8: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Search Basics

• Many different Models:– Boolean,

Probabilistic, Inference, Neural Net, and:

• Modified Vector Space Model (VSM)– Boolean + VSM– TF-IDF– The words in the

document and the query each define a Vector in an n-dimensional space

– Sim(q1, d1) = cos Θ

q1

d1

Θ

dj= <w1,j,w2,j,…,wn,j>q= <w1,q,w2,q,…wn,q>w = weight assigned to term

Page 9: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

9

Inverted Index

From “Taming Text”

Page 10: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

10

Lucene Background

• Created by Doug Cutting in 1999• Donated to ASF in 2001• Morphed into a Top Level Project (TLP) with many sub projects– Java (flagship) a.k.a. “Lucene”– Solr, Nutch, Mahout, Tika, several Lucene ports

• From here on out, Lucene refers to “Lucene Java”

Page 11: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Lucene is…

• NOT a crawler– See Nutch

• NOT an application– See PoweredBy on the Wiki

• NOT a library for doing Google PageRank or other link analysis algorithms– See Nutch

• A library for enabling text based search

Page 12: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

12

A Few Words about Solr• HTTP-based Search Server• XML Configuration• XML, JSON, Ruby, PHP, Java support

• Many, many nice features that Lucene users need– Faceting, spell checking, highlighting

– Caching, Replication, Distributed

• http://lucene.apache.org/solr

Page 13: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Indexing

• Process of preparing and adding text to Lucene, which stores it in an inverted index

• Key Point: Lucene only indexes Strings– What does this mean?

•Lucene doesn’t care about XML, Word, PDF, etc.– There are many good open source extractors available

•It’s our job to convert whatever file format we have into something Lucene can use

Page 14: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Indexing Classes

• Analyzer– Creates tokens using a Tokenizer and filters them through zero or more TokenFilters

• IndexWriter– Responsible for converting text into internal Lucene format

• Directory– Where the Index is stored– RAMDirectory, FSDirectory, others

Page 15: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Indexing Classes

• Document– A collection of Fields– Can be boosted

• Field– Free text, keywords, dates, etc.– Defines attributes for storing, indexing

– Can be boosted– Field Constructors and parameters

•Open up Fieldable and Field in IDE

Page 16: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

How to Index

• Create IndexWriter• For each input

– Create a Document– Add Fields to the Document– Add the Document to the IndexWriter

• Close the IndexWriter• Optimize (optional)

Page 17: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Indexing in a Nutshell• For each Document

– For each Field to be tokenized• Create the tokens using the specified Tokenizer– Tokens consist of a String, position, type and offset information

• Pass the tokens through the chained TokenFilters where they can be changed or removed

• Add the end result to the inverted index

• Position information can be altered– Useful when removing words or to prevent phrases from matching

Page 18: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Task 1.a• From the Boot Camp Files, use the basic.ReutersIndexer skeleton to start

• Index the small Reuters Collection using the IndexWriter, a Directory and StandardAnalyzer– Boost every 10 documents by 3

• Questions to Answer:– What Fields should I define?– What attributes should each Field have?– Pick a field to boost and give a reason why you think it should be boosted

• ~30 minutes

Page 19: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Use Luke

Page 20: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

5 minute Break

Page 21: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Searching• Parse user query• Lookup matching Documents• Score Documents• Return ranked list

Page 22: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

22

Key Classes:

– Searcher– Provides methods for searching

– Take a moment to look at the Searcher class declaration

• IndexSearcher, MultiSearcher, ParallelMultiSearcher

– IndexReader– Loads a snapshot of the index into memory for

searching

– More tomorrow

– TopDocs - The search results

– QueryParser– http://lucene.apache.org/java/docs/

queryparsersyntax.html

– Query– Logical representation of program’s information need

Page 23: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Query Parsing

• Basic syntax:title:hockey +(body:stanley AND body:cup)

• OR/AND must be uppercase• Default operator is OR (can be changed)

• Supports fairly advanced syntax, see the website– http://lucene.apache.org/java/docs/queryparsersyntax.html

• Doesn’t always play nice, so beware– Many applications construct queries programmatically or restrict syntax

Page 24: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

24

How to Search

• Create/Get an IndexSearcher• Create a Query

– Use a QueryParser– Construct it programmatically

• Display the results from the TopDocs– Retrieve Field values from Document

• More tomorrow on search lifecyle

Page 25: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Task 1.b• Using the ReutersIndexerTest.java skeleton in the boot camp files– Search your newly created index using queries you develop

• Questions:– What is the default field for the QueryParser?

– What Analyzer to use?

• ~20 minutes

Page 26: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Task 1 Results• Scores across queries are NOT comparable– They may not even be comparable for the same query over time (if the index changes)

• Performance– Caching– Warming– More Tomorrow

Page 27: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Lunch

1-2:30

Page 28: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

28

Discussion/Questions

• So far, we’ve seen the basics of search and indexing

• Next going to look into Analysis and Contrib modules

Page 29: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Analysis• Analysis is the process of creating Tokens to be indexed

• Analysis is usually done to improve results overall, but it comes with a price

• Lucene comes with many different Analyzers, Tokenizers and TokenFilters, each with their own goals

• StandardAnalyzer is included with the core JAR and does a good job for most English and Latin-based tasks

• Often times you want the same content analyzed in different ways

• Consider a catch-all Field in addition to other Fields

Page 30: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

30

Solr’s Analysis tool

• If you use nothing else from Solr, the Admin analysis tool can really help you understand analysis

• Download Solr and unpack it• cd apache-solr-1.3.0/example• java -jar start.jar• http://localhost:8983/solr/admin/analysis.jsp

Page 31: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

31

Analyzers

• StandardAnalyzer, WhitespaceAnalyzer, SimpleAnalyzer

• Contrib/analysis– Suite of Analyzers for many common situations•Languages•n-grams•Payloads

• Contrib/snowball

Page 32: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Tokenization

• Split words into Tokens to be processed

• Tokenization is fairly straightforward for most languages that use a space for word segmentation– More difficult for some East Asian languages

– See the CJK Analyzer

Page 33: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Modifying Tokens

• TokenFilters are used to alter the token stream to be indexed

• Common tasks:– Remove stopwords– Lower case– Stem/Normalize -> Wi-Fi -> Wi Fi– Add Synonyms

• StandardAnalyzer does things that you may not want

Page 34: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

34

Payloads

• Associate an arbitrary byte array with a term in the index

• Uses– Part of Speech– Font weight– URL

• Currently can search using the BoostingTermQuery

Page 35: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

35

n-grams

• Combine units of content together into a single token

• Character– 2-grams for the word “Lucene”:

• Lu,uc, ce, en, ne

– Can make search possible when data is noisy or hard to tokenize

• Word (“shingles” in Lucene parlance)– Pseudo Phrases

Page 36: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Custom Analyzers

• Problem: none of the Analyzers cover my problem

• Solution: write your own Analyzer

• Better solution: write a configurable Analyzer so you only need one Analyzer that you can easily change for your projects– See Solr

Page 37: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

37

Analysis APIs

• Have a look at the TokenStream and Token APIs

• Tokens and TokenStreams may be reused– Helps reduce allocations and speeds up indexing

– Not all Analysis can take advantage: caching

– Analyzer.reusableTokenStream()

– TokenStream.next(Token)

Page 38: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Special Cases

• Dates and numbers need special treatment to be searchable– o.a.l.document.DateTools– org.apache.solr.util.NumberUtils

• Altering Position Information– Increase Position Gap between sentences to prevent phrases from crossing sentence boundaries

– Index synonyms at the same position so query can match regardless of synonym used

Page 39: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Task 2• Take 15-20 minutes and write an Analyzer/Tokenizer/TokenFilter and Unit Test– Examine results in Luke– Run some searches

• Ideas:– Combine existing Tokenizers and TokenFilters

– Normalize abbreviations– Add payloads– Filter out all words beginning with the letter A

– Identify/Mark sentences

Page 40: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

40

Discussion

• What did you implement?• What issues do you face with your content?

• To Stem or not to Stem?• Stopwords: good or bad?• Tradeoffs of different techniques

Page 41: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Lucene Contributions

• Many people have generously contributed code to help solve common problems

• These are in contrib directory of the source

• Popular:– Analyzers– Highlighter– Queries and MoreLikeThis– Snowball Stemmers– Spellchecker

Page 42: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

42

Highlighter

• Highlight query keywords in context– Often useful for display purposes

• Important Classes:– Highlighter - Main entry point, coordinates the work

– Fragmenter - Splits up document for scoring

– Formatter - Marks up the results– Scorer - Scores the fragments

• SpanScorer - Can score phrases

• Use term vectors for performance• Look at example usage

Page 43: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

43

Spell Checking

• Suggest spelling corrections based on spellings of words in the index– Will/can suggest incorrectly spelled words

• Uses a distance measure to determine suggestions– Can also factor in document frequency

– Distance Measure is pluggable

Page 44: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

44

Spell Checking• Classes: Spellchecker, StringDistance

• See ContribExamplesTest• Practical aspects:– It’s not as simple as just turning it on

– Good results require testing and tuning•Pay attention to accuracy settings•Mind your Analysis (simple, no stemming)

•Consider alternate StringDistance (JaroWinklerDistance)

Page 45: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

45

More Like This

• Given a Document, find other Documents that are similar– Variation on relevance feedback– “Find Similar”

• Extracts the most important terms from a Document and creates a new query– Many options available for determining important terms

• Classes: MoreLikeThis– See ContribExamplesTest

Page 46: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

46

Summary

• Indexing• Searching• Analysis• Contrib

• Questions?

Page 47: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Resources

• http://lucene.apache.org/

• http://en.wikipedia.org/wiki/Vector_space_model

• Modern Information Retrieval by Baeza-Yates and Ribeiro-Neto

• Lucene In Action by Hatcher and Gospodnetić

• Wiki

• Mailing Lists– [email protected]

• Discussions on how to use Lucene

[email protected]• Discussions on how to develop Lucene

• Issue Tracking– https://issues.apache.org/jira/secure/Dashboard.jspa

• We always welcome patches– Ask on the mailing list before reporting a bug

Page 48: Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA

Resources

[email protected]• Lucid Imagination– Support– Training– Value Add– [email protected]