小编典典

在elasticsearch上查找具有空字符串值的文档

elasticsearch

我一直在尝试使用elasticsearch过滤仅在正文中包含空字符串的那些文档。到目前为止,我还没有运气。

在继续之前,我要提到的是,我已经尝试过遍及Interwebz和StackOverflow 的 许多 “解决方案”。

因此,以下是我要运行的查询,其后是对应的查询:

{
    "query": {
        "filtered":{
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "missing":{
                                "field":"_textContent"
                            }
                        }
                    ]
                }
            }
        }
    }
}

我也尝试了以下方法:

 {
    "query": {
        "filtered":{
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "missing":{
                                "field":"_textContent",
                                "existence":true,
                                "null_value":true
                            }
                        }
                    ]
                }
            }
        }
    }
}

以及以下内容:

   {
    "query": {
        "filtered":{
            "filter": {
                    "missing": {"field": "_textContent"}
            }
        }
    }
}

以上都不起作用。当我确定有包含空字符串字段的记录时,我得到一个空结果集。

如果有人能为我提供任何帮助,我将非常感谢。

谢谢!


阅读 1273

收藏
2020-06-22

共1个答案

小编典典

如果您使用默认的分析器(standard),则没有任何内容可以分析它是否为空字符串。因此,您需要逐字索引字段(未分析)。这是一个例子:

添加一个映射,该映射将为未标记的字段建立索引,如果您还需要索引的字段的标记化副本,则可以使用“
多字段”类型。

PUT http://localhost:9200/test/_mapping/demo
{
  "demo": {
    "properties": {
      "_content": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}

接下来,索引几个文档。

/POST http://localhost:9200/test/demo/1/
{
  "_content": ""
}

/POST http://localhost:9200/test/demo/2
{
  "_content": "some content"
}

执行搜索:

POST http://localhost:9200/test/demo/_search
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "_content": ""
        }
      }
    }
  }
}

返回带有空字符串的文档。

{
    took: 2,
    timed_out: false,
    _shards: {
        total: 5,
        successful: 5,
        failed: 0
    },
    hits: {
        total: 1,
        max_score: 0.30685282,
        hits: [
            {
                _index: test,
                _type: demo,
                _id: 1,
                _score: 0.30685282,
                _source: {
                    _content: ""
                }
            }
        ]
    }
}
2020-06-22