the lucene search engine

15
The Lucene Search Engine Kira Radinsky Modified by Amit Gross to Lucene 4 Based on the material from: Thomas Paul and Steven J. Owens

Upload: ulema

Post on 23-Feb-2016

66 views

Category:

Documents


0 download

DESCRIPTION

The Lucene Search Engine. Kira Radinsky Modified by Amit Gross to Lucene 4. Based on the material from: Thomas Paul and Steven J. Owens. What is Lucene ?. Doug Cutting’s grandmother’s middle name A open source set of Java Classses Search Engine/Document Classifier/Indexer - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The  Lucene  Search Engine

The Lucene Search Engine

Kira RadinskyModified by Amit Gross to Lucene 4

Based on the material from: Thomas Paul and Steven J. Owens

Page 2: The  Lucene  Search Engine

What is Lucene?• Doug Cutting’s grandmother’s middle name• A open source set of Java Classses– Search Engine/Document Classifier/Indexer– Developed by Doug Cutting (1996)

• Xerox/Apple/Excite/Nutch/Yahoo/Cloudera• Hadoop founder, Board of directors of the Apache Software

• Jakarta Apache Product. Strong open source community support.

• High-performance, full-featured text search engine library

• Easy to use yet powerful API

Page 3: The  Lucene  Search Engine

Use the Source, Luke• Document • Field

– Represents a section of a Document: name for the section + the actual data.

• Analyzer – Abstract class (to provide interface)– Document -> tokens (for later indexing)– StandardAnalyzer class.

• IndexWriter – Creates and maintains indexes.

• IndexSearcher – Searches through an index.

• QueryParser – Builds a parser that can search through an index.

• Query – Abstract class that contains the search criteria created by the QueryParser.

• TopDocs – Contains the top K Document objects found in a serach by an IndexSearcher,

and their scores.

Page 4: The  Lucene  Search Engine

Indexing a Document

Page 5: The  Lucene  Search Engine

Document from an articleprivate Document createDocument(String article, String author, String title, String topic, String url,

Date dateWritten) {

document.add(new TextField("author",author, Store.YES));document.add(new TextField("title",title, Store.YES ));document.add(new TextField("topic",topic, Store.YES ));document.add(new TextField("article", article, Store.NO));document.add(new StoredField("URL", url));document.add(new StringField("Date", dateWritten,

Store.NO)); return document;

}

Page 6: The  Lucene  Search Engine

The Field Object

Subclass Tokenized Indexed Stored Use for

TextField Yes Yes Can be contents you indexed and tokenized

StoredField No No Yescontents you don’t want to index, but want to store (url for example)

StringField No Yes Can beValues you want indexed but not tokenized (dates, keywords,..)

Page 7: The  Lucene  Search Engine

Store a Document in the indexDirectory dir = FSDirectory.open(new File("lucene-index"));private void indexDocument(Document document)

throws Exception {

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);

IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45, analyzer);

IndexWriter writer = new IndexWriter(dir, iwc); writer.addDocument(document);

writer.close();}

Page 8: The  Lucene  Search Engine

Analyzers and TokenizersSimpleAnalyzer SimpleAnalyzer seems to just use a Tokenizer that converts all

of the input to lower case.

StopAnalyzer StopAnalyzer includes the lower-case filter, and also has a filter that drops out any "stop words", words like articles (a, an, the, etc) that occur so commonly in english that they might as well be noise for searching purposes. StopAnalyzer comes with a set of stop words, but you can instantiate it with your own array of stop words.

StandardAnalyzer StandardAnalyzer does both lower-case and stop-word filtering, and in addition tries to do some basic clean-up of words, for example taking out apostrophes ( ' ) and removing periods from acronyms (i.e. "T.L.A." becomes "TLA").

Lucene Sandbox Here you can find analyzers in your own language

Page 9: The  Lucene  Search Engine

Adding to an Indexpublic void indexArticle(

String article, String author,String title, String topic,String url, Date dateWritten)

throws Exception { Document document = createDocument

(article, author, title, topic, url, dateWritten);

indexDocument(document);}

Page 10: The  Lucene  Search Engine

Searching the Index

Page 11: The  Lucene  Search Engine

Searching

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir));QueryParser qp = new QueryParser(Version.LUCENE_45, "article", analyzer);Query q = qp.parse(searchString);TopDocs top = searcher.search(q, numResults);

Page 12: The  Lucene  Search Engine

Extracting Document objects

for (ScoreDoc sd : top.scoreDocs) {Document doc =

searcher.doc(sd.doc); // display the articles that were

found to the user}

Page 13: The  Lucene  Search Engine

Search Criteria

Supports several searches: AND OR and NOT, fuzzy, proximity searches, wildcard searches, and range searches– author:Henry relativity AND "quantum physics“– "string theory" NOT Einstein– "Galileo Kepler"~5– author:Johnson date:[01/01/2004 TO 01/31/2004]

Page 14: The  Lucene  Search Engine

Thread Safety • Indexing and searching are not only thread safe, but

process safe. What this means is that:– Multiple index searchers can read the lucene index files at

the same time.– An index writer or reader can edit the lucene index files

while searches are ongoing– Multiple index writers or readers can try to edit the lucene

index files at the same time (it's important for the index writer/reader to be closed so it will release the file lock).

• The query parser is not thread safe, • The index writer however, is thread safe,

Page 15: The  Lucene  Search Engine

Luke

• Luke is a handy tool for development, that allows you to watch an already existing Lucene Index.

• http://code.google.com/p/luke/