Elasticsearch嵌套路径查询成一个对象类型
Elasticsearch nested path query into an object type
有了这个模板(简化版)。
{
"index_patterns": "index_pattern*",
"order": 1,
"version": 1,
"aliases": {
"some_alias": {}
},
"settings": {
"number_of_shards": 5,
},
"mappings": {
"dynamic": "false",
"properties": {
"someId": {
"type": "keyword"
},
"audience": {
"type": "object",
"properties": {
....
"ageRanges": {
"type": "nested",
"properties": {
"ageTo": {
"type": "integer"
},
"ageFrom": {
"type": "integer"
}
}
}
}
}
}
}
}
我需要查询 audience.ageRanges
是否不存在,或者是否存在应用其他过滤器。
假设我们要搜索具有特定 someId
值的文档是否符合 audience.ageRanges
查询子句(为清楚起见已删除)。
它有一些受众属性,但没有 ageRanges
.
"audience": {
"genders": [
"any"
],
"deviceType": "any"
}
下面不应该查询return具体的文档吗?
{
"query": {
"bool": {
"must": [
{
"term": {
"someId": {
"value": "03183f31"
}
}
},
{
"nested": {
"path": "audience.ageRanges",
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "audience.ageRanges"
}
}
]
}
}
}
}
]
}
}
}
我的结果是 0,它的工作原理有点令人困惑。
尝试使用包含 audience.ageRanges
项的文档 ID 并将 must_not
嵌套查询更改为 must
将 return 结果。
不应将 must_not
放入嵌套查询中,而应将嵌套查询放入 must_not
.
将示例索引数据视为
{
"someId":123,
"audience": {
"genders": [
"any"
],
"deviceType": "any"
}
}
您需要修改您的搜索查询,如下所示 -
搜索查询:
{
"query": {
"bool": {
"must": [
{
"term": {
"someId": {
"value": "123"
}
}
},
{
"bool": {
"must_not": {
"nested": {
"path": "audience.ageRanges",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "audience.ageRanges"
}
}
]
}
}
}
}
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "65852173",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"someId": 123,
"audience": {
"genders": [
"any"
],
"deviceType": "any"
}
}
}
]
有了这个模板(简化版)。
{
"index_patterns": "index_pattern*",
"order": 1,
"version": 1,
"aliases": {
"some_alias": {}
},
"settings": {
"number_of_shards": 5,
},
"mappings": {
"dynamic": "false",
"properties": {
"someId": {
"type": "keyword"
},
"audience": {
"type": "object",
"properties": {
....
"ageRanges": {
"type": "nested",
"properties": {
"ageTo": {
"type": "integer"
},
"ageFrom": {
"type": "integer"
}
}
}
}
}
}
}
}
我需要查询 audience.ageRanges
是否不存在,或者是否存在应用其他过滤器。
假设我们要搜索具有特定 someId
值的文档是否符合 audience.ageRanges
查询子句(为清楚起见已删除)。
它有一些受众属性,但没有 ageRanges
.
"audience": {
"genders": [
"any"
],
"deviceType": "any"
}
下面不应该查询return具体的文档吗?
{
"query": {
"bool": {
"must": [
{
"term": {
"someId": {
"value": "03183f31"
}
}
},
{
"nested": {
"path": "audience.ageRanges",
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "audience.ageRanges"
}
}
]
}
}
}
}
]
}
}
}
我的结果是 0,它的工作原理有点令人困惑。
尝试使用包含 audience.ageRanges
项的文档 ID 并将 must_not
嵌套查询更改为 must
将 return 结果。
不应将 must_not
放入嵌套查询中,而应将嵌套查询放入 must_not
.
将示例索引数据视为
{
"someId":123,
"audience": {
"genders": [
"any"
],
"deviceType": "any"
}
}
您需要修改您的搜索查询,如下所示 -
搜索查询:
{
"query": {
"bool": {
"must": [
{
"term": {
"someId": {
"value": "123"
}
}
},
{
"bool": {
"must_not": {
"nested": {
"path": "audience.ageRanges",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "audience.ageRanges"
}
}
]
}
}
}
}
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "65852173",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"someId": 123,
"audience": {
"genders": [
"any"
],
"deviceType": "any"
}
}
}
]