ElasticSearch按嵌套的布尔类型字段过滤 [英] ElasticSearch filter by nested boolean type fields
问题描述
我需要查询布尔类型的多个嵌套字段.映射结构:
映射":{特性" : {领导者":{"type":嵌套",特性" : {"except_1":{"type":布尔值"},"except_2":{"type":布尔值"},柜台" : {"type":整数"}}}}}
我正在尝试仅对 False
使用查询 except1
和 except2
.不幸的是,在我的尝试下,它为两个字段都返回了 True
和 False
,而我无法修复它.
查询":{嵌套":{"path":"leaders",询问": {布尔":{必须": [{学期": {"leaders.except_1":错误}},{学期": {"leaders.except_2":错误}}]}}}}
您可能正在寻找的是 inner_hits
选项-仅显示匹配的嵌套子文档.
PUT主管{"mappings":{"properties":{"leaders":{"type":"nested","properties":{"except_1":{"type":"boolean"},"except_2":{"type:" boolean}," counter:{" type:" integer}}}}}}}POST负责人/_doc{领导者":[{"except_1":是,"except_2":否},{"except_1":否,"except_2":否}]}GET Leaders/_搜索{询问": {嵌套":{"path":"leaders","inner_hits":{},询问": {布尔":{必须": [{学期": {"leaders.except_1":否}},{学期": {"leaders.except_2":否}}]}}}}}
然后
获取领导者/_搜索{询问": {嵌套":{"path":"leaders","inner_hits":{},询问": {布尔":{必须": [{学期": {"leaders.except_1":否}},{学期": {"leaders.except_2":否}}]}}}}}
屈服
{点击数":[{"_index":领导者","_type":"_ doc","_id":"u-he8HEBG_KW3EFn-gMz","_score":0.87546873,"_source":{<-默认行为领导者":[{"except_1":是,"except_2":false},{"except_1":否,"except_2":false}]},"inner_hits":{领导者":{点击数":{全部的":{值":1,"relation":"eq"},"max_score":0.87546873,"hits":[< -------仅匹配的嵌套子文档{"_index":领导者","_type":"_ doc","_id":"u-he8HEBG_KW3EFn-gMz","_nested":{"field":"leaders",偏移":1},"_score":0.87546873,_来源":{"except_1":否,"except_2":false}}]}}}}]}
此外,您可以通过在搜索查询的顶层说出"_ source":"inner_hits"
来强制系统仅返回 inner_hits
.>
I need to query on multiple nested fields on boolean types. Structure of mapping:
"mappings" : {
"properties" : {
"leaders" : {
"type" : "nested",
"properties" : {
"except_1" : {
"type" : "boolean"
},
"except_2" : {
"type" : "boolean"
},
"counter" : {
"type" : "integer"
}
}
}
}
}
I am trying to use query both except1
and except2
only to False
.
Below my try, unfortunately it returns True
and False
for both fields and I cannot fix it.
"query": {
"nested": {
"path": "leaders",
"query": {
"bool": {
"must": [
{
"term": {
"leaders.except_1": False
}
},
{
"term": {
"leaders.except_2": False
}
}
]
}
}
}
}
What you're probably looking for is the inner_hits
option -- showing only the matched nested subdocuments.
PUT leaders
{"mappings":{"properties":{"leaders":{"type":"nested","properties":{"except_1":{"type":"boolean"},"except_2":{"type":"boolean"},"counter":{"type":"integer"}}}}}}
POST leaders/_doc
{
"leaders": [
{
"except_1": true,
"except_2": false
},
{
"except_1": false,
"except_2": false
}
]
}
GET leaders/_search
{
"query": {
"nested": {
"path": "leaders",
"inner_hits": {},
"query": {
"bool": {
"must": [
{
"term": {
"leaders.except_1": false
}
},
{
"term": {
"leaders.except_2": false
}
}
]
}
}
}
}
}
then
GET leaders/_search
{
"query": {
"nested": {
"path": "leaders",
"inner_hits": {},
"query": {
"bool": {
"must": [
{
"term": {
"leaders.except_1": false
}
},
{
"term": {
"leaders.except_2": false
}
}
]
}
}
}
}
}
yielding
{
"hits":[
{
"_index":"leaders",
"_type":"_doc",
"_id":"u-he8HEBG_KW3EFn-gMz",
"_score":0.87546873,
"_source":{ <-- default behavior
"leaders":[
{
"except_1":true,
"except_2":false
},
{
"except_1":false,
"except_2":false
}
]
},
"inner_hits":{
"leaders":{
"hits":{
"total":{
"value":1,
"relation":"eq"
},
"max_score":0.87546873,
"hits":[ <------- only the matching nested subdocument
{
"_index":"leaders",
"_type":"_doc",
"_id":"u-he8HEBG_KW3EFn-gMz",
"_nested":{
"field":"leaders",
"offset":1
},
"_score":0.87546873,
"_source":{
"except_1":false,
"except_2":false
}
}
]
}
}
}
}
]
}
Furthermore, you can force the system to only return the inner_hits
by saying "_source": "inner_hits"
on the top-level of your search query.
这篇关于ElasticSearch按嵌套的布尔类型字段过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!