• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

hibernate lucene search

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to search for a set of keywords in a set of columns for a set of rows, for all these I am using a loop.. I am trying to create a query like this:
(masterid=1 && keyword="key1" or keyword="key2")) or (masterid=2 && (keyword="key3" or keyword="key4")) or (masterid=3 && (keyword="key5" or keyword="key6"))

My code goes like this :
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(EntityType.entityMap.get(entityType.getName())).get();

TermMatchingContext tmc = queryBuilder.keyword().onFields(fieldArray);
RangeMatchingContext rmc = queryBuilder.range().onField("rssMaster.id");
for (RSSConfigure rssConf : listRSSConf) {
String searchKey = rssConf.getFilterKey()==null?"":rssConf.getFilterKey();
String[] keywords = searchKey.split(RcmConstants.RSS_FILTERKEY_SEPARATOR);

BooleanQuery keywordQuery = new BooleanQuery();

for (String keyword : keywords) {
keywordQuery.add(tmc.matching(keyword).createQuery() , BooleanClause.Occur.SHOULD);
}

Query masterIdRangeQuery = rmc.from(rssConf.getRssMaster().getId()).to(rssConf.getRssMaster().getId()).createQuery();

luceneQuery1.add(masterIdRangeQuery, BooleanClause.Occur.MUST) ;
luceneQuery1.add(keywordQuery, BooleanClause.Occur.MUST) ;

}

logger.debug("lucene1 >>>>>>>>"+luceneQuery1);

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery1, EntityType.entityMap.get(entityType.getName()));

org.apache.lucene.search.Sort sort = new Sort(new SortField(sortByField, SortField.STRING, sortOrder.equalsIgnoreCase("asc") ? false : true ));
fullTextQuery.setSort(sort);


listEntities = fullTextQuery.list();

This gives me results for the query like this :
(masterid=1 && keyword="key1" or keyword="key2")) && (masterid=2 && (keyword="key3" or keyword="key4")) && (masterid=3 && (keyword="key5" or keyword="key6"))

Can anybody spot the mistake here ?
I know somewhere I have to use SHOULD clause to combine these queries in the outer most loop of listRSSConf.. but where exactly.. I am confused.

Please help ..
 
Shilpa Ratnaparkhi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to resolve the issue .. I added the or clause like this -- refer the last line:

for (RSSConfigure rssConf : listRSSConf) {
BooleanQuery luceneQuery1 = new BooleanQuery();

String searchKey = rssConf.getFilterKey()==null?"":rssConf.getFilterKey();
String[] keywords = searchKey.split("#");

BooleanQuery keywordQuery = new BooleanQuery();

for (String keyword : keywords) {
keywordQuery.add(tmc.matching(keyword).createQuery() , BooleanClause.Occur.SHOULD);
}

Query masterIdRangeQuery = rmc.from(rssConf.getRssMaster().getId()).to(rssConf.getRssMaster().getId()).createQuery();

luceneQuery1.add(masterIdRangeQuery, BooleanClause.Occur.MUST) ;
luceneQuery1.add(keywordQuery, BooleanClause.Occur.MUST) ;

queryList.add(luceneQuery1);
}

for (BooleanQuery qry : queryList) {
luceneQuery.add(qry, BooleanClause.Occur.SHOULD);
}
 
Shilpa Ratnaparkhi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shilpa Ratnaparkhi wrote:I have to search for a set of keywords in a set of columns for a set of rows, for all these I am using a loop.. I am trying to create a query like this:
(masterid=1 && keyword="key1" or keyword="key2")) or (masterid=2 && (keyword="key3" or keyword="key4")) or (masterid=3 && (keyword="key5" or keyword="key6"))

My code goes like this :
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(RSS.class).get();

TermMatchingContext tmc = queryBuilder.keyword().onFields(fieldArray);
RangeMatchingContext rmc = queryBuilder.range().onField("rssMaster.id");
for (RSSConfigure rssConf : listRSSConf) {
String searchKey = rssConf.getFilterKey()==null?"":rssConf.getFilterKey();
String[] keywords = searchKey.split("#");

BooleanQuery keywordQuery = new BooleanQuery();

for (String keyword : keywords) {
keywordQuery.add(tmc.matching(keyword).createQuery() , BooleanClause.Occur.SHOULD);
}

Query masterIdRangeQuery = rmc.from(rssConf.getRssMaster().getId()).to(rssConf.getRssMaster().getId()).createQuery();

luceneQuery1.add(masterIdRangeQuery, BooleanClause.Occur.MUST) ;
luceneQuery1.add(keywordQuery, BooleanClause.Occur.MUST) ;

}

logger.debug("lucene1 >>>>>>>>"+luceneQuery1);

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery1, RSS.class);

org.apache.lucene.search.Sort sort = new Sort(new SortField(sortByField, SortField.STRING, sortOrder.equalsIgnoreCase("asc") ? false : true ));
fullTextQuery.setSort(sort);


listEntities = fullTextQuery.list();

This gives me results for the query like this :
(masterid=1 && keyword="key1" or keyword="key2")) && (masterid=2 && (keyword="key3" or keyword="key4")) && (masterid=3 && (keyword="key5" or keyword="key6"))

Can anybody spot the mistake here ?
I know somewhere I have to use SHOULD clause to combine these queries in the outer most loop of listRSSConf.. but where exactly.. I am confused.

Please help ..

 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic