elasticsearch - ElasticSearch 应该嵌套和 bool must_not 存在
问题描述
使用以下映射:
"categories": {
"type": "nested",
"properties": {
"category": {
"type": "integer"
},
"score": {
"type": "float"
}
}
},
我想使用该categories
字段返回以下文件:
- 在给定类别中得分高于阈值,或
- 没有类别字段
这是我的查询:
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"terms": {
"categories.category": [
<id>
]
}
},
{
"range": {
"categories.score": {
"gte": 0.5
}
}
}
]
}
}
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "categories"
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
它正确返回带有和不带有类别字段的文档,并对结果进行排序,以便我想要的结果排在第一位,但它不会过滤得分低于 0.5 阈值的结果。
解决方案
好问题。
那是因为categories
从弹性搜索的角度来看,它并不完全是一个字段[在其上创建倒排索引并用于查询/搜索的字段],而是categories.category
and categories.score
is。
结果categories
在任何文档中都找不到,这实际上对所有文档都是正确的,您可以观察您所看到的结果。
将查询修改为以下内容,您会看到您的用例正常工作。
POST <your_index_name>/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"terms": {
"categories.category": [
"100"
]
}
},
{
"range": {
"categories.score": {
"gte": 0.5
}
}
}
]
}
}
}
},
{
"bool": {
"must_not": [ <----- Note this
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "categories.category"
}
},
{
"exists": {
"field": "categories.score"
}
}
]
}
}
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
推荐阅读
- java - 减少数组的多个值
- javascript - 展示
- javascript - 传递给子组件的道具在 Nuxt 中返回为未定义
- jquery - Jquery验证不适用于表中的第二行
- javascript - 如何使 Visual Studio 自动完成 npm 模块方法?
- android - 满足条件时可点击的按钮(Android Studio)
- xampp - 我可以通过移动设备上的 IP 在本地查看我的网站,但按钮无法保存数据等
- php - 如何在表数据中有图像的 JQUERY 数据表中显示分页和搜索?
- swift - 反向地理编码 CLGeocoder 内存泄漏
- python - 页面破折号侧边栏的回调不起作用