Search in sources :

Example 1 with DefaultSimilarity

use of org.apache.lucene.search.DefaultSimilarity in project greplin-lucene-utils by Cue.

the class ConstantQueryNormBoostingQuery method rewrite.

@Override
public Query rewrite(final IndexReader reader) throws IOException {
    BooleanQuery result = new BooleanQuery() {

        @Override
        public Similarity getSimilarity(final Searcher searcher) {
            final Similarity base = searcher.getSimilarity();
            return new DefaultSimilarity() {

                @Override
                public float queryNorm(final float sumOfSquaredWeights) {
                    return base.queryNorm(sumOfSquaredWeights);
                }

                @Override
                public float coord(final int overlap, final int max) {
                    switch(overlap) {
                        case // matched only one clause
                        1:
                            return 1.0f;
                        case // matched both clauses
                        2:
                            return ConstantQueryNormBoostingQuery.this.boost;
                        default:
                            return 0.0f;
                    }
                }
            };
        }
    };
    result.add(this.match, BooleanClause.Occur.MUST);
    result.add(this.context, BooleanClause.Occur.SHOULD);
    return result;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Similarity(org.apache.lucene.search.Similarity) DefaultSimilarity(org.apache.lucene.search.DefaultSimilarity) Searcher(org.apache.lucene.search.Searcher) DefaultSimilarity(org.apache.lucene.search.DefaultSimilarity)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)1 DefaultSimilarity (org.apache.lucene.search.DefaultSimilarity)1 Searcher (org.apache.lucene.search.Searcher)1 Similarity (org.apache.lucene.search.Similarity)1