-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate Search fails when search string greater than one..
PostPosted: Sat Jul 05, 2008 8:27 am 
Beginner
Beginner

Joined: Mon Aug 13, 2007 2:37 am
Posts: 22
Hi,

I'm need to using Hibernate Search and I wondering why my search fail when I provide a keyword greater than 2.

For example:

This works: "New"
or "York" (each separate)

This does not work: "New York"

Here's my code:
Code:
ADDRESS BEAN
@Entity
@Table(name = "Address")
@Indexed(index="indexes/addresses")
@Analyzer(impl = test.EnglishAnalyzer.class)
public abstract class AbstractAddressCbo  implements java.io.Serializable {
      @Column(name = "CITY_NAME")
@Field(index=Index.TOKENIZED)   
     private String cityName;



Code:
  DAO CLASS
try {
                 

                  FullTextSession fullTextSession = Search.createFullTextSession( session );
                  QueryParser parser = new QueryParser("businessId", new StandardAnalyzer());
                  StringBuffer queryStr = new StringBuffer();

                  queryStr = queryStr.append("businessAddresses.address.cityName:" + criteria_.cityName.trim() + "~ ");                  
                  org.apache.lucene.search.Query query = parser.parse(queryStr.toString());
                  
                   
                  Criteria criteria = fullTextSession.createCriteria( BusinessCbo.class )
                  .setFetchMode( "businessAddresses", FetchMode.JOIN );
                  searchQry = fullTextSession.createFullTextQuery( query ).setCriteriaQuery( criteria );
                                 
                   




We need the search to find a match for more than one word. Example: "New York" Is there something wrong with my configuration?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 05, 2008 10:30 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hello,
configuration look fine but the way you are building the query string is not; you defined the
default field as "businessId", then you force only the first part of name to be contained in the city name, so "New" gets associated to the cityName field, but "York" to the businessId.

look at http://www.hibernate.org/hib_docs/search/reference/en/html_single/#d0e273 for some examples.

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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 05, 2008 2:14 pm 
Beginner
Beginner

Joined: Mon Aug 13, 2007 2:37 am
Posts: 22
I have a multi-JOIN query and I don't understand the concept of specifying a default field in the QueryParser constructor. Since I'm new to using Hibernate Search all the example I see have the table primary key as the default field.

Next, I don't think that's the immediate problem I'm having, because when I comment out the "city" query and decide to do a search on any other fields like "state" and "business" in combination, it works. Or is it because both fields also have one work like "NY" and "Walmart"?

Here's the while query I have that I found out that the city parameter was the problem:

Code:
                  FullTextSession fullTextSession = Search.createFullTextSession( session );
                  QueryParser parser = new QueryParser("businessId", new StandardAnalyzer());
                  StringBuffer queryStr = new StringBuffer();

                  queryStr = queryStr.append("businessAddresses.address.cityName:" + criteria_.cityName.trim() + "~ ");
                  queryStr = queryStr.append(" AND businessAddresses.address.state:" + criteria_.stateName);
                  queryStr = queryStr.append(" AND businessServiceCategories.serviceCategory.serviceCategoryId:" + criteria_.categoryCode );
                  queryStr = queryStr.append(" AND ( businessName:" + keywords + "~");

                  queryStr = queryStr.append(" OR businessServices.service.serviceName:" + keywords + "~");
                  queryStr = queryStr.append(" OR businessSubCategories.serviceSubCategory.serviceSubCategoryName:" + keywords + "~");
                  queryStr = queryStr.append(" ) ");
                  
                  org.apache.lucene.search.Query query = parser.parse(queryStr.toString());
                  



I've been banging my head for days on this. So Pls pls...tell me how I can rewrite this query to recognize keywords that contain more than one word. Does it have anything to do with the Analyzer that I'm chosen?

Thanks in advance...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 05, 2008 9:26 pm 
Beginner
Beginner

Joined: Mon Aug 13, 2007 2:37 am
Posts: 22
Looks like using hibernate proximity search may have "temporary" solved the problem.

If I changed my code to:
Code:
queryStr = queryStr.append("businessAddresses.address.cityName:\"" + criteria_.cityName.trim() + "\"~ ");


Or
Code:
queryStr = queryStr.append("businessAddresses.address.cityName:\"" + criteria_.cityName.trim() + "\"~2 ");


The query works. However, non of them handles a situation where a user misspells a word or a phrase. Which is the main reason we were hoping Hibernate Search can solve our problem.

How can we change our query to find the following:

1. city = "Sherman Oaks"
2. city = "Shurman Oak"
3. business Name = "Toys of the World"
4. business Name = "Toyz of the Word"

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 06, 2008 5:48 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you're doing something wrong with the quotation marks;
I would recommend to use Lucene's API to build the Queries:

Code:
Query fuzzyQuery = new FuzzyQuery(new Term("businessAddresses.address.cityName", cityName));
Query stateQuery = new TermQuery(new Term("businessAddresses.address.state", stateName));
BooleanQuery andQuery = new BooleanQuery();
andQuery.add(fuzzyQuery, BooleanClause.Occur.MUST);
andQuery.add(stateQuery, BooleanClause.Occur.MUST);

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 8:08 am 
Beginner
Beginner

Joined: Mon Aug 13, 2007 2:37 am
Posts: 22
I changed the QueryParser function to BooleanParser and now I'm getting no results AT ALL!

Here's my old code:
Code:
                  queryStr = queryStr.append("businessAddresses.address.cityName:\"" + criteria_.cityName.trim() + "\"~ ");
                  queryStr = queryStr.append(" AND businessAddresses.address.state:" + criteria_.stateName);
                  //queryStr = queryStr.append(" AND businessAccountStatuses.accountStatus:" + PBConstants.ACTIVE );
                  queryStr = queryStr.append(" AND businessServiceCategories.serviceCategory.serviceCategoryId:" + criteria_.categoryCode );
                  queryStr = queryStr.append(" AND ( businessName:" + keywords + "~");
                  queryStr = queryStr.append(" OR businessServices.service.serviceName:" + keywords + "~");
                  queryStr = queryStr.append(" OR businessSubCategories.serviceSubCategory.serviceSubCategoryName:" + keywords + "~");
                  queryStr = queryStr.append(" ) ");
                  
                  
                  org.apache.lucene.search.Query query = parser.parse(queryStr.toString());

                  Criteria criteria = fullTextSession.createCriteria( BusinessCbo.class )
                  .setFetchMode( "businessAddresses", FetchMode.JOIN )
                  .setFetchMode( "businessServiceCategories", FetchMode.JOIN )
                  .setFetchMode( "businessServices", FetchMode.JOIN )
                  .setFetchMode( "businessSubCategories", FetchMode.JOIN )
                  .setFetchMode( "businessAccountStatuses", FetchMode.JOIN );
                  searchQry = fullTextSession.createFullTextQuery( query ).setCriteriaQuery( criteria );





Here's the new code:
Code:
                  PhraseQuery phraseQryBiz = new PhraseQuery();
                  PhraseQuery phraseQrySubCat = new PhraseQuery();
                  PhraseQuery phraseQrySrv = new PhraseQuery();
                  
                  FuzzyQuery fuzzyQryCity = new FuzzyQuery(new Term("businessAddresses.address.cityName", criteria_.cityName.trim()));
                  TermQuery stateQuery = new TermQuery(new Term("businessAddresses.address.state", criteria_.stateName.trim()));
                  TermQuery catQuery = new TermQuery(new Term("businessServiceCategories.serviceCategory.serviceCategoryId", criteria_.categoryCode.trim() ));
                  FuzzyQuery fuzzyQryBiz = new FuzzyQuery(new Term("businessName", keywords.trim() ));
                  FuzzyQuery fuzzyQrySrv = new FuzzyQuery(new Term("businessServices.service.serviceName", keywords.trim() ));
                  FuzzyQuery fuzzyQrySub = new FuzzyQuery(new Term("businessSubCategories.serviceSubCategory.serviceSubCategoryName", keywords.trim() ));
                  
                  BooleanQuery query = new BooleanQuery();
                  query.add(fuzzyQryCity, BooleanClause.Occur.MUST);
                  query.add(stateQuery, BooleanClause.Occur.MUST);
                  query.add(catQuery, BooleanClause.Occur.MUST);
                  query.add(fuzzyQryBiz, BooleanClause.Occur.SHOULD);
                  query.add(fuzzyQrySrv, BooleanClause.Occur.SHOULD);
                  query.add(fuzzyQrySub, BooleanClause.Occur.SHOULD);
                  
                  Criteria criteria = fullTextSession.createCriteria( BusinessCbo.class )
                  .setFetchMode( "businessAddresses", FetchMode.JOIN )
                  .setFetchMode( "businessServiceCategories", FetchMode.JOIN )
                  .setFetchMode( "businessServices", FetchMode.JOIN )
                  .setFetchMode( "businessSubCategories", FetchMode.JOIN )
                  .setFetchMode( "businessAccountStatuses", FetchMode.JOIN );
                  searchQry = fullTextSession.createFullTextQuery( query ).setCriteriaQuery( criteria );



What I'm I doing wrong?


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