lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From christian stadler <>
Subject How can I use QueryScorer() to find only perfect matches??
Date Mon, 15 Mar 2010 13:46:38 GMT
Hi there,

I have an issue with the QueryScorer(query) method at the moment and I need
some assistance.
I was indexing my e-book "lucene in action" and based on this index-db I 
started to play around with some boolean queries like:
(contents:+term contents:+query)
As a result I'm expecting as a perfect match for the phrase "term query" four

But when I run my sample to highlight this phrase in the context then I get a
lot more results. It also finds all the matches for "term" and "query"

I think the problem is the QueryScorer() which softens the former exact boolean
Then I was trying the following:
private static Highlighter GetHits(Query query, Formatter formatter)
    string filed = "contents"
    BooleanQuery termsQuery = new BooleanQuery();

    WeightedTerm[] terms = QueryTermExtractor.GetTerms(query, true, field);
    foreach (WeightedTerm term in terms)
        TermQuery termQuery = new TermQuery(new Term(field, term.GetTerm()));
        termsQuery.Add(termQuery, BooleanClause.Occur.MUST);

    // create query scorer based on term queries (field specific)
    QueryScorer scorer = new QueryScorer(termsQuery);

    Highlighter highlighter = new Highlighter(formatter, scorer);
    highlighter.SetTextFragmenter(new SimpleFragmenter(20));

    return highlighter;
to rewrite the query and set the term attribute from SHOULD to MUST

But the result was the same.
Do you have any example how I can use the QueryScorer() in exactly the same way
as to mimic a BooleanSearch??

thanks in advance

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message