Showing posts with label apache lucene. Show all posts
Showing posts with label apache lucene. Show all posts

Sunday, January 1, 2023

My GitHub Repo #03 : Brahmashira

Code Samples for the Blog Article 
[Starting Search with Apache Lucene 5.3.x/5.4.x]
MIT License, Copyright (c) 2018-19, Sumith Kumar Puri
https://github.com/sumithpuri








Project Codename

Brahmashira

Blog Post URL

http://www.techilashots.blog/2016/03/starting-search-with-apache-lucene.html

Blog Short URL

https://rebrand.ly/skp-ts-blog-03

Package Prefix

me.sumithpuri.github.brahmashira

GitHub URL

https://github.com/sumithpuri/skp-code-marathon-brahmashira

Contact E-Mail

code@sumithpuri.xyz

Contact Number

+91 9591497974 (WhatsApp, Viber, Telegram)

Historical

 Started this Movement of 1000s of Lines of Java / J2EE* Code to GitHub

 Was a Senior Software Architect (Java/J2EE) in Manila*, 2018 (At Start) 

 Named this Initial Code Journey as [ Manila Code Marathon - 2018 ]

 Code Is Non-Proprietary and Non-Copyright from my Work Experience.

 Was Back to Bangalore, Named as [ Bangalore Code Nights - 2019. ]

 Added More Code under [ -20 Days of Code in Benglauru- ] in 2020

 Celebration of Java/Java EE Code as Java Turned 25 in the Year ~ 2020!

  

Tuesday, April 19, 2016

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

The Stop Words like ["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", "with"] and the existence of them in terms or database 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 and 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 if we use 'StandardAnalyzer' by passing in the argument for Stop Words as CharArraySet.EMPTY_SET I found that the Query was still not able to retrreve any result. On Analysis with Luke, I found that for 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'.

 
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.

Tuesday, March 22, 2016

Starting Search with Apache Lucene 5.3.x/5.4.x

Before we delve into Apache Lucene, the following are the most important terms that you need to be familiar with. This will also help you clarify a few terms before getting into 'search' or 'information retrieval':

Let us get ahead with Apache Lucene 5.3.x/5.4.y; The most important aspects of Lucene is mentioned under each of the headings.

Fig. 0 : Lucene - Search/Process Architecture (Usage)




1. Lucene Introduction (Usage)
  • Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform.
  • Apache Lucene is an open source project available for free download
  • Scalable, High-Performance Indexing
  • Powerful, Accurate and Efficient Search Algorithms
  • Cross-Platform Solution
  •  
2. LuceneTerms (Concepts)
  • Inverted Index
    Inverted Index is used to get traverse from the string or search term to the document id's or locations of these terms. If we were to visualize this in terms of an 'index' - it would be 'inverted', as we would be using the term as a handle to retrieve 'id' or 'locations' - reverse of the popular usage of an index.


  • Index
    Index is an handle (information) that can be used to get further related information from a file, database or any other source of data. Usually, Index is also accompanied by compression, check-sum, hash or location of the remaining data. Index contains multiple Documents.

  • Document
    Document is a collection of Fields and the Values against each of the Fields. It is more like saying "Employee Name" - "Sumith Puri" | "Employee Desingation" - "Software Architect" | "Employee Age" - "33" | "Employee ID" - "067X" forms a document. The Lucene Indexing Process adds multiple documents to an Index. The entire set of Documents is called the Corpus.
     
  • Field
    Field
    contains Terms and are simply 'Sets of Tokens' of information. The Lucene Indexing process take care to Identify (or Process) Fields and Index them. Fields belong to a Document always.

  • Terms
    Terms are nothing but a 'Token' or 'String' of Information. This 'Term' is the smallest piece of Information that will be Indexed to form the Inverted Index. Set of Distince Terms is called the Vocabulary.

  • String
    String is simply a 'Token' or the English Language String.

  • Segment
    Segment is a fragmented or chunked part of the entire Index, for better storage and faster retrieval.

3. Lucene Segment (Indexing)

Each segment index maintains the following:
 

Field Names: This contains the set of field names used in the index.

Stored Field Values: This contains, for each document, a list of attribute-value pairs, where the attributes are field names. These are used to store auxiliary information about the document, such as its title, url, or an identifier to access a database. The set of stored fields are what is returned for each hit when searching. This is keyed by document number.   
 

Term Dictionary: A dictionary containing all of the terms used in all of the indexed fields of all of the documents. The dictionary also contains the number of documents which contain the term, and pointers to the term's frequency and proximity data.
 

Term Frequency Data: For each term in the dictionary, the numbers of all the documents that contain that term, and the frequency of the term in that document, unless frequencies are omitted (IndexOptions.DOCS_ONLY)
 

Term Proximity Data: For each term in the dictionary, the positions that the term occurs in each document. Note that this will not exist if all fields in all documents omit position data.
 

Normalization Factors: For each field in each document, a value is stored that is multiplied into the score for hits on that field.
 

Term Vectors: For each field in each document, the term vector (sometimes called document vector) may be stored. A term vector consists of term text and term frequency. To add Term Vectors to your index see the Field constructors

Deleted Documents: An optional file indicating which documents are deleted. 




4. Lucene Internals (Architecture)


    Fig. 1: Lucene Architectural Layers [Non-Copyrighted Image]