休眠搜索中的OnFields方法接受哪种数据类型

时间:2019-08-26 12:50:24

标签: java hibernate spring-boot hibernate-search

我希望能够使用休眠搜索来仅搜索用户在表单中选择的字段。

例如,如果用户选择按名字搜索,则休眠应仅考虑名字。如果用户选择名字和姓氏,则休眠状态应考虑名字和姓氏。

 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();

1 个答案:

答案 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();