gpt4 book ai didi

java - hibernate搜索中的OnFields方法接受什么数据类型

转载 作者:行者123 更新时间:2023-12-02 01:16:19 24 4
gpt4 key购买 nike

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

例如,如果用户选择按名字搜索,hibernate 应该只考虑名字。如果用户选择名字和姓氏,hibernate 应该考虑名字和姓氏。

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

最佳答案

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

关于java - hibernate搜索中的OnFields方法接受什么数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57658246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com