[hibernate-dev] [HSEARCH] New Query API: last call

Emmanuel Bernard emmanuel at hibernate.org
Wed Jun 2 12:00:20 EDT 2010


Guys, 
I'me now done with the level of abstraction and fluidity I wanted out of the query DSL. Please review before we push that out. Key features:
 - fluent API
 - use the field bridge system: type is passed, not raw string
 - use the analyzer transparently
 - simple use case simple, complex use cases possible

I'm showing below a few examples demonstrating key concepts.

Please comment / ask questions.

Term query
query = monthQb
	.keyword()
	.onField( "monthValue" )
	.matching( 2 ) //note that monthValue is of type int
	.createQuery();

//term query, showing analyzer integration
query = monthQb
	.keyword()
	.onField( "mythology_ngram" )
	.matching( "snobored" ) //we apply the ngram filter here
	.createQuery();

//use fuzzy query
query = monthQb
		.keyword()
			.fuzzy()
				.threshold( .8f ) //optional
				.prefixLength( 1 )  //optional
		.onField( "mythology" )
		.matching( "calder" )
		.createQuery();

//use wildcard queries
monthQb
	.keyword()
		.wildcard()
	.onField( "mythology" )
	.matching( "mon*" )
	.createQuery();

Alternative option
//apply on multiple fields
monthQb.keyword()
	.onField( "mythology" )
		.boostedTo( 30 )
	.andField( "history" )
	.matching( "whitening" )
	.createQuery();

//boost a field
monthQb
	.keyword()
	.onField( "history" )
		.boostedTo( 30 )
	.matching( "whitening" )
	.createQuery();

Range query
//Range query
monthQb
	.range()
		.onField( "estimatedCreation" )
		.from( from ) #from and to are actual java.util.Date. We do the conversion
		.to( to ).exclude()
		.createQuery();

monthQb
	.range()
		.onField( "estimatedCreation" )
		.below( brithDay )
		.createQuery();

Phrase query
monthQb
	.phrase()
		.slop( 1 )
	.onField( "mythology" )
	.sentence( "Month whitening" )
	.createQuery();

Boolean query
monthQb
	.bool()
		.should( monthQb.keyword().onField( "mythology" ).matching( "whitening" ).createQuery() )
		.should( monthQb.keyword().onField( "history" ).matching( "whitening" ).createQuery() )
	.createQuery();

//Boolean query all except (recommended)
monthQb
	.all()
		.except( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() )
	.createQuery();




More information about the hibernate-dev mailing list