-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to Include Hyphen in Hibernate Search Query?
PostPosted: Tue Apr 19, 2011 6:15 pm 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
Does anyone know how to avoid queries being split on hyphens? My queries are being split on hyphens resulting in the following lucene query:
Quote:
+(topic:news -topic:international +topic:news -topic:domestic)

However, I want:
Quote:
+(topic:news - international topic:news - domestic)


To build the query I put together a convenience method (see below) using hibernate search 3.4.0.Final.
Code:
public org.apache.lucene.search.Query buildLuceneQuery(String[] queries, String[] fields, BooleanClause.Occur[] clauses) {
        try {                   
           org.apache.lucene.search.Query luceneQuery = MultiFieldQueryParser.parse(Version.LUCENE_31, queries, fields, clauses, new StandardAnalyzer(Version.LUCENE_31));
            LOGGER.debug("Parsed lucene search query: {}", luceneQuery.toString());
            return luceneQuery;
        } catch (Exception e) {
           LOGGER.error("Unable to parse lucene search query: {}", StringUtils.join(queries, ", "));
            throw new RuntimeException("Unable to parse multifield query", e);
        }
    }


Top
 Profile  
 
 Post subject: Re: How to Include Hyphen in Hibernate Search Query?
PostPosted: Wed Apr 20, 2011 4:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
There are several approaches to this problem, depending on your use-case. Are this user entered queries? If not and you are creating this queries programmatically in your code I would consider using the programmatic query API instead of using a query parser.

If you need the query parser you need to escape the '-', because it is alternative way to specify NOT terms. See also the Lucene query parser syntax - http://lucene.apache.org/java/3_1_0/que ... yntax.html

Last, but not least your search results will heavily depend on how your data is indexed. Is "news - international" indexed as two tokens "news" and "international" or is this maybe an un-tokenised field in which case you would have to use a phrase query to get a hit.

--Hardy


Top
 Profile  
 
 Post subject: Re: How to Include Hyphen in Hibernate Search Query?
PostPosted: Sat Apr 23, 2011 8:29 pm 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
hardy.ferentschik wrote:
There are several approaches to this problem, depending on your use-case. Are this user entered queries? If not and you are creating this queries programmatically in your code I would consider using the programmatic query API instead of using a query parser.


The queries are user generated.

hardy.ferentschik wrote:
If you need the query parser you need to escape the '-', because it is alternative way to specify NOT terms. See also the Lucene query parser syntax - http://lucene.apache.org/java/3_1_0/que ... yntax.html


Since the queries are user generated, I'll have to scan and escape the "-"; do you have any suggestions on how best to do so?

hardy.ferentschik wrote:
Last, but not least your search results will heavily depend on how your data is indexed. Is "news - international" indexed as two tokens "news" and "international" or is this maybe an un-tokenised field in which case you would have to use a phrase query to get a hit.


The field is un-tokenised; so are you saying I need to ensure the user generated query is enclosed by double quotes before passing it to the multifield parser?

Thanks.

-Dan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.