Author: epbernard
Date: 2010-05-28 12:54:22 -0400 (Fri, 28 May 2010)
New Revision: 19630
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/dsl/v2/impl/ConnectedMultiFieldsPhraseQueryBuilder.java
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java
Log:
HSEARCH-414 Optimize phrase query when the "phrase" is one word
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/dsl/v2/impl/ConnectedMultiFieldsPhraseQueryBuilder.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/dsl/v2/impl/ConnectedMultiFieldsPhraseQueryBuilder.java 2010-05-28
16:44:27 UTC (rev 19629)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/dsl/v2/impl/ConnectedMultiFieldsPhraseQueryBuilder.java 2010-05-28
16:54:22 UTC (rev 19630)
@@ -18,6 +18,7 @@
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.hibernate.annotations.common.AssertionFailure;
@@ -123,27 +124,38 @@
throw new SearchException( "phrase query returns no term. Is there a problem with
your analyzers? " + sentence);
}
//TODO
- //if ( size == 1 ) {
- //optimization
- //}
- //else {
- if (isMultiPhrase) {
- MultiPhraseQuery query = new MultiPhraseQuery();
- query.setSlop( queryContext.getSlop() );
- for ( Map.Entry<Integer,List<Term>> entry : termsPerPosition.entrySet() )
{
- final List<Term> value = entry.getValue();
- query.add( value.toArray( new Term[value.size()] ), entry.getKey() );
+ if ( size == 1 ) {
+ final List<Term> terms = termsPerPosition.values().iterator().next();
+ if ( terms.size() == 1 ) {
+ perFieldQuery = new TermQuery( terms.get( 0 ) );
}
- perFieldQuery = query;
+ else {
+ BooleanQuery query = new BooleanQuery( );
+ for ( Term term : terms ) {
+ query.add( new TermQuery(term), BooleanClause.Occur.SHOULD );
+ }
+ perFieldQuery = query;
+ }
}
else {
- PhraseQuery query = new PhraseQuery();
- query.setSlop( queryContext.getSlop() );
- for ( Map.Entry<Integer,List<Term>> entry : termsPerPosition.entrySet() )
{
- final List<Term> value = entry.getValue();
- query.add( value.get(0), entry.getKey() );
+ if (isMultiPhrase) {
+ MultiPhraseQuery query = new MultiPhraseQuery();
+ query.setSlop( queryContext.getSlop() );
+ for ( Map.Entry<Integer,List<Term>> entry : termsPerPosition.entrySet() )
{
+ final List<Term> value = entry.getValue();
+ query.add( value.toArray( new Term[value.size()] ), entry.getKey() );
+ }
+ perFieldQuery = query;
}
- perFieldQuery = query;
+ else {
+ PhraseQuery query = new PhraseQuery();
+ query.setSlop( queryContext.getSlop() );
+ for ( Map.Entry<Integer,List<Term>> entry : termsPerPosition.entrySet() )
{
+ final List<Term> value = entry.getValue();
+ query.add( value.get(0), entry.getKey() );
+ }
+ perFieldQuery = query;
+ }
}
return fieldContext.getFieldCustomizer().setWrappedQuery( perFieldQuery
).createQuery();
}
Modified:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java
===================================================================
---
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java 2010-05-28
16:44:27 UTC (rev 19629)
+++
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java 2010-05-28
16:54:22 UTC (rev 19630)
@@ -311,7 +311,7 @@
.sentence( "colder and whitening" )
.createQuery();
- assertEquals( 1, fts.createFullTextQuery( query, Month.class ).getResultSize() );
+ assertEquals( "test exact phrase", 1, fts.createFullTextQuery( query,
Month.class ).getResultSize() );
query = monthQb.
phrase()
@@ -319,7 +319,7 @@
.sentence( "Month whitening" )
.createQuery();
- assertEquals( 0, fts.createFullTextQuery( query, Month.class ).getResultSize() );
+ assertEquals( "test slop", 0, fts.createFullTextQuery( query, Month.class
).getResultSize() );
query = monthQb.
phrase()
@@ -328,8 +328,18 @@
.sentence( "Month whitening" )
.createQuery();
- assertEquals( 1, fts.createFullTextQuery( query, Month.class ).getResultSize() );
+ assertEquals( "test slop", 1, fts.createFullTextQuery( query, Month.class
).getResultSize() );
+ query = monthQb.
+ phrase()
+ .onField( "mythology" )
+ .sentence( "whitening" )
+ .createQuery();
+
+ assertEquals( "test one term optimization", 1, fts.createFullTextQuery(
query, Month.class ).getResultSize() );
+
+
+
//Does not work as the NGram filter does not seem to be skipping posiional increment
between ngrams.
// query = monthQb
// .phrase()
Show replies by date