我希望能够使用休眠搜索来仅搜索用户在表单中选择的字段。
例如,如果用户选择按名字搜索,则休眠应仅考虑名字。如果用户选择名字和姓氏,则休眠状态应考虑名字和姓氏。
FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.
getFullTextEntityManager(entityManager);
QueryBuilder queryBuilder =
fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Client.class).get();
org.apache.lucene.search.Query query =
queryBuilder
.keyword()
.wildcard()
// i want below section (fields) to change based on user input but can't do it with list<string> or collection
.onFields("idNumber", "firstName", "middleName","lastName","nationality")
.matching("*"+searchText.toLowerCase()+"*")
.createQuery();
答案 0 :(得分:0)
使用List.toArray
:
List<String> fieldsSelectedByUser = ...;
FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.
getFullTextEntityManager(entityManager);
QueryBuilder queryBuilder =
fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Client.class).get();
org.apache.lucene.search.Query query =
queryBuilder
.keyword()
.wildcard()
.onFields(fieldsSelectedByUser.toArray(new String[0]))
.matching("*"+searchText.toLowerCase()+"*")
.createQuery();