Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: hibernate search returns no result
PostPosted: Wed Jun 07, 2017 3:36 pm 
Newbie

Joined: Wed Jun 07, 2017 3:21 pm
Posts: 4
HI all,
I am trying to create a hibernate full text search using hibernate-search-4.5.3.Final.jar
There is no errors in this application, but my Lucene query doesn't return any results.
I mean It doesn't return any of rows in the table.
can any one please help me.

Code:
@Autowired
private SessionFactory sessionFactory;

private FullTextSession fullTextSession;

protected Session getSession() {
   return this.sessionFactory.getCurrentSession();
}

protected FullTextSession getFullTextEntityManager() {
   if (fullTextSession == null) {
      fullTextSession = Search.getFullTextSession(getSession());
   }
   return fullTextSession;
}

public List<Product> searchProduct(String keyWord) {
      
   // This will ensure that index for already inserted data is created.
   try {
      getFullTextEntityManager().createIndexer().startAndWait();
   } catch (InterruptedException e) {
      e.printStackTrace();
   }
      
   // Create a Query Builder
   QueryBuilder queryBuilder = getFullTextEntityManager().getSearchFactory().buildQueryBuilder().forEntity(Product.class).get();

   // Create a Lucene Full Text Query
   org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("nameEn").matching(keyWord).createQuery();

   // Wrap Lucene query in a org.hibernate.Query
   org.hibernate.Query fullTextQuery = getFullTextEntityManager().createFullTextQuery(luceneQuery, Product.class);

   // execute search
   List<Product> result = fullTextQuery.list();

   return result;
}



Code:
@Entity
@Table(name = "MP_PRODUCT")
@Indexed
public class Product implements Serializable {

   @Id
   @DocumentId
   @GenericGenerator(name = "table", strategy = "enhanced-table", parameters = {
         @Parameter(name = "table_name", value = "PRODUCT_SEQ"), @Parameter(name = "initial_value", value = "1"),
         @Parameter(name = "increment_size", value = "1") })
   @GeneratedValue(generator = "table", strategy = GenerationType.TABLE)
   @Column(name = "ID_PRODUCT")
   private Long id;

   @Column(name = "NAME_EN")
   @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   private String nameEn;


Code:
<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.impl.FSDirectoryProvider</prop>
<prop key="hibernate.search.default.indexBase">/app/lucene/indexes</prop>


Top
 Profile  
 
 Post subject: Re: hibernate search returns no result
PostPosted: Wed Jun 07, 2017 8:33 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
The code looks ok.

My guess is that you're not passing a matching value for keyWord: bear in mind that you're using the default Analyzer (I see no analyzer definitions) which will lowercase all input, and tokenize it on whitespace.

For example if nameEn = "Hello World", this will create two matching tokens in the index: "hello" and "world" (in no particular order).

Since you're using a .keyword() type of query, the matching clause needs to match exactly one such token.

If you search for "Hello World" you will find absolutely nothing as it doesn't match neither "hello" nor "world". Try to search for "hello" and you'll find the entity.

It is important to understand how text will be processed, especially if you want people to be able to input any phrase. I would suggest to index some test data and then inspect the content of the index with Luke, that will help you understand how the text is processed.

Text analysis is extremely flexible and you can then play with the various options to achieve advanced matching.

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


Top
 Profile  
 
 Post subject: Re: hibernate search returns no result
PostPosted: Thu Jun 08, 2017 7:53 am 
Newbie

Joined: Wed Jun 07, 2017 3:21 pm
Posts: 4
Hi,
thank you for your reply :)

i add WhitespaceAnalyzer Analyzer

now i want to extract a keyword from a text

For example if nameEn = "Hello everybody TODAY" and i do the search for the keyword "body" it shows me the result

I add the code below but it shows me nothing in the console

Code:
List<Product> result = fullTextQuery.list();
QueryScorer scorer = new QueryScorer(luceneQuery);
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b><font color='red'>", "<font/></b>");
Highlighter highlighter = new Highlighter(formatter, scorer);
for (Product product : result) {
   String highlighterString = null;
   try {
      highlighterString = highlighter.getBestFragment(new EnglishAnalyzer(Version.LUCENE_36), "nameEn", product.getNameEn());
      LOGGER.info("*****************************" + highlighterString);
      } catch (IOException | InvalidTokenOffsetsException e) {
         e.printStackTrace();
      }
   }


Code:
@Entity
@Table(name = "MP_PRODUCT")
@Indexed
@Analyzer(impl = WhitespaceAnalyzer.class)
public class Product implements Serializable {

   @Id
   @DocumentId
   @GenericGenerator(name = "table", strategy = "enhanced-table", parameters = {
         @Parameter(name = "table_name", value = "PRODUCT_SEQ"), @Parameter(name = "initial_value", value = "1"),
         @Parameter(name = "increment_size", value = "1") })
   @GeneratedValue(generator = "table", strategy = GenerationType.TABLE)
   @Column(name = "ID_PRODUCT")
   private Long id;

   @Column(name = "NAME_EN")
   @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Analyzer(impl = EnglishAnalyzer.class)
   private String nameEn;

        @Column(name = "NAME_FR")
   @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Analyzer(impl = FrenchAnalyzer.class)
   private String nameFr;

   @Column(name = "NAME_AR")
   @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Analyzer(impl = ArabicAnalyzer.class)
   private String nameAr;


Top
 Profile  
 
 Post subject: Re: hibernate search returns no result
PostPosted: Thu Jun 08, 2017 8:24 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
You need to learn how to configure Analyzers to get the effects you're after.
The Apache Lucene documentation is more suited on this topic:
- https://lucene.apache.org/core/5_5_0/co ... mmary.html


You can easily try a new Analyzer out by using
Code:
org.hibernate.search.util.AnalyzerUtils
, or define new Analyzers for Hibernate Search to use using the @AnalyzerDef annotation https://docs.jboss.org/hibernate/stable ... #_analyzer

Specifically if you want to extract body out of everybody you'll want to look into stemming analyzers.

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


Top
 Profile  
 
 Post subject: Re: hibernate search returns no result
PostPosted: Thu Jun 08, 2017 3:50 pm 
Newbie

Joined: Wed Jun 07, 2017 3:21 pm
Posts: 4
Hi,
I have a problem with NGramFilterFactory

NameEn = "Hello everybody TODAY"
For example when i search for "b", it shows me the result
But when i search for "bo" or "bod" or "body", it shows me nothing

Now when i change minGramSize to 2
When i searched for "bo", it shows me the result
And the other key words "b", "bod", "body" it shows me nothing

i do not know where the problem exactly is, because according to the code below, when i entered "bo"
Output will be: "b", "bo"

Code:
@Indexed
@AnalyzerDef(name = "autocompleteNGramAnalyzer", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class), filters = {
      @TokenFilterDef(factory = WordDelimiterFilterFactory.class),
      @TokenFilterDef(factory = LowerCaseFilterFactory.class),
      @TokenFilterDef(factory = NGramFilterFactory.class, params = {
            @Parameter(name = "minGramSize", value = "1"),
            @Parameter(name = "maxGramSize", value = "50") }),
      @TokenFilterDef(factory = PatternReplaceFilterFactory.class, params = {
            @Parameter(name = "pattern", value = "([^a-zA-Z0-9\\.])"),
            @Parameter(name = "replacement", value = " "), @Parameter(name = "replace", value = "all") }) })
@Analyzer(definition = "autocompleteNGramAnalyzer")
public class Product implements Serializable {

@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private String nameEn;


Code:
org.apache.lucene.search.Query luceneQuery = queryBuilder.phrase().withSlop(2).onField("nameEn").boostedTo(5).sentence(keyWord.toLowerCase()).createQuery();


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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.