Skip to content

Commit

Permalink
option to ignore empty query for analyzers with stop words
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrignon committed Oct 18, 2016
1 parent 1b188c5 commit 3ec93ad
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 44 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,16 @@ MyDomainClass.search().list {
}
```

### Options

```groovy
grails.plugins.hibernatesearch = {
rebuildIndexOnStart false // see related section above
throwOnEmptyQuery false // throw or not exception when Hibernate Search raises an EmptyQueryException
fullTextFilter /* ... */ // see related section above
}
```

## Change log

### v2.0.2
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}
}

version "2.0.2"
version "2.0.3"
group "org.grails.plugins"

apply plugin:"eclipse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class HibernateSearchConfig {
private final FullTextSession fullTextSession
private static final List MASS_INDEXER_METHODS = MassIndexer.methods.findAll { it.returnType == MassIndexer }*.name

boolean throwExceptionOnEmptyQuery

HibernateSearchConfig( Session session ) {
this.fullTextSession = Search.getFullTextSession( session )
}
Expand Down Expand Up @@ -55,6 +57,16 @@ class HibernateSearchConfig {

massIndexer = fullTextSession.createIndexer().startAndWait()
}

/**
* Throws exception if Hibernate Search raises an EmptyQueryException, (could occur if analyzer has stop words) default false
*/
def throwOnEmptyQuery( boolean throwException ) {

log.debug "throwExceptionOnEmptyQuery = " + throwException

throwExceptionOnEmptyQuery = throwException
}

Object invokeMethod( String name, Object args ) {
if ( name in MASS_INDEXER_METHODS ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class HibernateSearchGrailsPlugin extends Plugin {

private final static Logger log = LoggerFactory.getLogger(this)

public static HibernateSearchConfig pluginConfig;

def grailsVersion = "3.1.12 > *"

def profiles = ['web']
Expand Down Expand Up @@ -70,12 +72,12 @@ class HibernateSearchGrailsPlugin extends Plugin {
log.info "* " + clazz.getSimpleName() + " is indexed"
grailsClass.metaClass.static.search = {

new HibernateSearchQueryBuilder( clazz, applicationContext.sessionFactory.getCurrentSession() )
new HibernateSearchQueryBuilder( clazz, applicationContext.sessionFactory.getCurrentSession(), pluginConfig )
}

grailsClass.metaClass.search = {
def instance = delegate
new HibernateSearchQueryBuilder( clazz, instance, applicationContext.sessionFactory.getCurrentSession() )
new HibernateSearchQueryBuilder( clazz, instance, applicationContext.sessionFactory.getCurrentSession(), pluginConfig )
}
}
}
Expand All @@ -88,7 +90,8 @@ class HibernateSearchGrailsPlugin extends Plugin {
log.debug 'hibernatesearch config found'
Session session = applicationContext.sessionFactory.openSession();

hibernateSearchConfig.delegate = new HibernateSearchConfig( session )
pluginConfig = new HibernateSearchConfig( session )
hibernateSearchConfig.delegate = pluginConfig
hibernateSearchConfig.resolveStrategy = Closure.DELEGATE_FIRST
hibernateSearchConfig.call()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.hibernate.search.FullTextQuery
import org.hibernate.search.FullTextSession
import org.hibernate.search.MassIndexer
import org.hibernate.search.Search
import org.hibernate.search.exception.EmptyQueryException;
import org.hibernate.search.query.dsl.FieldCustomization
import org.hibernate.search.query.dsl.QueryBuilder
import org.slf4j.Logger;
Expand Down Expand Up @@ -65,6 +66,38 @@ class HibernateSearchQueryBuilder {
children << component
}

/**
* @return true if composite contains at least one valid (not empty) query
*/
protected boolean forEachQuery( Closure action ) {

boolean notEmpty = false;
if (children) {

for (child in children) {
try {

Query subQuery = child.createQuery();

action.delegate = subQuery
action.resolveStrategy = Closure.DELEGATE_FIRST
action.call(subQuery)
notEmpty = true;

} catch (EmptyQueryException e) {
if (HibernateSearchGrailsPlugin.pluginConfig.throwExceptionOnEmptyQuery) {
throw e
} else {
log.warn 'empty Hibernate search query ignored! ' + child, e
}
}
}

}

return notEmpty
}

def toString( indent ) {
[( "-" * indent ) + this.class.simpleName, children.collect { it.toString( indent + 1 ) }].flatten().findAll {it}.join( "\n" )
}
Expand Down Expand Up @@ -100,54 +133,47 @@ class HibernateSearchQueryBuilder {

private static class MustNotComponent extends Composite {
Query createQuery( ) {
if ( children ) {

def query = queryBuilder.bool()
def query = queryBuilder.bool()
boolean notEmpty = forEachQuery { subQuery ->
query = query.must( subQuery ).not()
}

children*.createQuery().each {
query = query.must( it ).not()
}

query.createQuery()

} else {
queryBuilder.all().createQuery()
}
if (notEmpty) {
return query.createQuery()
} else {
return queryBuilder.all().createQuery()
}

}
}
private static class MustComponent extends Composite {
Query createQuery( ) {
if ( children ) {

def query = queryBuilder.bool()

children*.createQuery().each {
query = query.must( it )
}

query.createQuery()

} else {
queryBuilder.all().createQuery()
}
def query = queryBuilder.bool()
boolean notEmpty = forEachQuery { subQuery ->
query = query.must( subQuery )
}

if (notEmpty) {
return query.createQuery()
} else {
return queryBuilder.all().createQuery()
}
}
}

private static class ShouldComponent extends Composite {
Query createQuery( ) {
if ( children ) {

def query = queryBuilder.bool()

children*.createQuery().each {
query = query.should( it )
}

query.createQuery()

} else {
queryBuilder.all().createQuery()
}
def query = queryBuilder.bool()
boolean notEmpty = forEachQuery { subQuery ->
query = query.should( subQuery )
}

if (notEmpty) {
return query.createQuery()
} else {
return queryBuilder.all().createQuery()
}
}
}

Expand Down Expand Up @@ -219,6 +245,8 @@ class HibernateSearchQueryBuilder {

private static final List MASS_INDEXER_METHODS = MassIndexer.methods.findAll { it.returnType == MassIndexer }*.name

private final HibernateSearchConfig pluginConfig;

private final FullTextSession fullTextSession
private final clazz
private final instance
Expand All @@ -241,15 +269,16 @@ class HibernateSearchQueryBuilder {

Filter filter

HibernateSearchQueryBuilder( clazz, instance, Session session ) {
HibernateSearchQueryBuilder( clazz, instance, Session session, HibernateSearchConfig pluginConfig ) {
this.clazz = clazz
this.fullTextSession = Search.getFullTextSession( session )
this.instance = instance
this.staticContext = instance == null
this.pluginConfig = pluginConfig;
}

HibernateSearchQueryBuilder( clazz, Session session ) {
this( clazz, null, session )
HibernateSearchQueryBuilder( clazz, Session session, HibernateSearchConfig pluginConfig ) {
this( clazz, null, session, pluginConfig )
}

private FullTextQuery createFullTextQuery( ) {
Expand Down

0 comments on commit 3ec93ad

Please sign in to comment.