Skip to content

Instantly share code, notes, and snippets.

@muhdkhokhar
Last active March 12, 2023 17:06
Show Gist options
  • Save muhdkhokhar/2d8dee0c684413e874099b2f55c49712 to your computer and use it in GitHub Desktop.
Save muhdkhokhar/2d8dee0c684413e874099b2f55c49712 to your computer and use it in GitHub Desktop.
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