org.apache.lucene.index.memory
public class: MemoryIndex [javadoc |
source]
java.lang.Object
org.apache.lucene.index.memory.MemoryIndex
High-performance single-document main memory Apache Lucene fulltext search index.
Overview
This class is a replacement/substitute for a large subset of
org.apache.lucene.store.RAMDirectory functionality. It is designed to
enable maximum efficiency for on-the-fly matchmaking combining structured and
fuzzy fulltext search in realtime streaming applications such as Nux XQuery based XML
message queues, publish-subscribe systems for Blogs/newsfeeds, text chat, data acquisition and
distribution systems, application level routers, firewalls, classifiers, etc.
Rather than targetting fulltext search of infrequent queries over huge persistent
data archives (historic search), this class targets fulltext search of huge
numbers of queries over comparatively small transient realtime data (prospective
search).
For example as in
float score = search(String text, Query query)
Each instance can hold at most one Lucene "document", with a document containing
zero or more "fields", each field having a name and a fulltext value. The
fulltext value is tokenized (split and transformed) into zero or more index terms
(aka words) on addField(), according to the policy implemented by an
Analyzer. For example, Lucene analyzers can split on whitespace, normalize to lower case
for case insensitivity, ignore common terms with little discriminatory value such as "he", "in", "and" (stop
words), reduce the terms to their natural linguistic root form such as "fishing"
being reduced to "fish" (stemming), resolve synonyms/inflexions/thesauri
(upon indexing and/or querying), etc. For details, see
Lucene Analyzer Intro.
Arbitrary Lucene queries can be run against this class - see Lucene Query Syntax
as well as Query Parser Rules.
Note that a Lucene query selects on the field names and associated (indexed)
tokenized terms, not on the original fulltext(s) - the latter are not stored
but rather thrown away immediately after tokenization.
For some interesting background information on search technology, see Bob Wyman's
Prospective Search,
Jim Gray's
A Call to Arms - Custom subscriptions, and Tim Bray's
On Search, the Series.
Example Usage
Analyzer analyzer = PatternAnalyzer.DEFAULT_ANALYZER;
//Analyzer analyzer = new SimpleAnalyzer();
MemoryIndex index = new MemoryIndex();
index.addField("content", "Readings about Salmons and other select Alaska fishing Manuals", analyzer);
index.addField("author", "Tales of James", analyzer);
QueryParser parser = new QueryParser("content", analyzer);
float score = index.search(parser.parse("+author:james +salmon~ +fish* manual~"));
if (score > 0.0f) {
System.out.println("it's a match");
} else {
System.out.println("no match found");
}
System.out.println("indexData=" + index.toString());
Example XQuery Usage
(: An XQuery that finds all books authored by James that have something to do with "salmon fishing manuals", sorted by relevance :)
declare namespace lucene = "java:nux.xom.pool.FullTextUtil";
declare variable $query := "+salmon~ +fish* manual~"; (: any arbitrary Lucene query can go here :)
for $book in /books/book[author="James" and lucene:match(abstract, $query) > 0.0]
let $score := lucene:match($book/abstract, $query)
order by $score descending
return $book
No thread safety guarantees
An instance can be queried multiple times with the same or different queries,
but an instance is not thread-safe. If desired use idioms such as:
MemoryIndex index = ...
synchronized (index) {
// read and/or write index (i.e. add fields and/or query)
}
Performance Notes
Internally there's a new data structure geared towards efficient indexing
and searching, plus the necessary support code to seamlessly plug into the Lucene
framework.
This class performs very well for very small texts (e.g. 10 chars)
as well as for large texts (e.g. 10 MB) and everything in between.
Typically, it is about 10-100 times faster than RAMDirectory.
Note that RAMDirectory has particularly
large efficiency overheads for small to medium sized texts, both in time and space.
Indexing a field with N tokens takes O(N) in the best case, and O(N logN) in the worst
case. Memory consumption is probably larger than for RAMDirectory.
Example throughput of many simple term queries over a single MemoryIndex:
~500000 queries/sec on a MacBook Pro, jdk 1.5.0_06, server VM.
As always, your mileage may vary.
If you're curious about
the whereabouts of bottlenecks, run java 1.5 with the non-perturbing '-server
-agentlib:hprof=cpu=samples,depth=10' flags, then study the trace log and
correlate its hotspot trailer with its call stack headers (see
hprof tracing ).
- author:
whoschek.AT.lbl.DOT.gov -
| Method from org.apache.lucene.index.memory.MemoryIndex Detail: |
public void addField(String fieldName,
TokenStream stream) {
addField(fieldName, stream, 1.0f);
}
Equivalent to addField(fieldName, stream, 1.0f). |
public void addField(String fieldName,
String text,
Analyzer analyzer) {
if (fieldName == null)
throw new IllegalArgumentException("fieldName must not be null");
if (text == null)
throw new IllegalArgumentException("text must not be null");
if (analyzer == null)
throw new IllegalArgumentException("analyzer must not be null");
TokenStream stream;
if (analyzer instanceof PatternAnalyzer) {
stream = ((PatternAnalyzer) analyzer).tokenStream(fieldName, text);
} else {
stream = analyzer.tokenStream(fieldName,
new PatternAnalyzer.FastStringReader(text));
}
addField(fieldName, stream);
}
|
public void addField(String fieldName,
TokenStream stream,
float boost) {
/*
* Note that this method signature avoids having a user call new
* o.a.l.d.Field(...) which would be much too expensive due to the
* String.intern() usage of that class.
*
* More often than not, String.intern() leads to serious performance
* degradations rather than improvements! If you're curious why, check
* out the JDK's native code, see how it oscillates multiple times back
* and forth between Java code and native code on each intern() call,
* only to end up using a plain vanilla java.util.HashMap on the Java
* heap for it's interned strings! String.equals() has a small cost
* compared to String.intern(), trust me. Application level interning
* (e.g. a HashMap per Directory/Index) typically leads to better
* solutions than frequent hidden low-level calls to String.intern().
*
* Perhaps with some luck, Lucene's Field.java (and Term.java) and
* cousins could be fixed to not use String.intern(). Sigh :-(
*/
try {
if (fieldName == null)
throw new IllegalArgumentException("fieldName must not be null");
if (stream == null)
throw new IllegalArgumentException("token stream must not be null");
if (boost < = 0.0f)
throw new IllegalArgumentException("boost factor must be greater than 0.0");
if (fields.get(fieldName) != null)
throw new IllegalArgumentException("field must not be added more than once");
HashMap terms = new HashMap();
int numTokens = 0;
int pos = -1;
Token token;
while ((token = stream.next()) != null) {
String term = token.termText();
if (term.length() == 0) continue; // nothing to do
// if (DEBUG) System.err.println("token='" + term + "'");
numTokens++;
pos += token.getPositionIncrement();
ArrayIntList positions = (ArrayIntList) terms.get(term);
if (positions == null) { // term not seen before
positions = new ArrayIntList(stride);
terms.put(term, positions);
}
if (stride == 1) {
positions.add(pos);
} else {
positions.add(pos, token.startOffset(), token.endOffset());
}
}
// ensure infos.numTokens > 0 invariant; needed for correct operation of terms()
if (numTokens > 0) {
boost = boost * docBoost; // see DocumentWriter.addDocument(...)
fields.put(fieldName, new Info(terms, numTokens, boost));
sortedFields = null; // invalidate sorted view, if any
}
} catch (IOException e) { // can never happen
throw new RuntimeException(e);
} finally {
try {
if (stream != null) stream.close();
} catch (IOException e2) {
throw new RuntimeException(e2);
}
}
}
Iterates over the given token stream and adds the resulting terms to the index;
Equivalent to adding a tokenized, indexed, termVectorStored, unstored,
Lucene org.apache.lucene.document.Field .
Finally closes the token stream. Note that untokenized keywords can be added with this method via
#keywordTokenStream(Collection) , the Lucene contrib KeywordTokenizer or similar utilities. |
public IndexSearcher createSearcher() {
MemoryIndexReader reader = new MemoryIndexReader();
IndexSearcher searcher = new IndexSearcher(reader); // ensures no auto-close !!
reader.setSearcher(searcher); // to later get hold of searcher.getSimilarity()
return searcher;
}
Creates and returns a searcher that can be used to execute arbitrary
Lucene queries and to collect the resulting query results as hits. |
public int getMemorySize() {
// for example usage in a smart cache see nux.xom.pool.Pool
int PTR = VM.PTR;
int INT = VM.INT;
int size = 0;
size += VM.sizeOfObject(2*PTR + INT); // memory index
if (sortedFields != null) size += VM.sizeOfObjectArray(sortedFields.length);
size += VM.sizeOfHashMap(fields.size());
Iterator iter = fields.entrySet().iterator();
while (iter.hasNext()) { // for each Field Info
Map.Entry entry = (Map.Entry) iter.next();
Info info = (Info) entry.getValue();
size += VM.sizeOfObject(2*INT + 3*PTR); // Info instance vars
if (info.sortedTerms != null) size += VM.sizeOfObjectArray(info.sortedTerms.length);
int len = info.terms.size();
size += VM.sizeOfHashMap(len);
Iterator iter2 = info.terms.entrySet().iterator();
while (--len >= 0) { // for each term
Map.Entry e = (Map.Entry) iter2.next();
size += VM.sizeOfObject(PTR + 3*INT); // assumes substring() memory overlay
// size += STR + 2 * ((String) e.getKey()).length();
ArrayIntList positions = (ArrayIntList) e.getValue();
size += VM.sizeOfArrayIntList(positions.size());
}
}
return size;
}
Returns a reasonable approximation of the main memory [bytes] consumed by
this instance. Useful for smart memory sensititive caches/pools. Assumes
fieldNames are interned, whereas tokenized terms are memory-overlaid. |
public TokenStream keywordTokenStream(Collection keywords) {
// TODO: deprecate & move this method into AnalyzerUtil?
if (keywords == null)
throw new IllegalArgumentException("keywords must not be null");
return new TokenStream() {
private Iterator iter = keywords.iterator();
private int start = 0;
public Token next() {
if (!iter.hasNext()) return null;
Object obj = iter.next();
if (obj == null)
throw new IllegalArgumentException("keyword must not be null");
String term = obj.toString();
Token token = new Token(term, start, start + term.length());
start += term.length() + 1; // separate words by 1 (blank) character
return token;
}
};
}
|
public float search(Query query) {
if (query == null)
throw new IllegalArgumentException("query must not be null");
Searcher searcher = createSearcher();
try {
final float[] scores = new float[1]; // inits to 0.0f (no match)
searcher.search(query, new HitCollector() {
public void collect(int doc, float score) {
scores[0] = score;
}
});
float score = scores[0];
return score;
} catch (IOException e) { // can never happen (RAMDirectory)
throw new RuntimeException(e);
} finally {
// searcher.close();
/*
* Note that it is harmless and important for good performance to
* NOT close the index reader!!! This avoids all sorts of
* unnecessary baggage and locking in the Lucene IndexReader
* superclass, all of which is completely unnecessary for this main
* memory index data structure without thread-safety claims.
*
* Wishing IndexReader would be an interface...
*
* Actually with the new tight createSearcher() API auto-closing is now
* made impossible, hence searcher.close() would be harmless and also
* would not degrade performance...
*/
}
}
Convenience method that efficiently returns the relevance score by
matching this index against the given Lucene query expression. |
public String toString() {
StringBuffer result = new StringBuffer(256);
sortFields();
int sumChars = 0;
int sumPositions = 0;
int sumTerms = 0;
for (int i=0; i < sortedFields.length; i++) {
Map.Entry entry = sortedFields[i];
String fieldName = (String) entry.getKey();
Info info = (Info) entry.getValue();
info.sortTerms();
result.append(fieldName + ":\n");
int numChars = 0;
int numPositions = 0;
for (int j=0; j < info.sortedTerms.length; j++) {
Map.Entry e = info.sortedTerms[j];
String term = (String) e.getKey();
ArrayIntList positions = (ArrayIntList) e.getValue();
result.append("\t'" + term + "':" + numPositions(positions) + ":");
result.append(positions.toString(stride)); // ignore offsets
result.append("\n");
numPositions += numPositions(positions);
numChars += term.length();
}
result.append("\tterms=" + info.sortedTerms.length);
result.append(", positions=" + numPositions);
result.append(", Kchars=" + (numChars/1000.0f));
result.append("\n");
sumPositions += numPositions;
sumChars += numChars;
sumTerms += info.sortedTerms.length;
}
result.append("\nfields=" + sortedFields.length);
result.append(", terms=" + sumTerms);
result.append(", positions=" + sumPositions);
result.append(", Kchars=" + (sumChars/1000.0f));
return result.toString();
}
Returns a String representation of the index data for debugging purposes. |