hibernate-search - 如何在 JSON 对象属性上动态创建索引(JSON 对象道具也是动态的)
问题描述
我有一个场景,我想在 JSON 对象的键上动态创建索引(JSON 对象属性会有所不同)。我能够将 JSON 对象存储为索引(通过实现FieldBridge
)。
eg1: preference:{"sport":"football", "music":"pop")
eg2: preference:{"sport":"cricket", "music":"jazz", "cuisine":"mexican"}
但我无法查询单个字段,例如:
preference.sport
或preference.cuisine
休眠搜索中是否有任何方法/配置可以实现这一目标?
解决方案
If your fields are dynamic, there is no pre-defined schema and Hibernate Search is unable to determine how to query these fields. There are significant differences in how a match query should be executed on a text field or a date field, for example.
For that reason, you cannot use the Hibernate Search Query DSL to build your queries.
However, you can use native APIs.
If you're using the Lucene integration, just creating the relevant queries yourself will work fine (as long as you create the right one):
new TermQuery(new Term("sport", "value"))
If you're using the experimental Elasticsearch integration, you can use org.hibernate.search.elasticsearch.ElasticsearchQueries.fromJson( ... )
. You will have to write the whole query as JSON, though, and will not be able to take advantage of the Hibernate Search QueryBuilder at all, even for queries on statically defined fields. See https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#_queries
Better support for native queries, as well as dynamic fields with pre-defined types, which would be targetable in the Query DSL, is planned for Hibernate Search 6, but it's not there yet. See HSEARCH-3273.
推荐阅读
- javascript - 从桌面上的工具提示切换到移动设备上的弹出窗口
- java - Android:图像意图、获取多个、排序
- java - 已解决 [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 错误
- postgresql - Postgres从具有多个值的选择语句中插入
- python - 如何计算它在字典中出现相同字符串的次数
- exchange-server - Exchange Online 邮件速率发送限制问题
- python - 比较列表python中的字典键值
- sql - SQL Server 避免重复相同的连接
- javascript - Discord.js中文字符显示为框?
- elasticsearch - StormCrawler 站点地图 url 被注入到 Elastic 中,并处于错误状态。我们在种子.txt 中有一个 sitemap.xml 链接