Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search with multiple parameters in Hibernate Search 3.0.0.ga

Using:

  • Hibernate 3.2.7.ga
  • Hibernate-Search 3.0.0.ga
  • Hibernate-Anotations 3.3.0.ga
  • Hibernate-Commons-Cnnotations 3.0.0.ga
  • Lucene-Core 2.9.4
  • Lucene-Analyzers 2.9.4
  • Lucene-Queryparser 2.9.4

How can search with multiple parameters like:

SELECT * 
FROM example 
WHERE column1 = "text1"
AND (column2 = "text2" OR column2 ="text3")

With Hibernate-Search documentation I only found that example of searching:

Session session = universalHibernateDAO.getHibernateSession();
FullTextSession fullTextSession = Search.createFullTextSession(session);

Transaction tx = fullTextSession.beginTransaction();
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_29,
    new String[]{"summary", "body"}, analyzer);
org.apache.lucene.search.Query query = parser.parse( "Java rocks!" );
Query hibQuery = fullTextSession.createFullTextQuery( query, PscpExpedient.class );
List<PscpExpedient> result = (List<PscpExpedient>)hibQuery.list();
      
tx.commit();

Will be perfect if exist some way to do something like:

org.apache.lucene.search.Query query = parser.parse("column1: text1", 
                                                      "column2: text2 | text3" );

Also, if that is possible, will be nice to know how search "onlyOneword" and "text with multiple spaces"

Thanks!

like image 388
Jonatan H. Avatar asked Nov 18 '25 14:11

Jonatan H.


1 Answers

Try to use BooleanQuery like this:

org.apache.lucene.search.Query query1 = ....;
org.apache.lucene.search.Query query2 = ....;
org.apache.lucene.search.Query query3 = ....;

BooleanQuery booleanQuery = new BooleanQuery();

luceneBooleanQuery.add(query1, BooleanClause.Occur.MUST);
luceneBooleanQuery.add(query2, BooleanClause.Occur.SHOULD);
luceneBooleanQuery.add(query3, BooleanClause.Occur.SHOULD); 

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(booleanQuery, DomainClass.class);

It should work.

like image 153
Bilal BBB Avatar answered Nov 21 '25 02:11

Bilal BBB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!