|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.apache.lucene.search.Query
de.pangaea.metadataportal.search.MoreLikeThisQuery
public final class MoreLikeThisQuery
This implementation of a "more like this" is based on an algorithm from Lucene Contrib and optimized for use with panFMP. The constructor needs a panFMP document ID and optional a field name (which must have term vectors enabled). If you use the default field for similarity calculation, the default field must have term vectors.
Lucene does let you access the document frequency of terms, with IndexReader.docFreq(). Term frequencies can be computed by re-tokenizing the text, which, for a single document, is usually fast enough. But looking up the docFreq() of every term in the document is probably too slow.
You can use some heuristics to prune the set of terms, to avoid calling docFreq() too much, or at all. Since you're trying to maximize a tf*idf score, you're probably most interested in terms with a high tf. Choosing a tf threshold even as low as two or three will radically reduce the number of terms under consideration. Another heuristic is that terms with a high idf (i.e., a low df) tend to be longer. So you could threshold the terms by the number of characters, not selecting anything less than, e.g., six or seven characters. With these sorts of heuristics you can usually find small set of, e.g., ten or fewer terms that do a pretty good job of characterizing a document.
It all depends on what you're trying to do. If you're trying to eek out that last percent of precision and recall regardless of computational difficulty so that you can win a TREC competition, then the techniques I mention above are useless. But if you're trying to provide a "more like this" button on a search results page that does a decent job and has good performance, such techniques might be useful.
Depending on the size of your index and the size and makeup of your documents you may want to call the other set methods to control how the similarity queries are generated:
setMinTermFreq(...)
setMinDocFreq(...)
setMinWordLen(...)
setMaxWordLen(...)
setMaxQueryTerms(...)
setFractionTermsToMatch(...)
At rewrite(org.apache.lucene.index.IndexReader) time the reader is used to construct the
actual BooleanQuery containing the relevant terms.
| Field Summary | |
|---|---|
static boolean |
DEFAULT_BOOST_BY_SCORE
Boost terms in query based on score. |
static float |
DEFAULT_FRACTION_TERMS_TO_MATCH
How many terms must match a similar document (fraction, 1.0 means all). |
static int |
DEFAULT_MAX_QUERY_TERMS
Return a Query with no more than this many terms. |
static int |
DEFAULT_MAX_WORD_LENGTH
Ignore words greater than this length or if 0 then this has no effect. |
static int |
DEFAULT_MIN_DOC_FREQ
Ignore words which do not occur in at least this many docs. |
static int |
DEFAULT_MIN_TERM_FREQ
Ignore terms with less than this frequency in the source doc. |
static int |
DEFAULT_MIN_WORD_LENGTH
Ignore words less than this length or if 0 then this has no effect. |
| Constructor Summary | |
|---|---|
MoreLikeThisQuery(String docIdentifier)
Creates a MoreLikeThisQuery instance finding similar documents of a panFMP document ID. |
|
MoreLikeThisQuery(String docIdentifier,
String matchingField)
Creates a MoreLikeThisQuery instance finding similar documents of a panFMP document ID. |
|
| Method Summary | |
|---|---|
boolean |
equals(Object o)
|
float |
getFractionTermsToMatch()
Gets the percentage of terms to match in similar documents. |
int |
getMaxQueryTerms()
Returns the maximum number of query terms that will be included in the rewritten query. |
int |
getMaxWordLen()
Returns the maximum word length above which words will be ignored. |
int |
getMinDocFreq()
Returns the frequency at which words will be ignored which do not occur in at least this many docs. |
int |
getMinTermFreq()
Returns the frequency below which terms will be ignored in the source doc. |
int |
getMinWordLen()
Returns the minimum word length below which words will be ignored. |
int |
hashCode()
|
boolean |
isBoostByScore()
Returns whether to boost terms in query based on "score" or not. |
Query |
rewrite(IndexReader reader)
|
void |
setBoostByScore(boolean boostByScore)
Sets whether to boost terms in query based on "score" or not. |
void |
setFractionTermsToMatch(float fractionTermsToMatch)
Sets the percentage of terms to match in similar documents. |
void |
setMaxQueryTerms(int maxQueryTerms)
Sets the maximum number of query terms that will be included in the rewritten query. |
void |
setMaxWordLen(int maxWordLen)
Sets the maximum word length above which words will be ignored. |
void |
setMinDocFreq(int minDocFreq)
Sets the frequency at which words will be ignored which do not occur in at least this many docs. |
void |
setMinTermFreq(int minTermFreq)
Sets the frequency below which terms will be ignored in the source doc. |
void |
setMinWordLen(int minWordLen)
Sets the minimum word length below which words will be ignored. |
String |
toString(String field)
|
| Methods inherited from class org.apache.lucene.search.Query |
|---|
clone, combine, createWeight, extractTerms, getBoost, getSimilarity, mergeBooleanQueries, setBoost, toString, weight |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int DEFAULT_MIN_TERM_FREQ
getMinTermFreq(),
setMinTermFreq(int),
Constant Field Valuespublic static final int DEFAULT_MIN_DOC_FREQ
getMinDocFreq(),
setMinDocFreq(int),
Constant Field Valuespublic static final int DEFAULT_MIN_WORD_LENGTH
getMinWordLen(),
setMinWordLen(int),
Constant Field Valuespublic static final int DEFAULT_MAX_WORD_LENGTH
getMaxWordLen(),
setMaxWordLen(int),
Constant Field Valuespublic static final int DEFAULT_MAX_QUERY_TERMS
BooleanQuery.getMaxClauseCount(),
getMaxQueryTerms(),
setMaxQueryTerms(int),
Constant Field Valuespublic static final boolean DEFAULT_BOOST_BY_SCORE
isBoostByScore(),
setBoostByScore(boolean),
Constant Field Valuespublic static final float DEFAULT_FRACTION_TERMS_TO_MATCH
getFractionTermsToMatch(),
setFractionTermsToMatch(float),
Constant Field Values| Constructor Detail |
|---|
public MoreLikeThisQuery(String docIdentifier)
public MoreLikeThisQuery(String docIdentifier,
String matchingField)
| Method Detail |
|---|
public int getMinTermFreq()
DEFAULT_MIN_TERM_FREQ.
public void setMinTermFreq(int minTermFreq)
minTermFreq - the frequency below which terms will be ignored in the source doc.public int getMinDocFreq()
DEFAULT_MIN_DOC_FREQ.
public void setMinDocFreq(int minDocFreq)
minDocFreq - the frequency at which words will be ignored which do not occur in at
least this many docs.public boolean isBoostByScore()
setBoostByScore(boolean)public void setBoostByScore(boolean boostByScore)
boostByScore - true to boost terms in query based on "score", false otherwise.
The default is DEFAULT_BOOST_BY_SCORE.isBoostByScore()public int getMinWordLen()
DEFAULT_MIN_WORD_LENGTH.
public void setMinWordLen(int minWordLen)
minWordLen - the minimum word length below which words will be ignored.public int getMaxWordLen()
DEFAULT_MAX_WORD_LENGTH.
public void setMaxWordLen(int maxWordLen)
public int getMaxQueryTerms()
DEFAULT_MAX_QUERY_TERMS.
public void setMaxQueryTerms(int maxQueryTerms)
BooleanQuery.getMaxClauseCount().
public float getFractionTermsToMatch()
public void setFractionTermsToMatch(float fractionTermsToMatch)
DEFAULT_FRACTION_TERMS_TO_MATCH.
public Query rewrite(IndexReader reader)
throws IOException
rewrite in class QueryIOExceptionpublic String toString(String field)
toString in class Querypublic final boolean equals(Object o)
equals in class Querypublic final int hashCode()
hashCode in class Query
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||