Last active
March 12, 2023 17:06
-
-
Save muhdkhokhar/2d8dee0c684413e874099b2f55c49712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.queryparser.classic.ParseException; | |
import org.apache.lucene.queryparser.classic.QueryParser; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.index.Term; | |
public class QueryBuilder { | |
public static Query buildQuery(String fieldName, String searchTerm) throws ParseException { | |
// Escape any special characters in the search term | |
String escapedSearchTerm = QueryParser.escape(searchTerm); | |
// Build a term for the flattened XML field with the search term | |
Term term = new Term(fieldName, escapedSearchTerm); | |
// Create a query for the term | |
Query query = new TermQuery(term); | |
return query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment