Skip to content

Commit

Permalink
HSEARCH-2208 Replace Filter with Query in the Query DSL
Browse files Browse the repository at this point in the history
Because Filter has been deprecated in favor of Query and will be removed.
  • Loading branch information
yrodiere authored and Sanne committed May 9, 2017
1 parent 826a407 commit 08413b5
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 35 deletions.
Expand Up @@ -8,6 +8,7 @@
package org.hibernate.search.query.dsl;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;

/**
* Operations common to all types of queries
Expand Down Expand Up @@ -36,11 +37,11 @@ public interface QueryCustomization<T> {
T withConstantScore();

/**
* Filter the query results with the Filter instance
* @param filter the filter to use
* Filter the query results with the given Query instance
* @param filter the Query to use as a filter
* @return an instance of T for method chaining
*/
T filteredBy(Filter filter);
T filteredBy(Query filter);

//TODO filter(String) + parameters
}
Expand Up @@ -13,7 +13,6 @@
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery.Builder;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;

Expand Down Expand Up @@ -99,7 +98,7 @@ public MustJunction withConstantScore() {
}

@Override
public MustJunction filteredBy(Filter filter) {
public MustJunction filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -12,7 +12,6 @@

import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;

Expand Down Expand Up @@ -68,7 +67,7 @@ public AllContext withConstantScore() {
}

@Override
public AllContext filteredBy(Filter filter) {
public AllContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,8 +7,8 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;

import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.FuzzyContext;
import org.hibernate.search.query.dsl.TermMatchingContext;

Expand Down Expand Up @@ -67,7 +67,7 @@ public FuzzyContext withConstantScore() {
}

@Override
public FuzzyContext filteredBy(Filter filter) {
public FuzzyContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,8 +7,7 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;

import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.MoreLikeThisContext;
import org.hibernate.search.query.dsl.MoreLikeThisOpenedMatchingContext;
import org.hibernate.search.query.dsl.MoreLikeThisTerminalMatchingContext;
Expand Down Expand Up @@ -73,7 +72,7 @@ public MoreLikeThisContext withConstantScore() {
}

@Override
public MoreLikeThisContext filteredBy(Filter filter) {
public MoreLikeThisContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,8 +7,8 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;

import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.PhraseContext;
import org.hibernate.search.query.dsl.PhraseMatchingContext;

Expand Down Expand Up @@ -51,7 +51,7 @@ public PhraseContext withConstantScore() {
}

@Override
public PhraseContext filteredBy(Filter filter) {
public PhraseContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,8 +7,8 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;

import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.RangeContext;
import org.hibernate.search.query.dsl.RangeMatchingContext;

Expand Down Expand Up @@ -42,7 +42,7 @@ public RangeContext withConstantScore() {
}

@Override
public RangeContext filteredBy(Filter filter) {
public RangeContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,7 +7,7 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.SimpleQueryStringContext;
import org.hibernate.search.query.dsl.SimpleQueryStringMatchingContext;

Expand Down Expand Up @@ -49,7 +49,7 @@ public ConnectedSimpleQueryStringContext withConstantScore() {
}

@Override
public ConnectedSimpleQueryStringContext filteredBy(Filter filter) {
public ConnectedSimpleQueryStringContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -6,8 +6,7 @@
*/
package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;

import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.SpatialContext;
import org.hibernate.search.query.dsl.SpatialMatchingContext;
import org.hibernate.search.query.dsl.Unit;
Expand Down Expand Up @@ -49,7 +48,7 @@ public SpatialContext withConstantScore() {
}

@Override
public SpatialContext filteredBy(Filter filter) {
public SpatialContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,11 +7,10 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;

import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.FuzzyContext;
import org.hibernate.search.query.dsl.TermMatchingContext;
import org.hibernate.search.query.dsl.TermContext;
import org.hibernate.search.query.dsl.TermMatchingContext;
import org.hibernate.search.query.dsl.WildcardContext;

/**
Expand Down Expand Up @@ -61,7 +60,7 @@ public ConnectedTermContext withConstantScore() {
}

@Override
public ConnectedTermContext filteredBy(Filter filter) {
public ConnectedTermContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,7 +7,7 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.hibernate.search.query.dsl.TermMatchingContext;
import org.hibernate.search.query.dsl.WildcardContext;

Expand Down Expand Up @@ -48,7 +48,7 @@ public WildcardContext withConstantScore() {
}

@Override
public WildcardContext filteredBy(Filter filter) {
public WildcardContext filteredBy(Query filter) {
queryCustomizer.filteredBy( filter );
return this;
}
Expand Down
Expand Up @@ -7,12 +7,10 @@

package org.hibernate.search.query.dsl.impl;

import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.FilteredQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;

import org.hibernate.search.exception.AssertionFailure;
import org.hibernate.search.query.dsl.QueryCustomization;

Expand All @@ -23,7 +21,7 @@ class QueryCustomizer implements QueryCustomization<QueryCustomizer> {
private float boost = 1f;
private boolean constantScore;
private Query wrappedQuery;
private Filter filter;
private Query filter;

@Override
public QueryCustomizer boostedTo(float boost) {
Expand All @@ -38,7 +36,7 @@ public QueryCustomizer withConstantScore() {
}

@Override
public QueryCustomizer filteredBy(Filter filter) {
public QueryCustomizer filteredBy(Query filter) {
this.filter = filter;
return this;
}
Expand All @@ -60,10 +58,13 @@ public Query createQuery() {
}
finalQuery.setBoost( boost * finalQuery.getBoost() );
if ( filter != null ) {
finalQuery = new FilteredQuery( finalQuery, filter );
finalQuery = new BooleanQuery.Builder()
.add( finalQuery, Occur.MUST )
.add( filter, Occur.FILTER )
.build();
}
if ( constantScore ) {
finalQuery = new ConstantScoreQuery( new QueryWrapperFilter( finalQuery ) );
finalQuery = new ConstantScoreQuery( finalQuery );
}
return finalQuery;
}
Expand Down

0 comments on commit 08413b5

Please sign in to comment.