Introduction to Information Retrieval Introduction to Information Retrieval Lucene Tutorial Chris Manning and Pandu Nayak.

Slides:



Advertisements
Similar presentations
Lucene in action Information Retrieval A.A – P. Ferragina, U. Scaiella – – Dipartimento di Informatica – Università di Pisa –
Advertisements

Assignment 2: Full text search with Lucene Mathias Mosolf, Alexander Frenzel.
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Lucene Near Realtime Search Jason Rutherglen & Jake Mannix LinkedIn 6/3/2009 SOLR/Lucene Users Group San Francisco.
Lucene/SOLR 2: Lucene search API
Lucene Tutorial Based on Lucene in Action Michael McCandless, Erik Hatcher, Otis Gospodnetic.
Chapter 5: Introduction to Information Retrieval
© Copyright 2012 STI INNSBRUCK Apache Lucene Ioan Toma based on slides from Aaron Bannert
Advanced Indexing Techniques with Apache Lucene - Payloads Advanced Indexing Techniques with Michael Busch
Advanced Indexing Techniques with
The Lucene Search Engine Kira Radinsky Modified by Amit Gross to Lucene 4 Based on the material from: Thomas Paul and Steven J. Owens.
Lucene in action Information Retrieval A.A – P. Ferragina, U. Scaiella – – Dipartimento di Informatica – Università di Pisa –
Lucene Part3‏. Lucene High Level Infrastructure When you look at building your search solution, you often find that the process is split into two main.
For ITCS 6265 Professor: Wensheng Wu Present by TA: Xu Fei.
Solr has a lot of extensive features Solr Integration and Enhancements Todd Hatcher.
The Lucene Search Engine Kira Radinsky Based on the material from: Thomas Paul and Steven J. Owens.
Lucene Lab General IR Process Start Indexing (start stepping though all files) Tokenize & stem each file Index 1 st, Index User enters (roughly)
Parametric search and zone weighting Lecture 6. Recap of lecture 4 Query expansion Index construction.
1 CS 430 / INFO 430 Information Retrieval Lecture 6 Vector Methods 2.
Introduction to Lucene Debapriyo Majumdar Information Retrieval – Spring 2015 Indian Statistical Institute Kolkata.
Overview of Search Engines
Introduction to Information Retrieval Introduction to Information Retrieval Lucene Tutorial Chris Manning, Pandu Nayak, and Prabhakar Raghavan.
Full-Text Search with Lucene Yonik Seeley 02 May 2007 Amsterdam, Netherlands.
Full-Text Search with Lucene Yonik Seeley 02 May 2007 Amsterdam, Netherlands slides:
1 Introduction to Lucene Rong Jin. What is Lucene ?  Lucene is a high performance, scalable Information Retrieval (IR) library Free, open-source project.
Apache Lucene in LexGrid. Lucene Overview High-performance, full-featured text search engine library. Written entirely in Java. An open source project.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Softvérové knižnice a systémy Vyhľadávanie informácií Michal Laclavík.
1 Lucene Jianguo Lu School of Computer Science University of Windsor.
Lucene Open Source Search Engine. Lucene - Overview Complete search engine in a Java library Stand-alone only, no server – But can use SOLR Handles indexing.
Advanced Lucene Grant Ingersoll Center for Natural Language Processing ApacheCon 2005 December 12, 2005.
Lucene Boot Camp I Grant Ingersoll Lucid Imagination Nov. 3, 2008 New Orleans, LA.
Lucene Performance Grant Ingersoll November 16, 2007 Atlanta, GA.
Vyhľadávanie informácií Softvérové knižnice a systémy Vyhľadávanie informácií Michal Laclavík.
Lucene Part2. Lucene Jarkarta Lucene ( is a high- performance, full-featured, java, open-source, text search engine.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Lucene Part1 ‏. Lucene Use Case Store data in a 2 dimensional way How do we do this. Spreadsheet Relational Database X/Y.
Indexing UMLS concepts with Apache Lucene Julien Thibault University of Utah Department of Biomedical Informatics.
Chapter 6: Information Retrieval and Web Search
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
Lucene-Demo Brian Nisonger. Intro No details about Implementation/Theory No details about Implementation/Theory See Treehouse Wiki- Lucene for additional.
“ Lucene.Net is a source code, class-per-class, API-per-API and algorithmatic port of the Java Lucene search engine to the C# and.NET ”
Iccha Sethi Serdar Aslan Team 1 Virginia Tech Information Storage and Retrieval CS 5604 Instructor: Dr. Edward Fox 10/11/2010.
Lucene Boot Camp Grant Ingersoll Lucid Imagination Nov. 4, 2008 New Orleans, LA.
Design a full-text search engine for a website based on Lucene
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
Web Information Retrieval Textbook by Christopher D. Manning, Prabhakar Raghavan, and Hinrich Schutze Notes Revised by X. Meng for SEU May 2014.
Lucene Jianguo Lu.
Information Retrieval Lecture 6 Vector Methods 2.
Presented By:. What is JavaHelp: Most software developers do not look forward to spending time documenting and explaining their product. JavaSoft has.
CS520 Web Programming Full Text Search Chengyu Sun California State University, Los Angeles.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Introduction to Information Retrieval Introduction to Information Retrieval ΜΕ003-ΠΛΕ70: Ανάκτηση Πληροφορίας Διδάσκουσα: Ευαγγελία Πιτουρά Εισαγωγή στο.
Lucene : Text Search IG5 – TILE Esther Pacitti. Basic Architecture.
ΠΛΕ70: Ανάκτηση Πληροφορίας
Lucene Tutorial Chris Manning and Pandu Nayak
Searching AND INDEXING Big data
Query Models Use Types What do search engines do.
CS276 Lucene Section.
Searching and Indexing
Safe by default, optimized for efficiency
Implementation Issues & IR Systems
Vores tankesæt: 80% teknologi | 20% forretning
Topics Introduction to File Input and Output
Packages and Interfaces
Lucene in action Information Retrieval A.A
Lucene/Solr Architecture
Introduction to Elasticsearch with basics of Lucene May 2014 Meetup
Topics Introduction to File Input and Output
Presentation transcript:

Introduction to Information Retrieval Introduction to Information Retrieval Lucene Tutorial Chris Manning and Pandu Nayak

Introduction to Information Retrieval Open source IR systems  Widely used academic systems  Terrier (Java, U. Glasgow)  Indri/Galago/Lemur (C++ (& Java), U. Mass & CMU)  Tail of others (Zettair, …)  Widely used non-academic open source systems  Lucene  Things built on it: Solr, ElasticSearch  A few others (Xapian, …)

Introduction to Information Retrieval Based on “Lucene in Action” By Michael McCandless, Erik Hatcher, Otis Gospodnetic Covers Lucene It’s now up to 4.8 (used in examples)

Introduction to Information Retrieval Lucene  Open source Java library for indexing and searching  Lets you add search to your application  Not a complete search system by itself  Written by Doug Cutting (coming to talk this quarter!)  Used by: Twitter, LinkedIn; Reddit, Zappos; CiteSeer, Eclipse, …  … and many more (see java/PoweredBy) java/PoweredBy  Ports/integrations to other languages  C/C++, C#, Ruby, Perl, Python, PHP, …

Introduction to Information Retrieval Resources  Lucene:  Lucene in Action:  Code samples available for download  Ant:  Java build system used by “Lucene in Action” code

Introduction to Information Retrieval Lucene in a search system Raw Content Acquire content Build document Analyze document Index document Index Users Search UI Build query Render results Run query

Introduction to Information Retrieval Lucene demos  Command line Indexer  org.apache.lucene.demo.IndexFiles  Command line Searcher  org.apache.lucene.demo.SearchFiles

Introduction to Information Retrieval Core indexing classes  IndexWriter  Central component that allows you to create a new index, open an existing one, and add, remove, or update documents in an index  Built on an IndexWriterConfig and a Directory  Directory  Abstract class that represents the location of an index  Analyzer  Extracts tokens from a text stream

Introduction to Information Retrieval Creating an IndexWriter import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; import org.apache.lucene.analysis.standard.StandardAnalyzer;... public static final Version luceneVersion = Version.LUCENE_40; IndexWriter getIndexWriter(String dir) { Directory indexDir = FSDirectory.open(new File(dir)); IndexWriterConfig luceneConfig = new IndexWriterConfig( luceneVersion, new StandardAnalyzer(luceneVersion)); return(new IndexWriter(indexDir, luceneConfig)); }

Introduction to Information Retrieval Core indexing classes (contd.)  Document  Represents a collection of named Field s. Text in these Field s are indexed.  Field  Note: Lucene Field s can represent both “fields” and “zones” as described in the textbook  Or even other things like numbers.

Introduction to Information Retrieval A Document contains Field s import org.apache.lucene.document.Document; import org.apache.lucene.document.Field;... protected Document getDocument(File f) throws Exception { Document doc = new Document(); doc.add(new TextField("contents”, new FileReader(f))) doc.add(new StringField("filename”, f.getName())); doc.add(new StringField("fullpath”, f.getCanonicalPath())); return doc; }

Introduction to Information Retrieval Index a Document with IndexWriter private IndexWriter writer;... private void indexFile(File f) throws Exception { Document doc = getDocument(f); writer.addDocument(doc); }

Introduction to Information Retrieval Indexing a directory private IndexWriter writer;... public int index(String dataDir, FileFilter filter) throws Exception { File[] files = new File(dataDir).listFiles(); for (File f: files) { if (... && (filter == null || filter.accept(f))) { indexFile(f); } return writer.numDocs(); }

Introduction to Information Retrieval Closing the IndexWriter private IndexWriter writer;... public void close() throws IOException { writer.close(); }

Introduction to Information Retrieval The Index  The Index is the kind of inverted index we know and love  The default Lucene40 codec is:  variable-byte coding of delta values  multi-level skip lists  natural ordering of docIDs  interleaved docIDs and position data  Very short postings lists are inlined into the term dictionary  Other codecs are available: PFOR-delta, Simple9, …

Introduction to Information Retrieval Core searching classes  IndexSearcher  Central class that exposes several search methods on an index  Accessed via an IndexReader  Query  Abstract query class. Concrete subclasses represent specific types of queries, e.g., matching terms in fields, boolean queries, phrase queries, …  QueryParser  Parses a textual representation of a query into a Query instance

Introduction to Information Retrieval IndexSearcher IndexReader Directory QueryTopDocs

Introduction to Information Retrieval Creating an IndexSearcher import org.apache.lucene.search.IndexSearcher;... public static void search(String indexDir, String q) throws IOException, ParseException { IndexReader rdr = DirectoryReader.open( FSDirectory.open( new File(indexDir))); IndexSearcher is = new IndexSearcher(rdr);... }

Introduction to Information Retrieval Query and QueryParser import org.apache.lucene.search.Query; import org.apache.lucene.queryParser.QueryParser;... public static void search(String indexDir, String q) throws IOException, ParseException... QueryParser parser = new QueryParser(Version.LUCENE_40, "contents”, new StandardAnalyzer( Version.LUCENE_40)); Query query = parser.parse(q);... }

Introduction to Information Retrieval Core searching classes (contd.)  TopDocs  Contains references to the top documents returned by a search  ScoreDoc  Represents a single search result

Introduction to Information Retrieval search() returns TopDoc s import org.apache.lucene.search.TopDocs;... public static void search(String indexDir, String q) throws IOException, ParseException... IndexSearcher is =...;... Query query =...;... TopDocs hits = is.search(query, 10); }

Introduction to Information Retrieval TopDoc s contain ScoreDoc s import org.apache.lucene.search.ScoreDoc;... public static void search(String indexDir, String q) throws IOException, ParseException... IndexSearcher is =...;... TopDocs hits =...;... for(ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullpath")); }

Introduction to Information Retrieval Closing IndexSearcher public static void search(String indexDir, String q) throws IOException, ParseException... IndexSearcher is =...;... is.close(); }

Introduction to Information Retrieval How Lucene models content  A Document is the atomic unit of indexing and searching  A Document contains Field s  Field s have a name and a value  You have to translate raw content into Field s  Examples: Title, author, date, abstract, body, URL, keywords,...  Different documents can have different fields  Search a field using name:term, e.g., title:lucene

Introduction to Information Retrieval Field s  Field s may  Be indexed or not  Indexed fields may or may not be analyzed (i.e., tokenized with an Analyzer )  Non-analyzed fields view the entire value as a single token (useful for URLs, paths, dates, social security numbers,...)  Be stored or not  Useful for fields that you’d like to display to users  Optionally store term vectors  Like a positional index on the Field ’s terms  Useful for highlighting, finding similar documents, categorization

Introduction to Information Retrieval Field construction Lots of different constructors import org.apache.lucene.document.Field Field(String name, String value, Field.Store store, // store or not Field.Index index, // index or not Field.TermVector termVector); value can also be specified with a Reader, a TokenStream, or a byte[]

Introduction to Information Retrieval Field options  Field.Store  NO : Don’t store the field value in the index  YES : Store the field value in the index  Field.Index  ANALYZED : Tokenize with an Analyzer  NOT_ANALYZED : Do not tokenize  NO : Do not index this field  Couple of other advanced options  Field.TermVector  NO : Don’t store term vectors  YES : Store term vectors  Several other options to store positions and offsets

Introduction to Information Retrieval Using Field options IndexStoreTermVectorExample usage NOT_ANALYZEDYESNOIdentifiers, telephone/SSNs, URLs, dates,... ANALYZEDYESWITH_POSITIONS_OFFSETSTitle, abstract ANALYZEDNOWITH_POSITIONS_OFFSETSBody NOYESNODocument type, DB keys (if not used for searching) NOT_ANALYZEDNO Hidden keywords

Introduction to Information Retrieval Document import org.apache.lucene.document.Field  Constructor:  Document();  Methods  void add(Fieldable field); // Field implements // Fieldable  String get(String name); // Returns value of // Field with given // name  Fieldable getFieldable(String name); ... and many more

Introduction to Information Retrieval Multi-valued fields  You can add multiple Field s with the same name  Lucene simply concatenates the different values for that named Field Document doc = new Document(); doc.add(new Field(“author”, “chris manning”, Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field(“author”, “prabhakar raghavan”, Field.Store.YES, Field.Index.ANALYZED));...

Introduction to Information Retrieval Analyzer  Tokenizes the input text  Common Analyzer s  WhitespaceAnalyzer Splits tokens on whitespace  SimpleAnalyzer Splits tokens on non-letters, and then lowercases  StopAnalyzer Same as SimpleAnalyzer, but also removes stop words  StandardAnalyzer Most sophisticated analyzer that knows about certain token types, lowercases, removes stop words,...

Introduction to Information Retrieval Analysis example  “The quick brown fox jumped over the lazy dog”  WhitespaceAnalyzer  [The] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dog]  SimpleAnalyzer  [the] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dog]  StopAnalyzer  [quick] [brown] [fox] [jumped] [over] [lazy] [dog]  StandardAnalyzer  [quick] [brown] [fox] [jumped] [over] [lazy] [dog]

Introduction to Information Retrieval Another analysis example  “XY&Z Corporation –  WhitespaceAnalyzer  [XY&Z] [Corporation] [-]  SimpleAnalyzer  [xy] [z] [corporation] [xyz] [example] [com]  StopAnalyzer  [xy] [z] [corporation] [xyz] [example] [com]  StandardAnalyzer  [xy&z] [corporation]

Introduction to Information Retrieval What’s inside an Analyzer ?  Analyzer s need to return a TokenStream public TokenStream tokenStream(String fieldName, Reader reader) TokenStream TokenizerTokenFilter ReaderTokenizerTokenFilter

Introduction to Information Retrieval Tokenizer s and TokenFilter s  Tokenizer  WhitespaceTokenizer  KeywordTokenizer  LetterTokenizer  StandardTokenizer ...  TokenFilter  LowerCaseFilter  StopFilter  PorterStemFilter  ASCIIFoldingFilter  StandardFilter ...

Introduction to Information Retrieval Adding/deleting Document s to/from an IndexWriter void addDocument(Document d); void addDocument(Document d, Analyzer a); Important: Need to ensure that Analyzer s used at indexing time are consistent with Analyzer s used at searching time // deletes docs containing term or matching // query. The term version is useful for // deleting one document. void deleteDocuments(Term term); void deleteDocuments(Query query);

Introduction to Information Retrieval Index format  Each Lucene index consists of one or more segments  A segment is a standalone index for a subset of documents  All segments are searched  A segment is created whenever IndexWriter flushes adds/deletes  Periodically, IndexWriter will merge a set of segments into a single segment  Policy specified by a MergePolicy  You can explicitly invoke optimize() to merge segments

Introduction to Information Retrieval Basic merge policy  Segments are grouped into levels  Segments within a group are roughly equal size (in log space)  Once a level has enough segments, they are merged into a segment at the next level up

Introduction to Information Retrieval Searching a changing index Directory dir = FSDirectory.open(...); IndexReader reader = IndexReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); Above reader does not reflect changes to the index unless you reopen it. Reopen ing is more resource efficient than open ing a new IndexReader. IndexReader newReader = reader.reopen(); If (reader != newReader) { reader.close(); reader = newReader; searcher = new IndexSearcher(reader); }

Introduction to Information Retrieval Near-real-time search IndexWriter writer =...; IndexReader reader = writer.getReader(); IndexSearcher searcher = new IndexSearcher(reader); // Now let us say there’s a change to the index using writer writer.addDocument(newDoc); // reopen() and getReader() force writer to flush IndexReader newReader = reader.reopen(); if (reader != newReader) { reader.close(); reader = newReader; searcher = new IndexSearcher(reader); }

Introduction to Information Retrieval IndexSearcher  Methods  TopDocs search(Query q, int n);  Document doc(int docID);

Introduction to Information Retrieval QueryParser  Constructor  QueryParser(Version matchVersion, String defaultField, Analyzer analyzer);  Parsing methods  Query parse(String query) throws ParseException; ... and many more

Introduction to Information Retrieval QueryParser syntax examples Query expressionDocument matches if… javaContains the term java in the default field java junit java OR junit Contains the term java or junit or both in the default field (the default operator can be changed to AND) +java +junit java AND junit Contains both java and junit in the default field title:antContains the term ant in the title field title:extreme –subject:sportsContains extreme in the title and not sports in subject (agile OR extreme) AND javaBoolean expression matches title:”junit in action”Phrase matches in title title:”junit action”~5Proximity matches (within 5) in title java*Wildcard matches java~Fuzzy matches lastmodified:[1/1/09 TO 12/31/09] Range matches

Introduction to Information Retrieval Construct Query s programmatically  TermQuery  Constructed from a Term  TermRangeQuery  NumericRangeQuery  PrefixQuery  BooleanQuery  PhraseQuery  WildcardQuery  FuzzyQuery  MatchAllDocsQuery

Introduction to Information Retrieval TopDocs and ScoreDoc  TopDocs methods  Number of documents that matched the search totalHits  Array of ScoreDoc instances containing results scoreDocs  Returns best score of all matches getMaxScore()  ScoreDoc methods  Document id doc  Document score score

Introduction to Information Retrieval Scoring  Original scoring function uses basic tf-idf scoring with  Programmable boost values for certain fields in documents  Length normalization  Boosts for documents containing more of the query terms  IndexSearcher provides an explain() method that explains the scoring of a document

Introduction to Information Retrieval Lucene 4.0 Scoring  As well as traditional tf.idf vector space model, Lucene 4.0+ adds:  BM25  drf (divergence from randomness)  ib (information (theory)-based similarity) indexSearcher.setSimilarity( new BM25Similarity()); BM25Similarity custom = new BM25Similarity(1.2, 0.75); // k1, b indexSearcher.setSimilarity(custom);