【问题标题】:Delete from elasticsearch all items where field does not match a certan value从弹性搜索中删除字段与某个值不匹配的所有项目
【发布时间】:2014-02-18 02:47:28
【问题描述】:

我正在尝试从 Elasticsearch 索引中删除字段 time_crawl_started 与特定值不匹配的所有项目。我将 match_all 查询与 NOT 过滤器结合使用。

这是我目前得到的:

    $client = new Elasticsearch\Client();
    $params = Array(
      'index' => ...,
      'type'  => ...
    );
    $params['body']['query']['filtered']['query']['match_all'] = Array();
    $params['body']['query']['filtered']['filter']['not']['term']['time_crawl_started'] = $someDate;
    $client->deleteByQuery($params);

问题在于,这会删除所有项目,甚至那些将 time_crawl_started 设置为 $someDate 的项目,这只是一个日期时间,例如“2014-02-17 19:13:31”。

我应该如何更改它以仅删除日期不正确的项目?

【问题讨论】:

  • 尝试使用带有must_not 子句的布尔查询,而不是使用not 过滤器
  • 没有帮助...我还在尝试各种组合。

标签: elasticsearch


【解决方案1】:

问题在于 time_crawl_started 字段已被分析,因此任何按值进行的比较都是错误的。我必须手动创建索引(而不是通过仅将新文档插入不存在的索引来自动创建索引)并为我的项目类型指定映射,为 time_crawl_started 设置 'index' => 'not_analyzed'。

我最终使用了这样的脚本过滤器:

    $params['body']['query']['filtered']['query']['match_all'] = Array();
    $params['body']['query']['filtered']['filter']['script']['script'] = "doc['time_crawl_started'].value != \"" . $someDate . "\"";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多