简体   繁体   English

如何使用Hibernate搜索在多个字段中进行搜索?

[英]How can I use Hibernate search to search on several fields?

I'm trying to find a way to search items in my database using Hibernate search. 我正在尝试使用Hibernate搜索找到一种在我的数据库中搜索项目的方法。

What I have is an entity with several fields, and the fields I want to search on are indexed. 我所拥有的是具有多个字段的实体,并且我要搜索的字段被编入索引。 I would like to be able to search items with fieldA==valueA && fieldB==valueB (and maybe add more field to this search, if the user select more search criteria). 我希望能够使用fieldA==valueA && fieldB==valueB搜索项目(如果用户选择更多搜索条件,可能会在此搜索中添加更多字段)。

I am not even sure I should use Hibernate search for that or just construct a custom query. 我甚至不确定我应该使用Hibernate搜索还是只构建一个自定义查询。

Did you try a hql query? 你尝试过hql查询吗?

Query q = s.createQuery("from foo Foo as foo where foo.name=:name and foo.size=:size");
q.setProperties(fooBean); // fooBean has getName() and getSize()
List foos = q.list();

Source: http://docs.jboss.org/hibernate/core/3.5/reference/en/html/queryhql.html 资料来源: http//docs.jboss.org/hibernate/core/3.5/reference/en/html/queryhql.html

Edited: 编辑:

Sees for a Lucene example to search on different fields https://stackoverflow.com/a/16009337/7141562 看到Lucene示例搜索不同的字段https://stackoverflow.com/a/16009337/7141562

Anyway, i would stay with the JPA/Hibernate mode because it seems simpler and clearer solution. 无论如何,我会继续使用JPA / Hibernate模式,因为它看起来更简单,更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM