Over a million developers have joined DZone.

How to Handle Stop Words in Hibernate Search 5.5.2 / Apache Lucene 5.4.x

Making sure that stop words aren't an issue during both indexing and querying your database with Hibernate.

· Big Data Zone

Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brought to you in partnership with Hortonworks.

The Stop Words ["a", "an", "and", "are", "as", "at", "be", "but", "by", "for", "if", "in", "into", "is", "it", "no", "not", "of", "on", "or", "such", "that", "the", "their", "then", "there", "these", "they", "this", "to", "was", "will", and "with"] and their existence in terms, databases, or files that are to be indexed/searched by Lucene can lead to any of the following:

1. Stop Words being Ignored/Filtered during the Lucene Indexing Process

2. Stop Words being Ignored/Filtered during the Lucene Querying Process

3. No Result for Queries that Include, Start With or End With any Stop Word

The way to solve this problem or to handle them during both indexing and searching process is as follows. The method explained here is specially suitable if you are using Hibernate Search 5.5.2 which in turn is using Apache Lucene 5.3.x/5.4.x

1. Define Your Custom Analyzer, Adapted From the Standard Analyzer

You need to include only the two filters — 'LowerCaseFilterFactory' and 'StandardFilterFactory' — as part of the Tokenizer definition. The filter factory that we have not included here is the 'StopFilter'. This allows Stop Words to be considered as other normal English Words as they are indexed.

@Entity 
@Indexed 
@Table(name="table_name", catalog="catalog_name") 
@AnalyzerDef(name = "fedexTextAnalyzer",
   tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class), 
   filters = {
     @TokenFilterDef(factory = LowerCaseFilterFactory.class),
     @TokenFilterDef(factory = StandardFilterFactory.class) 
})


2. Mark the Field With Relevant Annotations (@Analyzer on @Field)

Along with the @Field Annotation on every Entity's or Table's Column Field, declare the Analyzer that we have defined above.

@Column(name="fedex_cs_product_name", nullable=false, length=100) 
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO, 
       analyzer=@Analyzer(definition = "fedexTextAnalyzer")) 
public String getFedexCsItemName() {    
  return this.fedexCsItemName;
}


3. Use WhitespaceAnalyzer to Query So That Stop Words Are 'Processed' By Default

Although the official documentation says that we should use 'StandardAnalyzer' by passing in the argument for Stop Words as CharArraySet.EMPTY_SET, I found that the Query was still not able to retrieve any result. On analysis with Luke, I found that in Queries such as 'Computer Science Books for Beginners', the 'for' was being ignored. Strange! I replaced it with WhitespaceAnalyzer, I found that it works for all 'Stop Words' and all 'Cases'.

Image title

I have found that the above is the best/minimal way to fix this issue. Also, our QA has verified that it works for all 'Stop Word' cases! Hope this helps you.

Hortonworks DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

Topics:
apache lucene ,hibernate search ,full text search

Published at DZone with permission of Sumith Puri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}