使用空键过滤掉elasticserach聚合桶

时间:2017-08-07 10:50:34

标签: elasticsearch

我有一个如下所示的聚合查询

"subjectArea.untouched" : {
  "terms" : {
    "field" : "subjectArea.untouched",
    "size" : 10,
    "exclude" : "" //to exclude buckets with empty string key
  }
}

但结果不符合预期

"aggregations" : {
"subjectArea.untouched" : {
  "doc_count_error_upper_bound" : 0,
  "sum_other_doc_count" : 0,
  "buckets" : [ {
    "key" : "", //Not expecting this bucket
    "doc_count" : 13
  }, {
    "key" : "subjectArea",
    "doc_count" : 1
  }, {
    "key" : "test1000",
    "doc_count" : 1
  } ]
}
}

我不想要结果中的第一个桶。有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

您需要在查询的过滤器部分中排除包含空字符串的文档。 对于您的用例,查询将如下所示:

{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "subjectArea.untouched": ""
          }
        }
      ]
    }
  },
  "aggs": {
    "subjectArea.untouched": {
      "terms": {
        "field": "subjectArea.untouched",
        "size": 10
      }
    }
  },
  "size": 0
}

基本上,查询的第一部分表现为sql中的 WHERE 子句。

修改 要过滤掉只是存储桶(不过滤掉文档),您应该使用filter aggregations。 查询看起来像:

{
  "aggs": {
    "filter_out": {
      "filter": {
        "bool": {
          "must_not": {
            "match": {
              "subjectArea.untouched": ""
            }
          }
        }
      },
      "aggs": {
        "subjectArea.untouched": {
          "terms": {
            "field": "subjectArea.untouched",
            "size": 10
          }
        }
      }
    }
  },
  "size": 0
}

答案 1 :(得分:0)

原始查询的问题是function strip_php_extension() { $uri= basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']); $ext = substr(strrchr($uri, '.'), 1); if ($ext == 'php') { $url1 = substr($uri, 0, strrpos($uri, '.')); if($_SERVER['QUERY_STRING']) { $url=$url1 ."?". $_SERVER['QUERY_STRING']; } else { $url=$url1; } redirect($url); } } function redirect($url) { if (!headers_sent()) { /* If headers not yet sent => do php redirect */ header('Location: '.$url); exit; } else { /* If headers already sent => do javaScript redirect */ echo '<script type="text/javascript">'; echo 'window.location.href="'.$url.'";'; echo '</script>'; /* If javaScript is disabled => do html redirect */ echo '<noscript>'; echo '<meta http-equiv="refresh" content="0; url='.$url.'" />'; echo '</noscript>'; exit; } } //call function simply strip_php_extension(); //And Write this code in your .htaccess file RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php [NC,L] Thanks. 应该获取一个值数组。因此,只需将查询更改为:

exclude

,它将按预期工作。 公认的解决方案似乎过于复杂。 Reference here