elasticsearch 在elasticsearch中查找空字符串
我正在尝试搜索字段中具有特定值的文档elasticsearch 在elasticsearch中查找空字符串,elasticsearch,elasticsearch,我正在尝试搜索字段中具有特定值的文档 { "query": { "bool": { "must": [ {"field": {"advs.status": "warn"}} ] } } } 这很有效。但当我试图查找该字段中有空字符串的文档时,会出现以下错误: ParseException[Cannot parse '' ... 然后是一个很长的预期内容列表,而不是空字符串 我尝试以下查询: { "query"
{
"query": {
"bool": {
"must": [
{"field": {"advs.status": "warn"}}
]
}
}
}
这很有效。但当我试图查找该字段中有空字符串的文档时,会出现以下错误:
ParseException[Cannot parse '' ...
然后是一个很长的预期内容列表,而不是空字符串
我尝试以下查询:
{
"query": {
"bool": {
"must": [
{"term": {"advs.status": ""}}
]
}
}
}
它不会失败,但什么也找不到。它适用于非空字符串。我该怎么做
此类型的映射与此完全相同:
{
"reports": {
"dynamic": "false",
"_ttl": {
"enabled": true,
"default": 7776000000
},
"properties": {
"@fields": {
"dynamic": "true",
"properties": {
"upstream_status": {
"type": "string"
}
}
},
"advs": {
"properties": {
"status": {
"type": "string",
"store": "yes"
}
}
},
"advs.status": {
"type": "string",
"store": "yes"
}
}
}
}
在
bool
中尝试使用must\u not
和missing
:
"must_not":{
"missing":{
"field":"advs.status",
"existence":true,
"null_value":true
}
}
或者另一种更有效地执行相同操作的方法是使用
exists
过滤器:
"exists" : {
"field" : "advs.status"
}
两者都有效,但这一个更好:)如果您想搜索包含空字符串的字段,您可以将映射更改为将“未分析”设置为该特定字段,也可以使用脚本过滤器:
"filter": {
"script": {
"script": "_source.advs.status.length() == 0"
}
}
“缺失”只适用于空值或根本不存在。这里已经回答了匹配的空字符串:如果未分析字段,我通常使用过滤器。下面是一个片段:
{
"filtered": {
"filter": {
"term": {
"field": ""
}
}
}
},
您可以尝试此临时解决方案,它有效但不是最佳的-
如何为json启用语法突出显示?能否将这两个答案合并为一个?
PUT t/t/1
{
"textContent": ""
}
PUT t/t/2
{
"textContent": "foo"
}
GET t/t/_search
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "textContent"
}
}
],
"must_not": [
{
"wildcard": {
"textContent": "*"
}
}
]
}
}
}