-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Boolean operators
PostPosted: Fri May 24, 2013 3:28 am 
Beginner
Beginner

Joined: Mon Nov 26, 2012 4:08 am
Posts: 24
Hello!
I´m sure this has been asked before but I haven't been able to find any answers yet googling.
According to Lucene documentation, you should be able to use + and - as boolean operators. Is this possible to do the same in Hibernate Search? When we try this, the + and - signs are disregarded. Do we have to do the parsing ourselves and use BooleanJunction.bool().must() or something to make it work?
Regards
Andreas


Top
 Profile  
 
 Post subject: Re: Boolean operators
PostPosted: Thu May 30, 2013 4:54 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Andreas,
with Hibernate Search you can either use standard Lucene queries or the Query builder DSL (but this also produces standard Lucene queries).

The "Lucene documentation" you are referring to is likely referring to the Lucene QueryParser; you can use the same and feed the output into Hibernate Search to have it executed.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Boolean operators
PostPosted: Fri May 31, 2013 6:19 am 
Newbie

Joined: Sat Feb 26, 2005 11:45 am
Posts: 13
Location: Stockholm, Sweden
Hi,

I have a very similar (confusing) issue.
Using hibernate-4.2.2 and hibernate-search-4.2.0.

I want my users to enter a searchstring and be able to use boolean operators like +/- or AND/OR/NOT.
This demands that I can build up a query dynamically with must, mustNot and should words and if possible, phrases.

As the code is below it seems to work, eaven though I haven't used any phrases.
If any of the shouldQuery, mustQuery or notQuery in the code is empty or null this will not work.
Am I on the right track?
How do I combine my queries dynamically with this line if one of the mustQuery, notQuery or shouldQuery is empty or null.
booleanJunction = qb.bool().must(mustQuery).must(notQuery).not().should(shouldQuery);

Also, I do now parse the - and + manually myself in a helper class searchWordUtil, should this be neccessary?

If I should use Lucine to 'feed' Hibernate search with queries, how is this possible, do you have any example?

Code:
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
        fullTextEntityManager.createIndexer().startAndWait(); // indexing the h2 database
        QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(EducationMoment.class).get();
        BooleanJunction booleanJunction = null;

        // searchWordUtil.createWordQ retun 3 lists containing the +words, -words and the rest of the words.
        ListMultimap listMultimap = searchWordUtil.createWordQ("aaaaa bbbb cccc -dddd +eeee");

        org.apache.lucene.search.Query shouldQuery = qb
                .keyword().fuzzy().withThreshold(.3f).withPrefixLength(1)
                .onFields("education.desciption","education.name")
                .matching(listMultimap.get(SearchWordUtil.WORD)).createQuery();
        //BooleanJunction booleanJunction1 = qb.bool().should(shouldQuery);

        org.apache.lucene.search.Query mustQuery = qb
                .keyword().fuzzy().withThreshold(.3f).withPrefixLength(1)
                .onField("education.desciption","education.name")
                .matching(listMultimap.get(SearchWordUtil.PLUS)).createQuery();
        //BooleanJunction booleanJunction2 = qb.bool().must(mustQuery);

        org.apache.lucene.search.Query notQuery = qb
                .keyword().fuzzy().withThreshold(.3f).withPrefixLength(1)
                .onFields("education.desciption","education.name")
                .matching(listMultimap.get(SearchWordUtil.MINUS)).createQuery();
        //BooleanJunction booleanJunction3 = qb.bool().must(notQuery).not();

        booleanJunction = qb.bool().must(mustQuery).must(notQuery).not().should(shouldQuery);

        javax.persistence.Query persistenceQuery = fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), EducationMoment.class);

        List result = persistenceQuery.getResultList();
        Assert.assertEquals(1, result.size());


/Mike

_________________
kmike


Top
 Profile  
 
 Post subject: Re: Boolean operators
PostPosted: Sat Jun 08, 2013 10:46 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Mike,
have you seen the utility class org.apache.lucene.queryParser.QueryParser ? It should provide what you need, especially if you use human friendly field names.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.