How do I search multiple fields for a given string?
I.E. if I have the following to fields indexed
Code:
parents.parentName
parents.childName
I have the following code to search in one field, but reading the api I am not sure how to search both fields with one search because when construct a QueryParser you have to give it a field. Can you give it mulitple fields?
Code:
try {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
// Problem this only searches the parents.parentName field
QueryParser parser = new QueryParser("parents.parentName", new SimpleAnalyzer() );
System.out.println("Searching for string: " + search);
Query luceneQuery = parser.parse( search );
javax.persistence.Query fullTextQuery =
fullTextEntityManager.createFullTextQuery( luceneQuery );
@SuppressWarnings("unchecked")
List<Top> result = (List<Top>) fullTextQuery.getResultList(); //return a list of managed objects
if (result == null) {
System.out.println("ERROR: query returned null list");
} else if (result.size() < 1) {
System.out.println("ERROR: query returned list of size: " + result.size());
} else {
System.out.println("PASS: query returned list of size: " + result.size());
for (Top t : result) {
System.out.println("Found in top id: " + t.getId());
}
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}