我正在使用Grails插件通过MySQL与ElasticSearch一起使用。我在域类中映射了一个域列,如下所示:
String updateHistoryJSON
(...)
static mapping = {
updateHistoryJSON type: 'text', column: 'update_history'
}
在MySQL中,这基本上映射到TEXT列,其目的是存储JSON内容。
因此,在数据库索引和ElasticSearch索引中,我都有2个实例:
-实例1具有updateHistoryJSON ='{“zip”:null,“street”:null,“name”:null,“categories”:[],“city”:null}'
-实例2具有updateHistoryJSON ='{}'
现在,我需要的是一个仅返回实例2的ElasticSearch查询。
我一直在使用Groovy DSL进行这样的关闭:
{
bool {
must_not = term(updateHistoryJSON: "{}")
minimum_should_match = 1
}
}
而ElasticSearch似乎忽略了它,它不断带回两个实例。
另一方面,如果我使用“missing”:{“field”:“updateHistoryJSON”}之类的过滤器,则不会返回任何文档。 “exists”:{“field”:“updateHistoryJSON”}也一样。
我在这里做错什么的任何想法吗?
最佳答案
我仍然不确定问题出在哪里,但至少我找到了解决方法。
由于基于updateHistoryJSON内容的搜索无法正常工作,因此我决定使用脚本根据updateHistoryJSON内容的大小进行搜索,这意味着,不是查找具有非空JSON的文档,而是查找updateHistoryJSON大小较大的文档大于2({} ==大小2)。
我使用的闭包是这样的:
{script = {
script = "doc['updateHistoryJSON'].size() > 2"
}
关于grails - 如何在ElasticSearch + Grails中使用具有空JSON属性的must_not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28947809/