| 
 | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.apache.lucene.search.similar.MoreLikeThis
public final class MoreLikeThis
Generate "more like this" similarity queries. 
 Based on this mail:
 
 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.
 
 An efficient, effective "more-like-this" query generator would be a great contribution, if
 anyone's interested.  I'd imagine that it would take a Reader or a String (the document's
 text), analyzer Analyzer, and return a set of representative terms using heuristics like those
 above.  The frequency and length thresholds could be parameters, etc.
 
 Doug
 
main() below in the source for real code, or
 if you want pseudo code, the simplest possible usage is as follows. The bold
 fragment is specific to this class.
 
 IndexReader ir = ...
 IndexSearcher is = ...
 
 MoreLikeThis mlt = new MoreLikeThis(ir);
 Reader target = ... // orig source of doc you want to find similarities to
 Query query = mlt.like( target);
 
 Hits hits = is.search(query);
 // now the usual iteration thru 'hits' - the only thing to watch for is to make sure
 you ignore the doc if it matches your 'target' document, as it should be similar to itself 
 
setFieldNames(...) so you can examine
 multiple fields (e.g. body and title) for similarity.
 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(...)
 setMaxDocFreq(...)
 setMaxDocFreqPct(...)
 setMinWordLen(...)
 setMaxWordLen(...)
 setMaxQueryTerms(...)
 setMaxNumTokensParsed(...)
 setStopWord(...) 
 
 Changes: Mark Harwood 29/02/04
 Some bugfixing, some refactoring, some optimisation.
  - bugfix: retrieveTerms(int docNum) was not working for indexes without a termvector -added missing code
  - bugfix: No significant terms being created for fields with a termvector - because 
            was only counting one occurrence per term/field pair in calculations(ie not including frequency info from TermVector) 
  - refactor: moved common code into isNoiseWord()
  - optimise: when no termvector support available - used maxNumTermsParsed to limit amount of tokenization
 
| Field Summary | |
|---|---|
| static Analyzer | DEFAULT_ANALYZERDefault analyzer to parse source doc with. | 
| static boolean | DEFAULT_BOOSTBoost terms in query based on score. | 
| static String[] | DEFAULT_FIELD_NAMESDefault field names. | 
| static int | DEFAULT_MAX_DOC_FREQIgnore words which occur in more than this many docs. | 
| static int | DEFAULT_MAX_NUM_TOKENS_PARSEDDefault maximum number of tokens to parse in each example doc field that is not stored with TermVector support. | 
| static int | DEFAULT_MAX_QUERY_TERMSReturn a Query with no more than this many terms. | 
| static int | DEFAULT_MAX_WORD_LENGTHIgnore words greater than this length or if 0 then this has no effect. | 
| static int | DEFAULT_MIN_DOC_FREQIgnore words which do not occur in at least this many docs. | 
| static int | DEFAULT_MIN_TERM_FREQIgnore terms with less than this frequency in the source doc. | 
| static int | DEFAULT_MIN_WORD_LENGTHIgnore words less than this length or if 0 then this has no effect. | 
| static Set<?> | DEFAULT_STOP_WORDSDefault set of stopwords. | 
| Constructor Summary | |
|---|---|
| MoreLikeThis(IndexReader ir)Constructor requiring an IndexReader. | |
| MoreLikeThis(IndexReader ir,
             Similarity sim) | |
| Method Summary | |
|---|---|
|  String | describeParams()Describe the parameters that control how the "more like this" query is formed. | 
|  Analyzer | getAnalyzer()Returns an analyzer that will be used to parse source doc with. | 
|  float | getBoostFactor()Returns the boost factor used when boosting terms | 
|  String[] | getFieldNames()Returns the field names that will be used when generating the 'More Like This' query. | 
|  int | getMaxDocFreq()Returns the maximum frequency in which words may still appear. | 
|  int | getMaxNumTokensParsed() | 
|  int | getMaxQueryTerms()Returns the maximum number of query terms that will be included in any generated 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. | 
|  Similarity | getSimilarity() | 
|  Set<?> | getStopWords()Get the current stop words being used. | 
|  boolean | isBoost()Returns whether to boost terms in query based on "score" or not. | 
|  Query | like(File f)Return a query that will return docs like the passed file. | 
|  Query | like(InputStream is)Return a query that will return docs like the passed stream. | 
|  Query | like(int docNum)Return a query that will return docs like the passed lucene document ID. | 
|  Query | like(Reader r)Return a query that will return docs like the passed Reader. | 
|  Query | like(URL u)Return a query that will return docs like the passed URL. | 
| static void | main(String[] a)Test driver. | 
|  String[] | retrieveInterestingTerms(int docNum) | 
|  String[] | retrieveInterestingTerms(Reader r)Convenience routine to make it easy to return the most interesting words in a document. | 
|  PriorityQueue<Object[]> | retrieveTerms(int docNum)Find words for a more-like-this query former. | 
|  PriorityQueue<Object[]> | retrieveTerms(Reader r)Find words for a more-like-this query former. | 
|  void | setAnalyzer(Analyzer analyzer)Sets the analyzer to use. | 
|  void | setBoost(boolean boost)Sets whether to boost terms in query based on "score" or not. | 
|  void | setBoostFactor(float boostFactor)Sets the boost factor to use when boosting terms | 
|  void | setFieldNames(String[] fieldNames)Sets the field names that will be used when generating the 'More Like This' query. | 
|  void | setMaxDocFreq(int maxFreq)Set the maximum frequency in which words may still appear. | 
|  void | setMaxDocFreqPct(int maxPercentage)Set the maximum percentage in which words may still appear. | 
|  void | setMaxNumTokensParsed(int i) | 
|  void | setMaxQueryTerms(int maxQueryTerms)Sets the maximum number of query terms that will be included in any generated 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. | 
|  void | setSimilarity(Similarity similarity) | 
|  void | setStopWords(Set<?> stopWords)Set the set of stopwords. | 
| Methods inherited from class java.lang.Object | 
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | 
| Field Detail | 
|---|
public static final int DEFAULT_MAX_NUM_TOKENS_PARSED
getMaxNumTokensParsed(), 
Constant Field Valuespublic static final Analyzer DEFAULT_ANALYZER
getAnalyzer()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_MAX_DOC_FREQ
getMaxDocFreq(), 
setMaxDocFreq(int), 
setMaxDocFreqPct(int), 
Constant Field Valuespublic static final boolean DEFAULT_BOOST
isBoost(), 
setBoost(boolean), 
Constant Field Valuespublic static final String[] DEFAULT_FIELD_NAMES
public 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 Set<?> DEFAULT_STOP_WORDS
setStopWords(java.util.Set>), 
getStopWords()public static final int DEFAULT_MAX_QUERY_TERMS
BooleanQuery.getMaxClauseCount(), 
getMaxQueryTerms(), 
setMaxQueryTerms(int), 
Constant Field Values| Constructor Detail | 
|---|
public MoreLikeThis(IndexReader ir)
public MoreLikeThis(IndexReader ir,
                    Similarity sim)
| Method Detail | 
|---|
public float getBoostFactor()
public void setBoostFactor(float boostFactor)
boostFactor - public Similarity getSimilarity()
public void setSimilarity(Similarity similarity)
public Analyzer getAnalyzer()
DEFAULT_ANALYZER.
DEFAULT_ANALYZERpublic void setAnalyzer(Analyzer analyzer)
like(int) method, all other 'like' methods require an analyzer.
analyzer - the analyzer to use to tokenize text.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 int getMaxDocFreq()
DEFAULT_MAX_DOC_FREQ.
public void setMaxDocFreq(int maxFreq)
maxFreq - the maximum count of documents that a term may appear 
            in to be still considered relevantpublic void setMaxDocFreqPct(int maxPercentage)
maxPercentage - the maximum percentage of documents (0-100) that a term may appear 
            in to be still considered relevantpublic boolean isBoost()
DEFAULT_BOOST.
setBoost(boolean)public void setBoost(boolean boost)
boost - true to boost terms in query based on "score", false otherwise.isBoost()public String[] getFieldNames()
DEFAULT_FIELD_NAMES.
public void setFieldNames(String[] fieldNames)
fieldNames - the field names that will be used when generating the 'More Like This'
 query.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)
maxWordLen - the maximum word length above which words will be ignored.public void setStopWords(Set<?> stopWords)
stopWords - set of stopwords, if null it means to allow stop wordsStopFilter.makeStopSet(), 
getStopWords()public Set<?> getStopWords()
setStopWords(java.util.Set>)public int getMaxQueryTerms()
DEFAULT_MAX_QUERY_TERMS.
public void setMaxQueryTerms(int maxQueryTerms)
maxQueryTerms - the maximum number of query terms that will be included in any
 generated query.public int getMaxNumTokensParsed()
DEFAULT_MAX_NUM_TOKENS_PARSEDpublic void setMaxNumTokensParsed(int i)
i - The maximum number of tokens to parse in each example doc field that is not stored with TermVector support
public Query like(int docNum)
           throws IOException
docNum - the documentID of the lucene doc to generate the 'More Like This" query for.
IOException
public Query like(File f)
           throws IOException
IOException
public Query like(URL u)
           throws IOException
IOException
public Query like(InputStream is)
           throws IOException
IOException
public Query like(Reader r)
           throws IOException
IOExceptionpublic String describeParams()
public static void main(String[] a)
                 throws Throwable
Throwable
public PriorityQueue<Object[]> retrieveTerms(int docNum)
                                      throws IOException
docNum - the id of the lucene document from which to find terms
IOException
public PriorityQueue<Object[]> retrieveTerms(Reader r)
                                      throws IOException
retrieveInterestingTerms().
r - the reader that has the content of the document
IOExceptionretrieveInterestingTerms(int)
public String[] retrieveInterestingTerms(int docNum)
                                  throws IOException
IOExceptionretrieveInterestingTerms(java.io.Reader)
public String[] retrieveInterestingTerms(Reader r)
                                  throws IOException
retrieveTerms() directly.
r - the source document
IOExceptionretrieveTerms(java.io.Reader), 
setMaxQueryTerms(int)| 
 | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||